24

I'm having some difficulty saving a stream of bytes from an image (in this case, a jpg) to a System.IO.MemoryStream object. The goal is to save the System.Drawing.Image to a MemoryStream, and then use the MemoryStream to write the image to an array of bytes (I ultimately need to insert it into a database). However, inspecting the variable data after the MemoryStream is closed shows that all of the bytes are zero... I'm pretty stumped and not sure where I'm doing wrong...

using (Image image = Image.FromFile(filename))
{
    byte[] data;

    using (MemoryStream m = new MemoryStream())
    {
        image.Save(m, image.RawFormat);
        data = new byte[m.Length];
        m.Write(data, 0, data.Length);
    }

    // Inspecting data here shows the array to be filled with zeros...
}

Any insights will be much appreciated!

4 Answers 4

39

To load data from a stream into an array, you read, not write (and you would need to rewind). But, more simply in this case, ToArray():

using (MemoryStream m = new MemoryStream())
{
    image.Save(m, image.RawFormat);
    data = m.ToArray();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Cool, I've always misunderstood read and write concerning streams up until this point. Thanks!
This won't work for all image types. Such as tiff images fail on the .Save() call
4

If the purpose is to save the image bytes to a database, you could simply do:

byte[] imgData = System.IO.File.ReadAllBytes(@"path/to/image.extension");

And then plug in your database logic to save the bytes.

1 Comment

Thanks, I was actually just using a file on my local pc to isolate the problem that I was having. In the application I'm using, the image is actually coming in as an array of bytes. I'm trying to instantiate an Image class to do some processing before saving it to a database. Good to know about ReadAllBytes! Thanks!
0

I found for another reason this article some seconds ago, maybe you will find it useful: http://www.codeproject.com/KB/recipes/ImageConverter.aspx

Basically I don't understand why you try to write an empty array over a memory stream that has an image. Is that your way to clean the image?

If that's not the case, read what you have written in your memorystream with ToArray method and assign it to your byte array

And that's all

2 Comments

Thanks for the link. I'm actually trying to do some processing on an image uploaded on a website before saving it to a database. I guess the System.Drawing.Image namespace has some functionality for resizing, so I was using the Image class.
Yea definitely good idea. That's the right place where you should search. Care about performance, Image class can easily consume a lot of memory (that's what making me mad in my current project)
0

Try this way, it works for me

                MemoryStream ms = new MemoryStream();
                Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
                panel1.DrawToBitmap(bmp, panel1.Bounds);
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] Pic_arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(Pic_arr, 0, Pic_arr.Length);
                ms.Close();

Well instead of a Image control, I used a Panel Control.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.