大马资讯论坛 - 马来西亚中文资讯平台
标题:
C# Image自动调整图片大小,picture auto resize-教学
[打印本页]
作者:
资讯王
时间:
2012-4-28 11:27
标题:
C# Image自动调整图片大小,picture auto resize-教学
Image自动调整图片大小,picture auto resize。当你的图片超过一定的数值,你可以自动把他缩小,你的荧幕就看得到啦...
使用方式
Image carImage = new Bitmap(new System.IO.MemoryStream((byte[])row["carImage"]));
if (carImage.Height > 500 || carImage.Width > 500)
{
carImage = resizeImage(carImage, new Size(300, 300));
}
复制代码
添加以下的function
public static Image resizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}
复制代码
欢迎光临 大马资讯论坛 - 马来西亚中文资讯平台 (http://freeinfo.com.my/)
Powered by Discuz! X3.3