转】C#.Net 上传图片,限制图片大小,检查类型完整版
1.缩小图片:
public class CImageLibrary
{
  public enum ValidateImageResult { OK, InvalidFileSize, InvalidImageSize }
 
  //检查图片大小
  public static ValidateImageResult ValidateImage(string file, int MAX_FILE_SIZE, int MAX_WIDTH, int MAX_HEIGHT)
    {
      byte[] bs = File.ReadAllBytes(file);
     
      double size = (bs.Length / 1024);
      //大于50KB
      if (size > MAX_FILE_SIZE) return ValidateImageResult.InvalidFileSize;
      Image img = Image.FromFile(file);
      if (img.Width > MAX_WIDTH || img.Height > MAX_HEIGHT) return ValidateImageResult.InvalidImageSize;
      return ValidateImageResult.OK;
    }
 
  //按宽度比例缩小图片
  public static Image GetOutputSizeImage(Image imgSource, int MAX_WIDTH)
    {
      Image imgOutput = imgSource;
     
      Size size = new Size(imgSource.Width, imgSource.Height);
      if (imgSource.Width <= 3 || imgSource.Height <= 3) return imgSource; //3X3大小的图片不转换
     
      if (imgSource.Width > MAX_WIDTH || imgSource.Height > MAX_WIDTH)
      {
        double rate = MAX_WIDTH / (double)imgSource.Width;
       
        if (imgSource.Height * rate > MAX_WIDTH)
          rate = MAX_WIDTH / (double)imgSource.Height;
       
          size.Width = Convert.ToInt32(imgSource.Width * rate);
          size.Height = Convert.ToInt32(imgSource.Height * rate);
       
          imgOutput = imgSource.GetThumbnailImage(size.Width, size.Height, null, IntPtr.Zero);
      }
     
      return imgOutput;
    }
 
  //按比例缩小图片
  public static Image GetOutputSizeImage(Image imgSource, Size outSize)
    {
      Image imgOutput = imgSource.GetThumbnailImage(outSize.Width, outSize.Height, null, IntPtr.Zero);
      return imgOutput;
    }
 
  public static byte[] GetImageBytes(string imageFileName)
    {
      Image img = Image.FromFile(imageFileName);
      return GetImageBytes(img);
    }
 
  public static byte[] GetImageBytes(Image img)
    {
      if (img == null) return null;
      try
      {
          System.IO.MemoryStream ms = new MemoryStream();
          img.Save(ms, ImageFormat.Jpeg);
        byte[] bs = ms.ToArray();
          ms.Close();
        return bs;
      }
      catch { return null; }
    }
 
  public static Image FromBytes(byte[] bs)
    {
      if (bs == null) return null;
      try
      {
          MemoryStream ms = new MemoryStream(bs);
          Image returnImage = Image.FromStream(ms);
          ms.Close();
        return returnImage;
       
      }
      catch { return null; }
    }
}
2.自定义类:
public class CFileUpload
{
  private FileUpload _fileUpload;
  private string _savePath;
  private string _LastUploadedFile = string.Empty;
  private bool _AutoGenFileName = false;
  public string LastUploadedFile { get { return _LastUploadedFile; } }
 
  private string PICTURE_FILE = "[.gif.png.jpeg.jpg.bmp]";
  private string ZIP_FILE = "[.zip.rar]";
  private string MUILT_MEDIA_FILE = "[.mpeg.wma]";
 
  private int IMG_MAX_WIDTH = 0;//未指定宽度
  private int IMG_MAX_HEIGHT = 0;//未指定高度
 
  /// <summary>
  /// 构造器
  /// </summary>
  /// <param name="fileUpload">Asp FileUpload对象</param>
  /// <param name="savePath">保存目录,不包含文件名</param>
  /// <param name="autoGenFileName">自动生成文件名</param>
  public CFileUpload(FileUpload fileUpload, string savePath, bool autoGenFileName)
    {
      _savePath = savePath;
      _fileUpload = fileUpload;
      _AutoGenFileName = autoGenFileName;
    }
 
  /// <summary>
  /// 构造器
  /// </summary>
  /// <param name="fileUpload">Asp FileUpload对象</param>
  /// <param name="savePath">保存目录,不包含文件名</param>
  public CFileUpload(FileUpload fileUpload, string savePath)
getsavefilename    {
      _savePath = savePath;
      _fileUpload = fileUpload;
    }
 
  /// <summary>
  /// 上传RAR文件
  /// </summary>
  public bool UploadRARFile()
    {
      return DoUpload(ZIP_FILE);
    }
 
  /// <summary>
  /// 上传视频文件
  /// </summary>
  public bool UploadVideo()
    {
      return DoUpload(MUILT_MEDIA_FILE);
    }
 
  /// <summary>
  /// 上传图片文件
  /// </summary>
  public bool UploadImage()
    {
      return DoUpload(PICTURE_FILE);
    }
 
  public bool UploadImage(int maxWidth, int maxHeight)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。