c#生成缩略图 图片固定大小 缩略图加空白填充

public void Upload()
{
                  if (fileid.PostedFile.FileName != "")
                   {
                       //容器高与宽
                       string backcolor = "#FFFFFF";
                       string borderColor = "#999999";
                       int desWidth = 200;
                       int desHeight = 300;
                       int penwidth = 0;
                       int penhight = 0;
                       int size = fileid.PostedFile.ContentLength;
                       if (fileid.PostedFile.ContentLength < 512000)
                       {
                           string filepath = fileid.PostedFile.FileName;
                           string name = System.IO.Path.GetFileName(filepath);
                           int num = filepath.LastIndexOf(".");
                           string filetype = filepath.Substring(num).ToUpper().Trim();
                           if (filetype == ".GIF" || filetype == ".PNG" || filetype == ".JPG" || filetype == ".JPEG" || filetype == ".BMP")
                           {
                               Byte[] filebyte = new byte[fileid.PostedFile.ContentLength];
                               System.IO.Stream ostream = this.fileid.PostedFile.InputStream;
                               System.Drawing.Image oimage = System.Drawing.Image.FromStream(ostream);
                               int owidth = oimage.Width;
                               int oheight = oimage.Height;
                               string hw = GetImageSize(owidth, oheight);
                               string[] aryhw = hw.Split(';');
                               int twidth = Convert.ToInt32(aryhw[0]);
                               int theight = Convert.ToInt32(aryhw[1]);               
                               //新建一个bmp图片                                         
                               System.Drawing.Bitmap timage = new System.Drawing.Bitmap(desWidth, desHeight);
                               //新建一个画板
                               System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(timage);                              
                               g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                               g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                               g.Clear(System.Drawing.ColorTranslator.FromHtml(backcolor));
                               if (twidth < desWidth & theight == desHeight)
                               {
                                   penwidth = desWidth - twidth;
                               }
                               else if (twidth == desWidth && theight < desHeight)
                               {
                                   penhight = desHeight - theight;
                               }
                               else if (twidth < desWidth && theight < desHeight)
                               {
                                   penwidth = desWidth - twidth;
                                   penhight = desHeight - theight;
                               }
                               int top = penhight / 2;
                               int left = penwidth / 2;
                               g.DrawImage(oimage, new System.Drawing.Rectangle(left, top, twidth, theight), new System.Drawing.Rectangle(0, 0, owidth, oheight), System.Drawing.GraphicsUnit.Pixel);
                               System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.ColorTranslator.FromHtml(borderColor));
                               g.DrawRectangle(pen,0,0,desWidth-2,desHeight-2);
                               //string pathifile = Server.HtmlEncode(Request.PhysicalApplicationPath) + "image\\" +"t"+ DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".jpg";
                               string pathifiles = Server.HtmlEncode(Request.PhysicalApplicationPath) + "image\\" + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + ".jpg";
                               try
                               {
                                   //原图保存
                                   // oimage.Save(pathifile,System.Drawing.Imaging.ImageFormat.Jpeg);
                                   //缩图图保存
                                   timage.Save(pathifiles, System.Drawing.Imaging.ImageFormat.Jpeg);
                               }
                               catch (Exception ex)
                               {
                                   throw ex;
                               }
                               finally
                               {
                                   g.Dispose();
                                   timage.Dispose();
                               }
                           }
}
 //获取缩略图的高与宽
           protected string GetImageSize(int LoadImgW, int LoadImgH)
           {
               int xh = 0;
               int xw = 0;
               //容器高与宽
               int oldW = 200;
               int oldH = 300;
               //图片的高宽与容器的相同
               if (LoadImgH == oldH && LoadImgW == (oldW))
               {//1.正常显示
                   xh = LoadImgH;
                   xw = LoadImgW;
               }
               if (LoadImgH == oldH && LoadImgW > (oldW))
               {//2、原高==容高,原宽>容宽 以原宽为基础
                   xw = (oldW);
                   xh = LoadImgH * xw / LoadImgW;
               }
               if (LoadImgH == oldH && LoadImgW < (oldW))
               {//3、原高==容高,原宽<容宽  正常显示   
                   xw = LoadImgW;
                   xh = LoadImgH;
               }
               if (LoadImgH > oldH && LoadImgW == (oldW))
               {//4、原高>容高,原宽==容宽 以原高为基础   
                   xh = oldH;
                   xw = LoadImgW * xh / LoadImgH;
               }
               if (LoadImgH > oldH && LoadImgW > (oldW))
               {//5、原高>容高,原宽>容宽           
                   if ((LoadImgH / oldH) > (LoadImgW / (oldW)))
                   {//原高大的多,以原高为基础
                       xh = oldH;
                       xw = LoadImgW * xh / LoadImgH;
                   }
                   else
                   {//以原宽为基础
                       xw = (oldW);
                       xh = LoadImgH * xw / LoadImgW;
                   }
               }
               if (LoadImgH > oldH && LoadImgW < (oldW))
               {//6、原高>容高,原宽<容宽 以原高为基础        
                   xh = oldH;
                   xw = LoadImgW * xh / LoadImgH;
               }
               if (LoadImgH < oldH && LoadImgW == (oldW))
               {//7、原高<容高,原宽=容宽 正常显示       
                   xh = LoadImgH;
                   xw = LoadImgW;
               }
               if (LoadImgH < oldH && LoadImgW > (oldW))
               {//8、原高<容高,原宽>容宽 以原宽为基础    
                   xw = (oldW);
                   xh = LoadImgH * xw / LoadImgW;
               }
               if (LoadImgH < oldH && LoadImgW < (oldW))
               {//9、原高<容高,原宽<容宽//正常显示    
                   xh = LoadImgH;
                   xw = LoadImgW;
               }
               return xw + ";" + xh;   
           }

转载于:https://www.cnblogs.com/lhstrong/archive/2009/09/25/1573816.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值