论坛首页 编程语言技术论坛

.net2.0 生成缩略图,与指定比例不等自动补白边的方法。(原创)

浏览 7978 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-06-22  
c# 代码
    //int_Height int_Width 指定高度和指定宽度 input_Imgfile,out_ImgFile为原图片和缩小后图片的路径。
  1. public static void Thumbnail (int int_Width, int int_Height, string input_ImgFile, string out_ImgFile)   
  2.         {   
  3.   
  4.             System.Drawing.Image oldimage = System.Drawing.Image.FromFile(input_ImgFile);   
  5.             float New_Width; // 新的宽度   
  6.             float New_Height; // 新的高度   
  7.             float Old_Width,Old_Height; //原始高宽   
  8.             int flat = 0;//标记图片是不是等比   
  9.               
  10.   
  11.             int xPoint = 0;//若果要补白边的话,原图像所在的x,y坐标。   
  12.             int yPoint=0;   
  13.             //判断图片   
  14.   
  15.             Old_Width = (float)oldimage.Width;   
  16.             Old_Height = (float)oldimage.Height;   
  17.   
  18.             if ((Old_Width / Old_Height) > ((float)int_Width / (float)int_Height)) //当图片太宽的时候   
  19.             {   
  20.                 New_Height = Old_Height * ((float)int_Width / (float)Old_Width);   
  21.                 New_Width = (float)int_Width;   
  22.                  //此时x坐标不用修改   
  23.                 yPoint = (int)(((float)int_Height - New_Height) / 2);   
  24.                 flat = 1;   
  25.             }   
  26.             else if ((oldimage.Width / oldimage.Height) == ((float)int_Width / (float)int_Height))   
  27.             {   
  28.                 New_Width = int_Width;   
  29.                 New_Height = int_Height;   
  30.             }   
  31.             else  
  32.             {   
  33.                 New_Width = (int)oldimage.Width * ((float)int_Height / (float)oldimage.Height);  //太高的时候  
  34.                 New_Height = int_Height;   
  35.                 //此时y坐标不用修改   
  36.                 xPoint = (int)(((float)int_Width - New_Width) / 2);   
  37.                 flat = 1;   
  38.             }   
  39.                
  40.   
  41.                        // ===缩小图片===   
  42.             System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage((int)New_Width, (int)New_Height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);   
  43.             Bitmap bm = new Bitmap(thumbnailImage);   
  44.   
  45.                        if (flat != 0)   
  46.             {   
  47.                 Bitmap bmOutput = new Bitmap(int_Width,int_Height);   
  48.                 Graphics gc = Graphics.FromImage(bmOutput);   
  49.                 SolidBrush tbBg = new SolidBrush(Color.White);   
  50.                 gc.FillRectangle(tbBg,0, 0, int_Width, int_Height); //填充为白色   
  51.   
  52.  
  53.                 gc.DrawImage(bm,xPoint, yPoint, (int)New_Width, (int)New_Height);   
  54.                 bmOutput.Save(out_ImgFile);   
  55.             }   
  56.             else  
  57.             {   
  58.                 bm.Save(out_ImgFile);   
  59.             }   
  60.   
  61.         }  
   发表时间:2007-07-08  
嗯嗯~~ 关键还是要会用 GUI的画图方法
0 请登录后投票
   发表时间:2007-07-08  
呵呵,主要的关键代码就是先生成一个有背景颜色的bitmap,延伸一下,如果需要嵌上画框、加上水印、诸如此类的…………
0 请登录后投票
   发表时间:2007-07-13  
缩略图,居然还用GetThumbnailImage,唉
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics