Image Processing in C #

I am loading a jpg image from the hard drive in bytes []. Is there a way to resize an image (reduce the resolution) without having to put it in a Bitmap object?

thanks

+4
source share
3 answers

There are always ways, but are they better ... JPG is a compressed image format, which means that to perform any image manipulation you need something to interpret this data. The bimap object will do this for you, but if you want to go a different route, you need to learn to understand the jpeg specification, create some kind of parser, etc. There may be shortcuts that you can use without having to make a complete intuition of the original jpg, but I think that would be a bad idea.

Oh, and don’t forget that there are different file formats for JPG (JFIF and EXIF) that you need to understand ...

I would think very hard before avoiding objects specifically designed for what you are trying to do.

+3
source

A .jpeg file is just a bag of bytes without a JPEG decoder. There is one built-in Bitmap class, it perfectly decodes .jpeg files. The result is a Bitmap object, you cannot get around this.

And it supports resizing through the Graphics class, as well as the Bitmap (Image, Size) constructor. But yes, when reducing the size of the .jpeg image, a larger file is often created. This is an unavoidable side effect of the Graphics.Interpolation mode. He tries to improve the appearance of the thumbnail by running pixels through a filter. The bicubic filter does an excellent job of this.

It looks great for the human eye, doesn't look so great for a JPEG encoder. The filter creates interpolated pixel colors, designed to prevent the complete disappearance of image details while reducing the size. However, these mixed pixel values ​​make image compression difficult, thereby creating a larger file.

You can play with Graphics.InterpolationMode and choose a lower quality filter. Creates a poorer image, but is easier to compress. I doubt that you will appreciate the result.

+1
source

That's what I'm doing.

And no, I don’t think you can resize the image without first processing it in memory (i.e. in some bitmap image).

Decent resizing involves the use of an interpolation / extrapolation algorithm; he can’t just “pick every n pixels,” unless you can negotiate with your closest neighbor.

Here are a few explanations: http://www.cambridgeincolour.com/tutorials/image-interpolation.htm

protected virtual byte[] Resize(byte[] data, int width, int height) { var inStream = new MemoryStream(data); var outStream = new MemoryStream(); var bmp = System.Drawing.Bitmap.FromStream(inStream); var th = bmp.GetThumbnailImage(width, height, null, IntPtr.Zero); th.Save(outStream, System.Drawing.Imaging.ImageFormat.Jpeg); return outStream.ToArray(); } 
+1
source

All Articles