简体   繁体   English

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

[英]How to resize image without losing quality

Hey I have this Image:嘿,我有这张图片:

i am using this method to resize my images:我正在使用这种方法来调整我的图像大小:

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

However when I'm done this would be my result:但是,当我完成后,这将是我的结果:

As You can see the picture is somehow dotty and poor quality.正如你所看到的,图片有点乱,质量很差。 So changed my approach and used this Method to resize my picture:所以改变了我的方法并使用这个方法来调整我的图片:

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

But The Issue still stands.但问题仍然存在。 i was wondering how may i be able to resize this image to smaller size without losing quality.我想知道如何才能在不损失质量的情况下将此图像调整为更小的尺寸。

I must add After resize i will create an byte array and save it within the database (Yes i know bad thing, but within this project it has to be saved within database).我必须添加调整大小后,我将创建一个字节数组并将其保存在数据库中(是的,我知道坏事,但在这个项目中,它必须保存在数据库中)。 Also on retrieval I get the image from webapi, so that byte array will be converted to base64 string.同样在检索时,我从 webapi 获取图像,以便将字节数组转换为 base64 字符串。 and i show that b64 on image tag like below.我在图像标签上显示 b64,如下所示。 eg:例如:

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

I'm using the following method for thousands of images and it never loses significant quality or results in a dotted image.我对数千张图像使用以下方法,它永远不会失去显着的质量或导致点状图像。

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

I've taken the liberty of scaling the image you posted using this code to 128px (Like the thumbnail you posted).我冒昧地将您使用此代码发布的图像缩放到 128 像素(如您发布的缩略图)。

Result:结果:

在此处输入图片说明

This method have been working great for me.这种方法对我很有用。

Just create a new Size with the Width and Height that you want and create a new Bitmap using the image that you want to scale.只需使用您想要的宽度和高度创建一个新的大小,并使用您想要缩放的图像创建一个新的位图。

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

    return new Bitmap(bm, newSize);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM