namespace IStation.Win
{
public static class ImageExtend
{
///
/// 将指定图片转化为指定尺寸,Image对象自带获取缩略图的方法GetThumbnailImage
///
///
///
///
///
public static Image GetCustomizedImage(this Image img, int width, int height)
{
var bp = new Bitmap(width, height);
//程序使用设备的DPI创建图片,所以代码注释
//using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
//{
// float dpiX = g.DpiX;
// float dpiY = g.DpiY;
// bp.SetResolution((int)dpiX, (int)dpiY);
//}
using (Graphics g = Graphics.FromImage(bp))
{
Rectangle resultRectangle = new Rectangle(0, 0, width, height);
Rectangle sourceRectangle = new Rectangle(0, 0, img.Width, img.Height);
g.DrawImage(img, resultRectangle, sourceRectangle, GraphicsUnit.Pixel);
}
return bp;
}
}
}