|
1 | 1 | using System.Drawing; |
2 | 2 | using System.Drawing.Drawing2D; |
| 3 | +using System.Drawing.Imaging; |
3 | 4 |
|
4 | 5 | namespace WindowTextExtractor.Utils |
5 | 6 | { |
6 | 7 | static class ImageUtils |
7 | 8 | { |
8 | | - public static Bitmap Reduce(Bitmap source, int width, int height) |
| 9 | + /// <summary> |
| 10 | + /// Resize the image to the specified width and height. |
| 11 | + /// </summary> |
| 12 | + /// <param name="image">The image to resize.</param> |
| 13 | + /// <param name="width">The width to resize to.</param> |
| 14 | + /// <param name="height">The height to resize to.</param> |
| 15 | + /// <returns>The resized image.</returns> |
| 16 | + public static Bitmap ResizeImage(Image image, int width, int height) |
9 | 17 | { |
10 | | - var bitmap = new Bitmap(width, height); |
11 | | - using (var graphics = Graphics.FromImage(bitmap)) |
| 18 | + var destRect = new Rectangle(0, 0, width, height); |
| 19 | + var destImage = new Bitmap(width, height); |
| 20 | + |
| 21 | + destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); |
| 22 | + |
| 23 | + using (var graphics = Graphics.FromImage(destImage)) |
12 | 24 | { |
| 25 | + graphics.CompositingMode = CompositingMode.SourceCopy; |
| 26 | + graphics.CompositingQuality = CompositingQuality.HighQuality; |
13 | 27 | graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; |
14 | | - graphics.DrawImage(source, new Rectangle(0, 0, width, height), new Rectangle(0, 0, source.Width, source.Height), GraphicsUnit.Pixel); |
| 28 | + graphics.SmoothingMode = SmoothingMode.HighQuality; |
| 29 | + graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; |
| 30 | + |
| 31 | + using (var wrapMode = new ImageAttributes()) |
| 32 | + { |
| 33 | + wrapMode.SetWrapMode(WrapMode.TileFlipXY); |
| 34 | + graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); |
| 35 | + } |
15 | 36 | } |
16 | | - return bitmap; |
| 37 | + |
| 38 | + return destImage; |
17 | 39 | } |
18 | 40 | } |
19 | 41 | } |
0 commit comments