asp.net(C#)放缩图像的代码(源自asp.netStartKit中classKit的作者)

    public static byte[] ResizeImageFile(byte[] imageFile, PhotoSize size)
        {
            
using (System.Drawing.Image original = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
            {
                
int targetH, targetW;
                
if (size == PhotoSize.Small || size == PhotoSize.Medium)
                {
                    
// regardless of orientation,
                    
// the *height* is constant for thumbnail images (UI constraint)

                    
if (original.Height > original.Width)
                    {
                        
if (size == PhotoSize.Small)
                            targetH 
= DefaultValues.FixedSmallImageHeight;
                        
else
                            targetH 
= DefaultValues.FixedMediumImageHeight;

                        targetW 
= (int)(original.Width * ((float)targetH / (float)original.Height));
                    }
                    
else
                    {
                        
if (size == PhotoSize.Small)
                            targetW 
= DefaultValues.FixedSmallImageWidth;
                        
else
                            targetW 
= DefaultValues.FixedMediumImageWidth;

                        targetH 
= (int)(original.Height * ((float)targetW / (float)original.Width));
                    }
                }
                
else
                {
                    
// for full preview, we scale proportionally according to orienation
                    if (original.Height > original.Width)
                    {
                        targetH 
= Math.Min(original.Height, DefaultValues.MaxFullImageSize);
                        targetW 
= (int)(original.Width * ((float)targetH / (float)original.Height));
                    }
                    
else
                    {
                        targetW 
= Math.Min(original.Width, DefaultValues.MaxFullImageSize);
                        targetH 
= (int)(original.Height * ((float)targetW / (float)original.Width));
                    }
                }

                
using (System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(new MemoryStream(imageFile)))
                {
                    
// Create a new blank canvas.  The resized image will be drawn on this canvas.
                    using (Bitmap bmPhoto = new Bitmap(targetW, targetH, PixelFormat.Format24bppRgb))
                    {
                        bmPhoto.SetResolution(
7272);

                        
using (Graphics grPhoto = Graphics.FromImage(bmPhoto))
                        {
                            grPhoto.SmoothingMode 
= SmoothingMode.AntiAlias;
                            grPhoto.InterpolationMode 
= InterpolationMode.HighQualityBicubic;
                            grPhoto.PixelOffsetMode 
= PixelOffsetMode.HighQuality;
                            grPhoto.DrawImage(imgPhoto, 
new Rectangle(00, targetW, targetH), 00, original.Width, original.Height, GraphicsUnit.Pixel);

                            MemoryStream mm 
= new MemoryStream();
                            bmPhoto.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
                            
return mm.GetBuffer();
                        }
                    }
                }
            }
        }
posted @ 2007-03-16 13:12  Otis.blogs  阅读(449)  评论(0编辑  收藏  举报