Shuxia Ning
2024-08-28 1a8a81785470302fc7fbd6914a9df5d1094dac2a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
namespace IStation.Win
{
    public static class ImageExtend
    {
        /// <summary>
        /// 将指定图片转化为指定尺寸,Image对象自带获取缩略图的方法GetThumbnailImage
        /// </summary>
        /// <param name="img"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        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;
        }
 
 
 
 
    }
}