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);
问题在于,使用此代码调整大小后,图像有点模糊。
我想将原始图像的一个版本复制到剪贴板,但是要缩放(缩小2/3倍),如果我将图像放在某个地方并手动调整其大小(以更高的分辨率),则我希望获得与原始图像相同的质量。
我该怎么做?
答案 0 :(得分:0)
尝试使用此代码,几乎就像您的代码一样,但是我在自己的图片查看器中使用了它,却看不到任何“模糊”。
它不会调整每一边的大小,而是将图片与MaxImageSizeToResize边成正方形放置并保存比例。可能是由于未按比例调整大小而导致模糊或丢失了某些属性。
尝试一下然后说-结果是否相同。
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;
}
}