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 阅读(158) 评论(0)  编辑 收藏 网摘 所属分类: ASP.NET常用代码

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-03-17 23:42 编辑过
Google站内搜索

China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!
开发者征途系统新作:《设计模式——基于C#的工程化实现及扩展》



相关文章:

相关链接: