上传缩略图,等比例压缩

前些日子一位同学问我怎么上传缩略图,并可以指定图片的尺度而且进行等比压缩。晚上,自己没事在网上看到一些网友发的帖子,都是挺好,最后我也写了一个上传的方法。供大家参考:

 

代码
   
     
1 public byte [] UploadFile(FileUpload f_Ifile, int ThumbnailWidth, int ThumbnailHeight, string resPathId, string otherPath)
2 {
3 try
4 {
5 // 获取客户端指定上传文件的访问
6   HttpPostedFile upfile = f_Ifile.PostedFile;
7 // 得到上传文件的长度
8   int UpfileLength = upfile.ContentLength;
9 // 没有选择上传的文件
10   if (UpfileLength == 0 )
11 {
12 return null ;
13 }
14 // 得到上传文件的客户端MIME类型
15   string contentType = upfile.ContentType;
16 byte [] fileArray = new Byte[UpfileLength];
17 Stream fileStream = upfile.InputStream;
18 fileStream.Read(fileArray, 0 , UpfileLength);
19 string filename = Path.GetFileName(upfile.FileName);
20 string sExt = upfile.FileName.Substring(upfile.FileName.LastIndexOf( " . " )).ToLower();
21 // 读取xml判断上传的文件格式及大小
22   string xmlPath = System.Web.HttpContext.Current.Server.MapPath( " ~/File.xml " );
23 bool postfix = Tools.UpDownFile.VerdictPostfixByFile(filename, xmlPath, " FileManager " , " id " , " UpImgFile " , " format " );
24 bool size = Tools.UpDownFile.VerdictSizeByFile(f_Ifile, xmlPath, " FileManager " , " id " , " ImageSize " , " size " );
25 System.Drawing.Image xImage;
26 // 缩略图的高,宽
27 int PhotoHeight, PhotoWidth;
28 // 创建新的图形
29 Rectangle NewPhoto;
30 // 位图图像
31 System.Drawing.Bitmap xBitmap;
32 // 路径
33 // string filePath = "";
34 if (postfix == true && size == true )
35 {
36 xImage = System.Drawing.Bitmap.FromStream(upfile.InputStream);
37 // 赋值,高和宽
38 // this.ThumbnailHeight = xImage.Height.ToString();
39 // this.ThumbnailWidth = xImage.Width.ToString();
40 if ( this .txtwidth.Text == "" )
41 {
42 ThumbnailWidth = Convert.ToInt32(xImage.Width.ToString());
43 }
44 else
45 {
46 ThumbnailWidth = Convert.ToInt32( this .txtwidth.Text);
47 }
48 if ( this .txtheight.Text == "" )
49 {
50 ThumbnailHeight = Convert.ToInt32(xImage.Height.ToString());
51 }
52 else
53 {
54 ThumbnailHeight = Convert.ToInt32( this .txtheight.Text);
55 }
56 // 上传资源模块的缩略图
57 if (resPathId != "" && otherPath == "" )
58 {
59 ResPath rp = BLL.ResPathManager.GetResPathByResModelId(Convert.ToInt32(resPathId));
60 if (rp.ResPaths != "" )
61 {
62 filepath = rp.ResPaths + @" \Pic " ;
63 }
64 else
65 {
66 return null ;
67 }
68 }
69 // 上传的不是资源文件的缩略图
70 else if (resPathId == "" && otherPath != "" )
71 {
72 filepath = otherPath;
73 if ( ! Tools.IOHelp.CreateDirectory(filepath))
74 {
75 return null ;
76 }
77 }
78 else
79 {
80 return null ;
81 }
82 string ranNum = Tools.FormatStr.GetUpFileName() + sExt;
83 filepath = filepath + @" \ " + ranNum;
84 xBitmap = new System.Drawing.Bitmap(fileStream);
85
86 PhotoHeight = xBitmap.Height;
87 PhotoWidth = xBitmap.Width;
88 if (Convert.ToDecimal(PhotoHeight) / Convert.ToDecimal(PhotoWidth) > Convert.ToDecimal(PhotoHeight) / Convert.ToDecimal(PhotoWidth))
89 {
90 PhotoHeight = Convert.ToInt16((Convert.ToDecimal(ThumbnailHeight) / Convert.ToDecimal(ThumbnailWidth)) * Convert.ToDecimal(PhotoWidth));
91 NewPhoto = new Rectangle(Convert.ToInt16((Convert.ToDecimal(xBitmap.Width) - Convert.ToDecimal(PhotoWidth)) / 2 ), 0 , PhotoWidth, PhotoHeight);
92 }
93 else if (Convert.ToDecimal(PhotoHeight) / Convert.ToDecimal(PhotoWidth) < Convert.ToDecimal(ThumbnailHeight) / Convert.ToDecimal(ThumbnailWidth))
94 {
95 PhotoWidth = Convert.ToInt16((Convert.ToDecimal(ThumbnailWidth) / Convert.ToDecimal(ThumbnailHeight)) * Convert.ToDecimal(PhotoHeight));
96
97 NewPhoto = new
98 Rectangle(Convert.ToInt16((Convert.ToDecimal(xBitmap.Width) - Convert.ToDecimal(PhotoWidth)) / 2 ), 0 , PhotoWidth, PhotoHeight);
99 }
100 else
101 {
102 NewPhoto = new Rectangle( 0 , 0 , PhotoWidth, PhotoHeight);
103 }
104
105 System.Drawing.Image myBitmap;
106 myBitmap = xBitmap.Clone(NewPhoto, System.Drawing.Imaging.PixelFormat.DontCare);
107 System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
108 System.Drawing.Image myThumbnail = xImage.GetThumbnailImage(ThumbnailWidth, ThumbnailHeight, myCallback, IntPtr.Zero);
109
110 myThumbnail.Save(filepath);
111 this .Label1.Text = ranNum;
112
113 myThumbnail.Dispose();
114 myBitmap.Dispose();
115 xBitmap.Dispose();
116 return fileArray;
117 }
118 }
119 catch (Exception ex)
120 {
121
122 return null ;
123 }
124 return null ;
125 }

上述为上传缩略图的方法,涉及到读取xml文件和存储地址,不是很完整,掉了个方法,补上:

 

  
    
private static bool ThumbnailCallback() { return false ; }

 

 

 

你可能感兴趣的:(缩略图)