[英]Resize image in C# without losing quality
static Image ScaleImage(Image image, int Width, int Height)
{
int originalWidth = image.Width;
int originalHeight = image.Height;
double ratioX = (double)Width / (double)originalWidth;
double ratioY = (double)Height / (double)originalHeight;
double ratio = Math.Min(ratioX, ratioY);
int newHeight = (int)(originalHeight * ratio);
int newWidth = (int)(originalWidth * ratio);
Image scaledImage = new Bitmap(newWidth, newHeight);
Graphics graphic = Graphics.FromImage(scaledImage);
graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphic.CompositingQuality = CompositingQuality.HighQuality;
graphic.Clear(Color.Transparent);
graphic.DrawImage(image, 0, 0, newWidth, newHeight);
return scaledImage;
}
Image full = new Bitmap("img.png");
Image scaledImage = ScaleImage(full, full.Width / 2, full.Height / 2);
Clipboard.SetImage(scaledImage);
The problem is that, after resizing using this code, the image is kind of blurred. 问题在于,使用此代码调整大小后,图像有点模糊。
I want to copy to clipboard a version of original image, but scaled(2/3 times smaller) and if I past the image somewhere and manually resize this(to a bigger resolution), I want the same quality as original one. 我想将原始图像的一个版本复制到剪贴板,但是要缩放(缩小2/3倍),如果我将图像粘贴到某个地方并手动调整大小(以更高的分辨率),则我希望获得与原始图像相同的质量。
How can I do this? 我怎样才能做到这一点?
try this one code, it's almost like yours, but i use it in my own pictures viewer and dont see any "blur". 试试这个代码,它几乎就像您的代码,但是我在自己的图片查看器中使用它,却看不到任何“模糊”。
It dont resize each side, it put picture in square with MaxImageSizeToResize side and save proportions. 它不会调整每一侧的大小,而是将图片与MaxImageSizeToResize一侧放置在正方形中并保存比例。 May be you see blur because of not proportional resize, or lost some properties. 可能是由于未按比例调整大小而导致模糊或丢失了某些属性。
Try it and say - is result the same or not. 尝试说-结果是否相同。
public static System.Drawing.Bitmap Resize(string pathToOriginalFile, int MaxImageSizeToResize)
{
using (var image = System.Drawing.Image.FromFile(pathToOriginalFile))
{
return LocalGet(image);
}
System.Drawing.Bitmap LocalGet(System.Drawing.Image image)
{
int rW = 0;
int rH = 0;
double c = 0;
c = ((double)image.Height / (double)MaxImageSizeToResize);
rW = (int)(image.Width / c);
rH = MaxImageSizeToResize;
var destRect = new System.Drawing.Rectangle(0, 0, rW, rH);
var destImage = new System.Drawing.Bitmap(rW, rH);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = System.Drawing.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 System.Drawing.Imaging.ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.