【发布时间】:2010-09-10 09:45:48
【问题描述】:
如何在不影响图像质量的情况下调整图像大小?
【问题讨论】:
-
你能告诉我们更多细节吗?你的图片有多大,你需要多大?
-
imageresizing.net - 该库生成您可以使用 .NET 获得的最高质量图像
标签: c# image image-scaling
如何在不影响图像质量的情况下调整图像大小?
【问题讨论】:
标签: c# image image-scaling
正如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));
}
【讨论】:
除非您正在制作矢量图形,否则无法在不损失一些图像质量的情况下调整图像大小。
【讨论】:
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;
}
【讨论】:
我相信您要做的是“调整大小/重新采样”您的图像。这是一个很好的网站,提供说明并提供实用程序类(我也碰巧使用):
http://www.codeproject.com/KB/GDI-plus/imgresizoutperfgdiplus.aspx
【讨论】:
除非您调整大小,否则您无法对光栅图形执行此操作。
您可以通过良好的过滤和平滑来调整大小而不会丢失任何显着质量。
您还可以更改图像的 DPI 元数据(假设它有一些),这将保持完全相同的像素数,但会改变图像编辑器在“真实世界”测量中对其的看法。
只是为了涵盖所有基础,如果您真的只是指图像的文件大小而不是实际图像尺寸,我建议您查看图像数据的无损编码。我对此的建议是将图像重新保存为 .png 文件(我倾向于使用画图作为 Windows 中图像的免费转码器。在画图中加载图像,另存为新格式)
【讨论】:
您无法在不损失某些质量的情况下调整图像大小,这仅仅是因为您减少了像素数。
不要减小客户端的大小,因为浏览器不能很好地调整图像大小。
您可以做的是在渲染之前或在用户上传之前以编程方式更改大小。
这里有一篇文章解释了在 c# 中执行此操作的一种方法: http://www.codeproject.com/KB/GDI-plus/imageresize.aspx
【讨论】:
看看你是否喜欢image resizing quality of this open source ASP.NET module. 有一个现场演示,所以你可以自己玩弄。它产生的结果(对我而言)无法与 Photoshop 输出区分开来。它也有类似的文件大小 - MS 在他们的 JPEG 编码器上做得很好。
【讨论】:
在这里你也可以找到在这个类中添加水印代码:
public class ImageProcessor
{
public Bitmap Resize(Bitmap image, int newWidth, int newHeight, string message)
{
try
{
Bitmap newImage = new Bitmap(newWidth, Calculations(image.Width, image.Height, newWidth));
using (Graphics gr = Graphics.FromImage(newImage))
{
gr.SmoothingMode = SmoothingMode.AntiAlias;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(image, new Rectangle(0, 0, newImage.Width, newImage.Height));
var myBrush = new SolidBrush(Color.FromArgb(70, 205, 205, 205));
double diagonal = Math.Sqrt(newImage.Width * newImage.Width + newImage.Height * newImage.Height);
Rectangle containerBox = new Rectangle();
containerBox.X = (int)(diagonal / 10);
float messageLength = (float)(diagonal / message.Length * 1);
containerBox.Y = -(int)(messageLength / 1.6);
Font stringFont = new Font("verdana", messageLength);
StringFormat sf = new StringFormat();
float slope = (float)(Math.Atan2(newImage.Height, newImage.Width) * 180 / Math.PI);
gr.RotateTransform(slope);
gr.DrawString(message, stringFont, myBrush, containerBox, sf);
return newImage;
}
}
catch (Exception exc)
{
throw exc;
}
}
public int Calculations(decimal w1, decimal h1, int newWidth)
{
decimal height = 0;
decimal ratio = 0;
if (newWidth < w1)
{
ratio = w1 / newWidth;
height = h1 / ratio;
return height.To<int>();
}
if (w1 < newWidth)
{
ratio = newWidth / w1;
height = h1 * ratio;
return height.To<int>();
}
return height.To<int>();
}
}
【讨论】:
那里有一些东西,上下文感知调整大小,不知道你是否能够使用它,但它值得一看,这是肯定的
一个不错的视频演示(放大出现在中间) http://www.youtube.com/watch?v=vIFCV2spKtg
这里可能有一些代码。 http://www.semanticmetadata.net/2007/08/30/content-aware-image-resizing-gpl-implementation/
那是不是有点矫枉过正?也许有一些简单的过滤器可以应用于放大的图像以稍微模糊像素,您可以研究一下。
【讨论】:
您是放大还是缩小?是小百分比还是更大的因素,例如 2 倍、3 倍?您的应用程序的质量是什么意思?什么类型的图像——照片、硬边线图,还是什么?编写自己的低级像素研磨代码或尝试尽可能多地使用现有库(.net 或其他)?
关于这个主题有大量的知识。关键概念是插值。
浏览建议:
* 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
【讨论】:
这是一个forum thread,它提供了一个 C# 图像大小调整代码示例。您可以使用 GD library 绑定器之一在 C# 中进行重新采样。
【讨论】: