C# 调整图像大小时出现质量问题?

C# 调整图像大小时出现质量问题?,c#,image,C#,Image,我正在使用此代码调整图像大小。但结果并不好,我要的是最好的质量。我知道它的质量很低,因为我也用photoshop调整了相同图像的大小,结果不一样,所以更好。我怎么修理它 private static Image resizeImage(Image imgToResize, Size size) { int sourceWidth = imgToResize.Width; int sourceHeight = imgToResize.Height;

我正在使用此代码调整图像大小。但结果并不好,我要的是最好的质量。我知道它的质量很低,因为我也用photoshop调整了相同图像的大小,结果不一样,所以更好。我怎么修理它

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;
    }
private static Image resizeImage(图像imgToResize,大小)
{
int sourceWidth=imgToResize.Width;
int sourceHeight=imgToResize.Height;
浮动百分比=0;
浮动nPercentW=0;
浮点数nPercentH=0;
nPercentW=((float)size.Width/(float)sourceWidth);
nPercentH=((浮点)size.Height/(浮点)sourceHeight);
如果(nPercentH
这是我使用的例行程序。也许你会发现它很有用。它是一种扩展方法,用于引导。唯一的区别是我省略了代码以保留纵横比,您可以很容易地插入它

public static Image GetImageHiQualityResized(this Image image, int width, int height)
{
    var thumb = new Bitmap(width, height);
    using (var g = Graphics.FromImage(thumb))
    {
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.InterpolationMode = InterpolationMode.High;
        g.DrawImage(image, new Rectangle(0, 0, thumb.Width, thumb.Height));
        return thumb;
    }
}
此扩展方法的示例用法可能包括:

// Load the original image
using(var original = Image.FromFile(@"C:\myimage.jpg"))
using(var thumb = image.GetImageHiQualityResized(120, 80))
{
    thumb.Save(@"C:\mythumb.png", ImageFormat.Png);
}
注释

  • 有关保存和加载图像的其他方法,请参见
默认的JPG编码和默认的PNG编码之间的区别确实非常不同。下面是使用示例的两个拇指,一个用
ImageFormat.Png
保存,另一个用
ImageFormat.Jpeg
保存

PNG图像

JPEG图像

如果您确定绝对必须使用JPEG,您可能会发现本问题中原始海报所做的工作很有帮助。它涉及将图像编解码器和编码参数配置为高质量设置


如果是我,我会尽快使用PNG格式,因为它是无损的。

我知道它只是在调整大小,但值得一提的是,你正在将javascript函数与1000美元的专业图形软件进行比较。这不是javascript函数,而是c语言,我知道有一种方法可以用c语言来实现,库会给你最好的质量,并且处理得当。查看后一个链接,了解提高质量的技巧-如果您想要一个免费的、开源的解决方案,可以为您处理所有的边缘案例,请查看前一个链接。@Berotmanya:有趣。我一直很满意这套动作的质量。如果你能发布一个图片的例子,与PhotoShop的结果相比,它的大小没有达到你满意的程度,这将是很有帮助的。另外,了解保存图像的格式也很有趣。如果您使用的是lossy.jpg格式,这可能是质量问题的根源。原图:2。Photoshop调整大小:3。编程调整大小:正如您所看到的,Photoshop调整大小的图像大小为17kb,而编程调整大小的图像大小为4kb。当然,质量显然很差(尤其是当你的客户是新娘房时)。@Berotmanya:Imageshack被我的工作挡住了。我到家后去看看。同时,更改保存代码,使其使用
ImageFormat.Png
。这将防止您因.JPG图像格式而丢失。如何将代码更改为uses ImageFormat.Png?有没有办法用现有的格式来解决这个问题?@Berotmanya:在检查了你的示例图片后,我给答案添加了更多内容。我想你会找到完整的答案。