ASP.NET(C#)图片上传压缩成缩略图的代码
2013年01月18日
⁄ 综合
⁄ 共 2172字 ⁄ 字号
小 中 大
-
-
-
-
-
- public static void MakeSmallImg(System.IO.Stream fromFileStream,string fileSaveUrl,System.Double templateWidth,System.Double templateHeight)
- {
-
- System.Drawing.Image myImage = System.Drawing.Image.FromStream(fromFileStream,true);
-
-
- System.Double newWidth = myImage.Width , newHeight = myImage.Height;
-
- if(myImage.Width>myImage.Height || myImage.Width==myImage.Height)
- {
- if(myImage.Width > templateWidth)
- {
-
- newWidth = templateWidth;
- newHeight = myImage.Height * (newWidth/myImage.Width);
- }
- }
-
- else
- {
- if(myImage.Height > templateHeight)
- {
-
- newHeight = templateHeight;
- newWidth = myImage.Width * (newHeight/myImage.Height);
- }
- }
-
-
- System.Drawing.Size mySize = new Size((int)newWidth,(int)newHeight);
-
- System.Drawing.Image bitmap = new System.Drawing.Bitmap(mySize.Width,mySize.Height);
-
- System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
-
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
-
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
-
- g.Clear(Color.White);
-
- g.DrawImage(myImage , new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height) ,
- new System.Drawing.Rectangle(0, 0, myImage.Width,myImage.Height) ,
- System.Drawing.GraphicsUnit.Pixel);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- bitmap.Save(fileSaveUrl , System.Drawing.Imaging.ImageFormat.Jpeg);
-
- g.Dispose();
- myImage.Dispose();
- bitmap.Dispose();
- }