110,533
社区成员
发帖
与我相关
我的任务
分享
图片等比缩放,不会失真,设置picturebox控件的sizemode属性为CenterImage 居中显示
public static Image GetPicBySize(Image image, int width, int height)
{
int picWidth = image.Width;
int picHeight = image.Height;
if (picWidth < width && picHeight < width)
{
width = picWidth;
height = picHeight;
}
else if (picWidth / picHeight > width / height)
{
height = width * picHeight / picWidth;
}
else
{
width = height * picWidth / picHeight;
}
Bitmap thumb = new Bitmap(width, height);
using (Graphics g_thumb = Graphics.FromImage(thumb))
{
g_thumb.InterpolationMode = InterpolationMode.HighQualityBicubic;
g_thumb.DrawImage(image, 0, 0, width, height);
}
return (Image)thumb;
}