在不损失任何质量的情况下调整图像大小

我需要调整图像大小,但图像质量不会受到影响。

正如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文件(我倾向于使用paint作为窗口中图像的自由代码转换器,将图像加载到paint中,保存为新格式)

您不能调整图像的大小,而不会损失一些质量,因为您正在减少像素数量。

不要减小客户端的大小,因为浏览器不能很好地调整图像的大小。

你可以做的是编程改变大小,然后再渲染它,或者当用户上传它。

这里有一篇文章解释了一种在c#中完成此操作的方法: http : //www.codeproject.com/KB/GDI-plus/imageresize.aspx

看看你是否喜欢这个开源ASP.NET模块的图像大小调整质量。 有一个现场演示,所以你可以自己搞砸了。 它产生的结果是(对我来说)不可能与Photoshop输出区分。 它也有相似的文件大小 – MS在他们的JPEG编码器上做得很好。

有东西在那里,情境感知调整大小,不知道你是否能够使用它,但值得一看,这是肯定的

一个不错的视频演示(放大显示在中间) 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

在这里你可以找到这个类中添加水印代码:

 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>(); } } 

这是一个提供C#图像调整大小代码示例的论坛主题 。 您可以使用其中一个GD库绑定器在C#中进行重新采样。