Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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#_.net_Image_Graphics - Fatal编程技术网

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

C# 如何在不损失质量的情况下调整图像大小,c#,.net,image,graphics,C#,.net,Image,Graphics,嘿,我有这张照片: 我正在使用此方法调整图像的大小: public static Bitmap ResizeImage(Image image, int width, int height) { var destRect = new Rectangle(0, 0, width, height); var destImage = new Bitmap(width, height); destImage.SetResolution(image.HorizontalReso

嘿,我有这张照片:

我正在使用此方法调整图像的大小:

public static Bitmap ResizeImage(Image image, int width, int height)
{
    var destRect = new Rectangle(0, 0, width, height);
    var destImage = new Bitmap(width, height);

    destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);

    using (var graphics = Graphics.FromImage(destImage))
    {
        graphics.CompositingMode = CompositingMode.SourceCopy;
        graphics.CompositingQuality = CompositingQuality.HighQuality;
        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = SmoothingMode.HighQuality;
        graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;

        using (var wrapMode = new ImageAttributes())
        {
            wrapMode.SetWrapMode(WrapMode.TileFlipXY);
            graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
        }
    }

    return destImage;
}
但是,当我完成时,这将是我的结果:

正如你所看到的,这幅画不知何故有些呆板,质量很差。因此,改变了我的方法,并使用此方法调整图片大小:

public static Image ResizeImage(Image OriginalImage, Size ThumbSize)
{
    Int32 thWidth = ThumbSize.Width;
    Int32 thHeight = ThumbSize.Height;
    Image i = OriginalImage;
    Int32 w = i.Width;
    Int32 h = i.Height;
    Int32 th = thWidth;
    Int32 tw = thWidth;
    if (h > w)
    {
        Double ratio = (Double)w / (Double)h;
        th = thHeight < h ? thHeight : h;
        tw = thWidth < w ? (Int32)(ratio * thWidth) : w;
    }
    else
    {
        Double ratio = (Double)h / (Double)w;
        th = thHeight < h ? (Int32)(ratio * thHeight) : h;
        tw = thWidth < w ? thWidth : w;
    }
    Bitmap target = new Bitmap(tw, th);
    Graphics g = Graphics.FromImage(target);
    g.SmoothingMode = SmoothingMode.HighQuality;
    g.CompositingQuality = CompositingQuality.HighQuality;
    g.InterpolationMode = InterpolationMode.High;
    Rectangle rect = new Rectangle(0, 0, tw, th);
    g.DrawImage(i, rect, 0, 0, w, h, GraphicsUnit.Pixel);
    return (Image)target;
}
公共静态图像大小图像(图像原始图像,大小拇指大小)
{
Int32 thWidth=拇指大小。宽度;
Int32 thHeight=拇指大小。高度;
图像i=原始图像;
Int32 w=i.宽度;
Int32 h=i.高度;
Int32 th=宽度;
Int32 tw=宽度;
如果(h>w)
{
双比值=(双)w/(双)h;
th=thHeight
但问题依然存在。我想知道我如何能够调整这个图像的大小,以较小的大小,而不失去质量

我必须添加调整大小后,我将创建一个字节数组,并保存在数据库中(是的,我知道不好的事情,但在这个项目中,它必须保存在数据库中)。同样在检索时,我从webapi获取图像,这样字节数组将转换为base64字符串。我在图像标签上显示b64,如下所示。e、 g:

<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
    9TXL0Y4OHwAAAABJRU5ErkJggg=="/>

我对成千上万的图像使用以下方法,它不会丢失显著的质量或导致虚线图像

public static Image ScaleImage(Image image, int height)
{
    double ratio = (double)height/ image.Height;
    int newWidth = (int)(image.Width * ratio);
    int newHeight = (int)(image.Height * ratio);
    Bitmap newImage = new Bitmap(newWidth, newHeight);
    using (Graphics g = Graphics.FromImage(newImage))
    {
        g.DrawImage(image, 0, 0, newWidth, newHeight);
    }
    image.Dispose();
    return newImage;
}
我已经利用这段代码将您发布的图像随意缩放到128px(就像您发布的缩略图一样)

结果:


这个方法对我很有效

只需创建一个具有所需宽度和高度的新尺寸,并使用要缩放的图像创建一个新位图

public Bitmap GetResizeImage(Bitmap bm, int newWidth, int newHeight)
{
    var newSize = new Size(newWidth, newHeight);

    return new Bitmap(bm, newSize);
}

图像看起来像矢量gfx,所以如果你得到了原始的矢量形式,就用你想要的分辨率渲染它。这将不会损失任何质量(除非使用太小的分辨率)。无论您做什么,任何光栅gfx大小调整器都将失去质量(除非使用原始分辨率的倍数)。您的图像中有一些噪声,在缩小尺寸之前对其进行平滑处理可能有助于改善效果。结果看起来像是在使用抖动。您是否也在将颜色比特宽度从
15/16/24/32位->8bit
?@Spektre不,我没有将其颜色比特宽度转换为8bit。。这里一定是有什么东西造成了这个问题,是因为我把它发送到了数据库还是在img标签上将图像显示为base64?检查自动取款机。这不是我喜欢的,但我想DBS引擎可以自行“优化”您的图像到8比特以保存space@Spektre好的,让我从这里跟踪其余部分。尝试调整最近邻过滤器的大小。。。如果即使在那里也有点,那么它很可能在某个地方抖动。另外,你确定目标图像像素格式与源图像匹配吗(不是C#用户)?嘿,伙计,我用了你的方法,和以前一样。。。我开始认为还有另一个问题。这可能是因为我使用base64字符串在img标签上显示我的图像吗?为什么这样做?重新缩放图像后,为什么不按原样显示呢?最终结果必须保存在数据库中。但我也在检查这一点,看看问题是否仍然存在,然后我可以从这一点跟踪其余问题。@ecKO好的,让我知道我是否可以提供任何帮助。@ecKO很乐意帮助!祝你有美好的一天。