Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以最小的质量损失调整大小_C#_Image - Fatal编程技术网

C# 以最小的质量损失调整大小

C# 以最小的质量损失调整大小,c#,image,C#,Image,我需要调整图像的大小,但图像质量不会受到影响,图像的大小将从10x10到1000x1000不等,它会有一些严重的拥塞和一些空闲时间 它必须上下缩放“可能会损失一些图像质量”,这是可以的,但它必须是最小的,所有的东西都带有光栅图形 请不要使用库或其他外部二进制文件这里有一个链接显示如何调整图像大小: 您需要的是使您的图像具有可伸缩性。我建议使用XML格式来存储图像,因为XML具有很强的可伸缩性。例如: <Image Width="640" Height="480"> <Pix

我需要调整图像的大小,但图像质量不会受到影响,图像的大小将从10x10到1000x1000不等,它会有一些严重的拥塞和一些空闲时间 它必须上下缩放“可能会损失一些图像质量”,这是可以的,但它必须是最小的,所有的东西都带有光栅图形
请不要使用库或其他外部二进制文件这里有一个链接显示如何调整图像大小:


您需要的是使您的图像具有可伸缩性。我建议使用XML格式来存储图像,因为XML具有很强的可伸缩性。例如:

<Image Width="640" Height="480">
  <Pixel X="0" Y="0">
    <Red>20</Red>
    <Green>80</Green>
    <Blue>0</Blue>
    <Alpha>255</Alpha>
  </Pixel>
  <Pixel X="1" Y="0">
    ...

20
80
0
255
...

如果您担心内存,我打赌这会压缩得非常好:-)。

每当您调整图像大小时,您都会有一些质量损失-这是无法避免的,但是您可以通过在图形上下文中使用最高质量的选项来帮助将其最小化

下面是我使用普通GDI+函数的用法:

public static Bitmap ResizeImage(Bitmap bmp, int width, int height, 
                                 InterpolationMode mode = InterpolationMode.HighQualityBicubic)                                         
{
    Bitmap bmpOut = null;

    try
    {                                
        decimal ratio;
        int newWidth = 0;
        int newHeight = 0;

        // If the image is smaller than a thumbnail just return original size
        if (bmp.Width < width && bmp.Height < height)
        {
            newWidth = bmp.Width;
            newHeight = bmp.Height;
        }
        else
        {
            if (bmp.Width == bmp.Height)
            {
                if (height > width)
                {
                    newHeight = height;
                    newWidth = height;
                }
                else
                {
                    newHeight = width;
                    newWidth = width;                         
                }
            }
            else if (bmp.Width >= bmp.Height)
            {
                ratio = (decimal) width/bmp.Width;
                newWidth = width;
                decimal lnTemp = bmp.Height*ratio;
                newHeight = (int) lnTemp;
            }
            else
            {
                ratio = (decimal) height/bmp.Height;
                newHeight = height;
                decimal lnTemp = bmp.Width*ratio;
                newWidth = (int) lnTemp;
            }
        }

        //bmpOut = new Bitmap(bmp, new Size( newWidth, newHeight));                     
        bmpOut = new Bitmap(newWidth, newHeight);                
        bmpOut.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);

        Graphics g = Graphics.FromImage(bmpOut);
        g.InterpolationMode = mode;

        g.SmoothingMode = SmoothingMode.HighQuality;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;

        g.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
        g.DrawImage(bmp, 0, 0, newWidth, newHeight);                
    }
    catch
    {
        return null;
    }

    return bmpOut;
}
一般来说,GDI+并不是高质量图像处理的最佳解决方案——它很不错,但如果您需要Web应用程序中的最高质量、更好的性能和线程安全性,则需要考虑其他选项


Bertrand LeRoy不久前有一篇关于图像大小调整的文章,提供了一些使用核心.NET框架的替代方案

你做错了。使用矢量图形格式,这不会是一个问题。如果没有库或其他外部二进制文件,可能无法实现所需的@OIDFORSTY复制(尤其是在这种规模下和任何一半有用的质量)。图像增强是一个巨大的研究领域。因为这是我的第一份工作之一,我很想尽可能多地学习,所以我想将“外部工作”保持在最低水平@Stefan Kendall我真的不知道“光栅图形的一切”是如何让你认为我实际上能够处理向量的?这是……讽刺吗?如果是这样,那就太棒了!如果没有,也许你需要一本词典…:-)@oidfrosty——这当然不是一个完整的解决方案,但是如果您像这样存储图像,您可以通过使用XSLT+XPath实现上述技术,这将比C#解决方案更具可读性和可扩展性。事实上,大多数图像处理任务(如裁剪、调整大小、级别等)都可以简化为XSLT。@NickAldwin-谈到XML,我可不会开玩笑。
public static bool ResizeImage(string filename, string outputFilename, 
                                int width, int height, 
                                InterpolationMode mode = InterpolationMode.HighQualityBicubic)
{

    using (var bmpOut = ResizeImage(filename, width, height, mode) )
    {
        var imageFormat = GetImageFormatFromFilename(filename);
        if (imageFormat == ImageFormat.Emf)
            imageFormat = bmpOut.RawFormat; 

        bmpOut.Save(outputFilename, imageFormat);
    }

    return true;
}