C# 在不损失任何质量的情况下调整图像大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/87753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Resizing an Image without losing any quality
提问by public static
How can I resize an image, with the image quality unaffected?
如何在不影响图像质量的情况下调整图像大小?
回答by Randy
Unless you're doing vector graphics, there's no way to resize an image without potentially losing some image quality.
除非您正在制作矢量图形,否则无法在不损失某些图像质量的情况下调整图像大小。
回答by Kris Erickson
As rcarsays, you can't without losing some quality, the best you can do in c# is:
正如rcar所说,你不能不损失一些质量,你在 c# 中可以做的最好的是:
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
}
回答by AaronS
You can't resize an image without losing some quality, simply because you are reducing the number of pixels.
你不能在不损失一些质量的情况下调整图像大小,仅仅是因为你减少了像素数。
Don't reduce the size client side, because browsers don't do a good job of resizing images.
不要减小客户端的大小,因为浏览器不能很好地调整图像大小。
What you can do is programatically change the size before you render it, or as a user uploads it.
您可以做的是在渲染之前或在用户上传它时以编程方式更改大小。
Here is an article that explains one way to do this in c#: http://www.codeproject.com/KB/GDI-plus/imageresize.aspx
这是一篇解释在 c# 中执行此操作的方法的文章:http: //www.codeproject.com/KB/GDI-plus/imageresize.aspx
回答by workmad3
Unless you resize up, you cannot do this with raster graphics.
除非您调整大小,否则您无法使用光栅图形执行此操作。
What you can do with good filtering and smoothing is to resize without losing any noticablequality.
你能有良好的过滤和平滑要做的就是不会丢失任何调整大小noticable质量。
You can also alter the DPI metadata of the image (assuming it has some) which will keep exactly the same pixel count, but will alter how image editors think of it in 'real-world' measurements.
您还可以更改图像的 DPI 元数据(假设它有一些),这将保持完全相同的像素数,但会改变图像编辑器在“真实世界”测量中的看法。
And just to cover all bases, if you really meant just the file size of the image and not the actual image dimensions, I suggest you look at a lossless encoding of the image data. My suggestion for this would be to resave the image as a .png file (I tend to use paint as a free transcoder for images in windows. Load image in paint, save as in the new format)
并且只是为了涵盖所有基础,如果您的意思只是图像的文件大小而不是实际图像尺寸,我建议您查看图像数据的无损编码。我对此的建议是将图像重新保存为 .png 文件(我倾向于使用 Paint 作为 Windows 中图像的免费转码器。在 Paint 中加载图像,另存为新格式)
回答by Victor
There is something out there, context aware resizing, don't know if you will be able to use it, but it's worth looking at, that's for sure
有一些东西,上下文感知调整大小,不知道你是否能够使用它,但值得一看,这是肯定的
A nice video demo (Enlarging appears towards the middle) http://www.youtube.com/watch?v=vIFCV2spKtg
一个不错的视频演示(放大出现在中间) http://www.youtube.com/watch?v=vIFCV2spKtg
Here there could be some code. http://www.semanticmetadata.net/2007/08/30/content-aware-image-resizing-gpl-implementation/
这里可能有一些代码。 http://www.semanticmetadata.net/2007/08/30/content-aware-image-resizing-gpl-implementation/
Was that overkill? Maybe there are some easy filters you can apply to an enlarged image to blur the pixels a bit, you could look into that.
这是矫枉过正吗?也许有一些简单的过滤器可以应用于放大的图像以稍微模糊像素,您可以研究一下。
回答by DarenW
Are you resizing larger, or smaller? By a small % or by a larger factor like 2x, 3x? What do you mean by quality for your application? And what type of images - photographs, hard-edged line drawings, or what? Writing your own low-level pixel grinding code or trying to do it as much as possible with existing libraries (.net or whatever)?
您调整大小是更大还是更小?是小百分比还是更大的因素,如 2x、3x?您所说的应用质量是什么意思?什么类型的图像 - 照片,硬边线条图,或者什么?编写自己的低级像素打磨代码还是尝试使用现有库(.net 或其他)尽可能多地完成?
There is a large body of knowledge on this topic. The key concept is interpolation.
关于这个主题有大量的知识。关键概念是插值。
Browsing recommendations:
* http://www.all-in-one.ee/~dersch/interpolator/interpolator.html
* http://www.cambridgeincolour.com/tutorials/image-interpolation.htm
* for C#: https://secure.codeproject.com/KB/GDI-plus/imageprocessing4.aspx?display=PrintAll&fid=3657&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26&select=629945* this is java-specific but might be educational - http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
浏览建议:
* http://www.all-in-one.ee/~dersch/interpolator/interpolator.html
* http://www.cambridgeincolour.com/tutorials/image-interpolation.htm
* 对于 C#:https: //secure.codeproject.com/KB/GDI-plus/imageprocessing4.aspx?display=PrintAll&fid=3657&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26&select=629945*这是Java特定的,但可能具有教育意义 - http: //today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html
回答by jimg
Here is a forum threadthat provides a C# image resizing code sample. You could use one of the GD librarybinders to do resampling in C#.
回答by jimg
See if you like the image resizing quality of this open source ASP.NET module.There's a live demo, so you can mess around with it yourself. It yields results that are (to me) impossible to distinguish from Photoshop output. It also has similar file sizes - MS did a good job on their JPEG encoder.
看看您是否喜欢这个开源 ASP.NET 模块的图像大小调整质量。有一个现场演示,所以你可以自己弄乱它。它产生的结果(对我来说)无法与 Photoshop 输出区分开来。它也有类似的文件大小——微软在他们的 JPEG 编码器上做得很好。
回答by Ashlocke
I believe what you're looking to do is "Resize/Resample" your images. Here is a good site that gives instructions and provides a utility class(That I also happen to use):
我相信您要做的是“调整大小/重新采样”您的图像。这是一个提供说明并提供实用程序类的好站点(我也碰巧使用):
http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx
http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx
回答by ozba
private static Image resizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width / (float)sourceWidth);
nPercentH = ((float)size.Height / (float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}