- ali
- 等级:
- 性别:
- 文章: 39
- 积分: 110
- 来自: 广州
|
c# 代码
//int_Height int_Width 指定高度和指定宽度 input_Imgfile,out_ImgFile为原图片和缩小后图片的路径。
- public static void Thumbnail (int int_Width, int int_Height, string input_ImgFile, string out_ImgFile)
- {
-
- System.Drawing.Image oldimage = System.Drawing.Image.FromFile(input_ImgFile);
- float New_Width;
- float New_Height;
- float Old_Width,Old_Height;
- int flat = 0;
-
-
- int xPoint = 0;
- int yPoint=0;
-
-
- Old_Width = (float)oldimage.Width;
- Old_Height = (float)oldimage.Height;
-
- if ((Old_Width / Old_Height) > ((float)int_Width / (float)int_Height))
- {
- New_Height = Old_Height * ((float)int_Width / (float)Old_Width);
- New_Width = (float)int_Width;
-
- yPoint = (int)(((float)int_Height - New_Height) / 2);
- flat = 1;
- }
- else if ((oldimage.Width / oldimage.Height) == ((float)int_Width / (float)int_Height))
- {
- New_Width = int_Width;
- New_Height = int_Height;
- }
- else
- {
- New_Width = (int)oldimage.Width * ((float)int_Height / (float)oldimage.Height); //太高的时候
- New_Height = int_Height;
-
- xPoint = (int)(((float)int_Width - New_Width) / 2);
- flat = 1;
- }
-
-
-
- System.Drawing.Image thumbnailImage = oldimage.GetThumbnailImage((int)New_Width, (int)New_Height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
- Bitmap bm = new Bitmap(thumbnailImage);
-
- if (flat != 0)
- {
- Bitmap bmOutput = new Bitmap(int_Width,int_Height);
- Graphics gc = Graphics.FromImage(bmOutput);
- SolidBrush tbBg = new SolidBrush(Color.White);
- gc.FillRectangle(tbBg,0, 0, int_Width, int_Height); //填充为白色
-
-
- gc.DrawImage(bm,xPoint, yPoint, (int)New_Width, (int)New_Height);
- bmOutput.Save(out_ImgFile);
- }
- else
- {
- bm.Save(out_ImgFile);
- }
-
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- blu3leaf
- 等级: 初级会员
- 文章: 45
- 积分: 32
|
嗯嗯~~ 关键还是要会用 GUI的画图方法
|
返回顶楼 |
|
|
- SteveGY
- 等级:
- 性别:
- 文章: 80
- 积分: 189
|
呵呵,主要的关键代码就是先生成一个有背景颜色的bitmap,延伸一下,如果需要嵌上画框、加上水印、诸如此类的…………
|
返回顶楼 |
|
|
- jnet
- 等级: 初级会员
- 性别:
- 文章: 1
- 积分: 30
- 来自: 河外星系
|
缩略图,居然还用GetThumbnailImage,唉
|
返回顶楼 |
|
|