0

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:

enter image description here

Or like this:

enter image description here

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.

jps
  • 20,041
  • 15
  • 75
  • 79
Redwing19
  • 75
  • 7

2 Answers2

0

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)

suriel
  • 191
  • 1
  • 10
  • Thanks for the help, however, I'm still not having any luck. I tried using the code from the top answer in that link but have the same result (image with weird squiggly lines covering bottom 90% of it) – Redwing19 Sep 27 '22 at 19:32
0

After struggling with this for some time, it turns out that my problem had nothing to do with the Drawing.Resize code...

My application allows users to upload images, and apparently when I upgraded it to .Net 6, I ran into a known bug with the HttpContext.Request.Body.ReadAsync, which could (and did) truncate the byte array for the file, resulting in a corrupted image

FYI, I found my answer here: Missing data when uploading/downloading image

After changing my upload file code to no longer use HttpContext.Request.Body.ReadAsync, it is once again resizing images like a charm.

Redwing19
  • 75
  • 7