Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize image which is placed in byte[] array

Tags:

c#

.net

Size image placed in byte[] array (don't know the type of image). I have to produce another byte [] array, which size should be up to 50kB. How can I do some kind of scaling?

like image 877
Fishman Avatar asked Jan 09 '12 14:01

Fishman


People also ask

How is an image stored in a byte array?

Any image is merely a sequence of bytes structured in accordance with whatever underlying format used to represent it, eg color data, layers, dimensions, etc. The significance of any byte(s) you see during debugging is entirely dependent upon the native format of the image, eg PNG, TIFF, JPEG, BMP, etc.

Is an image a byte array?

Images are binary data - this is easily represented as byte arrays.


2 Answers

Unless you want to get into some serious math, you need to load your byte array into a memory stream, load an image from that memory stream, and use the built-in GDI functions in the System.Drawing namespace.

Doing a 25%, or 50% scale is easy. Beyond that, you need to start doing interpolation and differencing to make anything look halfway decent in binary data manipulation. You'll be several days into it before you can match what's already available in GDI.

System.IO.MemoryStream myMemStream = new System.IO.MemoryStream(myBytes);
System.Drawing.Image fullsizeImage = System.Drawing.Image.FromStream(myMemStream);
System.Drawing.Image newImage = fullsizeImage .GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
System.IO.MemoryStream myResult = new System.IO.MemoryStream();
newImage.Save(myResult ,System.Drawing.Imaging.ImageFormat.Gif);  //Or whatever format you want.
return  myResult.ToArray();  //Returns a new byte array.

BTW - if you really need to figure out your source image type, see: How to check if a byte array is a valid image

like image 189
Wesley Long Avatar answered Sep 23 '22 07:09

Wesley Long


Ok, so after some experiments, I have something like that:

public static byte[] Resize2Max50Kbytes(byte[] byteImageIn)
{
    byte[] currentByteImageArray = byteImageIn;
    double scale = 1f;

    if (!IsValidImage(byteImageIn))
    {
        return null;
    }

    MemoryStream inputMemoryStream = new MemoryStream(byteImageIn);
    Image fullsizeImage = Image.FromStream(inputMemoryStream);

    while (currentByteImageArray.Length > 50000)
    {
        Bitmap fullSizeBitmap = new Bitmap(fullsizeImage, new Size((int)(fullsizeImage.Width * scale), (int)(fullsizeImage.Height * scale)));
        MemoryStream resultStream = new MemoryStream();

        fullSizeBitmap.Save(resultStream, fullsizeImage.RawFormat);

        currentByteImageArray = resultStream.ToArray();
        resultStream.Dispose();
        resultStream.Close();

        scale -= 0.05f;
    }

    return currentByteImageArray;
}

Has someone another idea? Unfortunatelly Image.GetThumbnailImage() was causing very dirty images.

like image 24
Fishman Avatar answered Sep 24 '22 07:09

Fishman