Resize from byte array - What am I doing wrong here?

May 14, 2013 at 2:09 PM
byte[] m_Bytes = ReadToEnd(fileStream); //converts fileStream to byte array - this works
WriteableBitmap bmp1 = new WriteableBitmap(166, 166);
bmp1.FromByteArray(m_Bytes);
WriteableBitmap resizedImage = bmp1.Resize(25, 25, WriteableBitmapExtensions.Interpolation.Bilinear);
byte[] buffer1 = resizedImage.ToByteArray(); //if I use bmp1 here, the code works fine
CurrentOrder.CompanyImage = buffer1; //saves an unreadable byte array in the DB
Coordinator
May 14, 2013 at 4:20 PM
The ToByteArray is a raw ARGB stream, so really big and can only be read via the FromByteArray method.
You should encode the image as JPEG, after the resize. Different platforms have different ways how to accomplish this.
May 14, 2013 at 5:04 PM
Now I am trying an encoder but when I save that image to SQL Server, it saves a purple splotchy image, even when I don't use resize.

However, I am still using ToByteArray (or ToArray) to save it to the database after encoding to JPEG. Is there something I can use instead to save the stream as a byte array into the database?
May 14, 2013 at 8:17 PM
This worked for me!!!! Instead of ToByteArray or ToArray:
    public static byte[] ReadBytesFromStream(MemoryStream stream1)
    {
        MemoryStream stream = new MemoryStream(stream1.ToArray());
        byte[] size = new byte[16 * 1024];
        using (MemoryStream memoryStream = new MemoryStream())
        {
            int readCount;
            while ((readCount = stream.Read(size, 0, size.Length)) > 0)
            {
                memoryStream.Write(size, 0, readCount);
            }
            return memoryStream.ToArray();
        }
    }