简体   繁体   中英

Resizing image with System.Drawing.Graphics.DrawImage produces image with lines or gray covering most of the result

I've had the same image resizing code in place for 7+ years and it worked like a charm while targeting old .net frameworks (with the most recent being 4.61). I've finally got around to attempting to upgrade the application to .net 6, and for some reason the image resize code no longer produces valid image.

No exception is thrown, but the image it produces is corrupt or something, as only the very top of the photo looks correct, with the rest of the image sort of overlaid with zig-zaggy lines.

It doesn't do this for all images either, so I can't make sense of what is happening. My code is pretty simple and is as follows:

var newImage = new Bitmap(newWidth, newHeight);
using (Graphics graphicsHandle = Graphics.FromImage(newImage))
{
     graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
     graphicsHandle.DrawImage(orgImage, 0, 0, newWidth, newHeight);
}

But the images look like this:

在此处输入图像描述

Or like this:

在此处输入图像描述

Literally, the only thing that's changed is targeting .net 6/core, but I don't know how to fix this. Any help would be greatly appreciated.

I'm not quite sure if it helps (and if it's still working in .net 6) but I wanted to scale an image once and I found this which I'm using since then without any trouble: How to resize an Image C#

(the very first answer when sorting by highest score is the one I'm using)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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