C#high -quality image zoom method
It is not easy to adjust the image size when running in C#. Although has ,
and properties, the setting of these attributes does not adjust the image size. System.Drawing.Image
Size
To adjust the size of the image, you need to use the new size to create a new Width
object and draw the original image to the object. You can use the following code to implement: Height
However, this method will lead to a decline in image quality. To achieve high -quality zoom, the Bitmap
class is needed. How to operate the example below:
Bitmap objBitmap = new Bitmap(objImage, new Size(227, 171));
This function provides high -quality zoom in the following ways: Graphics
/// <summary> /// 将图像调整为指定宽度和高度。 /// </summary> /// <param name="image">要调整大小的图像。</param> /// <param name="width">要调整到的宽度。</param> /// <param name="height">要调整到的高度。</param> /// <returns>调整大小后的图像。</returns> public static Bitmap ResizeImage(Image image, int width, int height) { var destRect = new Rectangle(0, 0, width, height); var destImage = new Bitmap(width, height); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; using (var wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); } } return destImage; }
to prevent heavy shadows around the image boundary.
WrapMode
TileFlipXY
Use high -quality Resolution
and Compositing
Interpolation
Other considerations include the longitudinal ratio of the image (reserved for readers as exercises), and the trap where the image zoom in the image when saving the image. Smoothing
The above is the detailed content of How to Resize Images in C# for High Quality?. For more information, please follow other related articles on the PHP Chinese website!