首页新闻找找看学习计划

.net png 缩放 不了?

0
悬赏园豆:5 [已关闭问题] 关闭于 2012-01-03 10:51

代码如下

 

 1         public static Image FixedSize(Image imgPhoto, int Width, int Height)
2 {
3
4 int sourceWidth = imgPhoto.Width;
5 int sourceHeight = imgPhoto.Height;
6
7
8 int sourceX = 0;
9 int sourceY = 0;
10 int destX = 0;
11 int destY = 0;
12
13 float nPercent = 0;
14 float nPercentW = 0;
15 float nPercentH = 0;
16
17 nPercentW = ((float)sourceWidth / (float)Width); // 源图片宽度:画布宽度
18 nPercentH = ((float)sourceHeight / (float)Height);
19
20 int destWidth = 0; //缩放后的 宽度
21 int destHeight = 0;//缩放后的 高度
22
23 if (sourceHeight <= Height && sourceWidth <= Width)
24 {
25 destWidth = sourceWidth;
26 destHeight = sourceHeight;
27
28
29 }
30 else
31 {
32 //高度比较大
33 if (nPercentH > nPercentW)
34 {
35 nPercent = nPercentH;
36 }
37 else
38 {
39 nPercent = nPercentW;
40 }
41
42 destWidth = (int)((float)sourceWidth / nPercent);
43 destHeight = (int)((float)sourceHeight / nPercent);
44 }
45
46 destX = (int)(((float)Width - (float)destWidth) / 2); //图片x 坐标
47 destY = (int)(((float)Height - (float)destHeight) / 2);//图片y 坐标
48
49
50
51 Bitmap bmPhoto = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
52 bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
53
54 Graphics grPhoto = Graphics.FromImage(bmPhoto);
55 grPhoto.Clear(Color.White);
56 grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
57
58 grPhoto.DrawImage(imgPhoto,
59 new Rectangle(destX, destY, destWidth, destHeight),///它指定所绘制图像的位置和大小。
60 new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),///它指定 image 对象中要绘制的部分。
61 GraphicsUnit.Pixel);///它指定 srcRect 参数所用的度量单位。
62
63 grPhoto.Dispose();
64 return bmPhoto;
65 }
gdi
fun5的主页 fun5 | 初学一级 | 园豆:4
提问于:2011-12-05 11:04
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册