生成高質量縮略圖

//方法1
public static Bitmap CreateThumbnail(Bitmap source, int thumbWi, int thumbHi, bool maintainAspect)
        {
            
// return the source image if it's smaller than the designated thumbnail
            if (source.Width < thumbWi && source.Height < thumbHi) return source;

            System.Drawing.Bitmap ret 
= null;
            
try
            {
                
int wi, hi;

                wi 
= thumbWi;
                hi 
= thumbHi;

                
if (maintainAspect)
                {
                    
// maintain the aspect ratio despite the thumbnail size parameters
                    if (source.Width > source.Height)
                    {
                        wi 
= thumbWi;
                        hi 
= (int)(source.Height * ((decimal)thumbWi / source.Width));
                    }
                    
else
                    {
                        hi 
= thumbHi;
                        wi 
= (int)(source.Width * ((decimal)thumbHi / source.Height));
                    }
                }

                
// original code that creates lousy thumbnails
                
// System.Drawing.Image ret = source.GetThumbnailImage(wi,hi,null,IntPtr.Zero);
                ret = new Bitmap(wi, hi);
                
using (Graphics g = Graphics.FromImage(ret))
                {
                    g.InterpolationMode 
= System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.FillRectangle(Brushes.White, 
00, wi, hi);
                    g.DrawImage(source, 
00, wi, hi);
                }
            }
            
catch
            {
                ret 
= null;
            }

            
return ret;
        }
//調用
 using (Graphics g = Graphics.FromImage(ret))
                {
                    g.InterpolationMode 
= System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.FillRectangle(Brushes.White, 
00, wi, hi);
                    g.DrawImage(source, 
00, wi, hi);
                }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章