产生高质量成比例缩略图

来源:互联网 发布:老子西出函谷关 知乎 编辑:程序博客网 时间:2024/05/16 04:50
 不多说了,大家看代码,生成的缩略图有超出范围的时候,但是在限制范围长和宽为1:1时肯定在范围之内.
private void Get_Image(string imgpath,string thetype,int limit_width,int limit_height) //图片路径,图片形式,范围宽,范围长
        {
            System.Drawing.Image originalImage 
= System.Drawing.Image.FromFile(Server.MapPath("temp")+"/"+imgpath);
            
int pic_width = originalImage.Width;
            
int pic_height = originalImage.Height;
            
int towidth = 0;
            
int toheight = 0;
            
if(pic_width<=limit_width&&pic_height<=limit_height)
            
{
                towidth 
= pic_width;
                toheight 
= pic_height;
            }

            
else
            
{
                
if(pic_width>pic_height)
                
{
                    
double temp = (double)pic_width/(double)limit_width;
                    towidth 
= limit_width;
                    toheight 
= (int)(pic_height/temp);
                }

                
else
                
{
                    
double temp = (double)pic_height/(double)limit_height;
                    towidth 
= (int)(pic_width/temp);
                    toheight 
= limit_height;
                }

            }

            
try
            
{
                System.Drawing.Bitmap bmp;
                
if(thetype == "full")
                
{
                    bmp 
= new System.Drawing.Bitmap(limit_width,limit_height);
                }

                
else
                
{
                    bmp 
= new System.Drawing.Bitmap(towidth,toheight);
                }

                Graphics g 
= System.Drawing.Graphics.FromImage(bmp);
                g.InterpolationMode 
= System.Drawing.Drawing2D.InterpolationMode.High;
                g.SmoothingMode 
= System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.Clear(Color.White);
                
if(thetype == "full")
                
{
                    
int temp = towidth - toheight;
                    
if(temp>0)
                    
{
                        g.DrawImage(originalImage,
new Rectangle(0,temp/2,towidth,toheight),0,0,pic_width,pic_height,GraphicsUnit.Pixel);
                    }

                    
else
                    
{
                        g.DrawImage(originalImage,
new Rectangle(-temp/2,0,towidth,toheight),0,0,pic_width,pic_height,GraphicsUnit.Pixel);
                    }

                }

                
else
                
{
                    g.DrawImage(originalImage,
new Rectangle(0,0,towidth,toheight),0,0,pic_width,pic_height,GraphicsUnit.Pixel);
                }

                System.IO.MemoryStream ms 
= new System.IO.MemoryStream();
                bmp.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
                Response.ClearContent();
                Response.ContentType 
= "image/Jpeg";
                Response.BinaryWrite(ms.ToArray());
                originalImage.Dispose();
                bmp.Dispose();
                g.Dispose();
            }

            
catch
            
{
                
            }

        }