[ACCEPTED]-Image.FromStream() method returns Invalid Argument exception-memorystream

Accepted answer
Score: 10

Image.FromStream() expects a stream that contains ONLY one 7 image!

It resets the stream.Position to 0. I've 6 you have a stream that contains multiple 5 images or other stuff, you have to read 4 your image data into a byte array and to 3 initialize a MemoryStream with that:

Image.FromStream(new MemoryStream(myImageByteArray));

The MemoryStream has to remain 2 open as long as the image is in use.

I've 1 learned that the hard way, too.

Score: 4

Maybe the image is embedded in an OLE field 2 and you have to consider the 88 bytes OLE 1 header plus payload:

byteBlobData = (Byte[]) reader.GetValue(0);
stream = new MemoryStream(byteBlobData, 88, byteBlobData.Length - 88);
img = Image.FromStream(stream);
Score: 3

I'm guessing that something is going wrong 16 when receiving the file from the server. Perhaps 15 you're only getting part of the file before 14 trying to convert it to an Image? Are you sure it's 13 the exact same byte array you're feeding 12 the C++ application?

Try saving the stream 11 to a file and see what you get. You might 10 be able to uncover some clues there.

You 9 can also add a breakpoint and manually compare 8 some of the bytes in the byte array to what 7 they're supposed to be (if you know that).


Edit: It 6 looks like there's nothing wrong with receiving 5 the data. The problem is that it's in raw 4 format (not a format that Image.FromStream understands). The 3 Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr) constructor may be of use here. Or, you can create 2 the blank bitmap and blt it manually from 1 the raw data.

Score: 3

I've had this problem when doing this:

MemoryStream stream = new MemoryStream();
screenshot.Save(stream, ImageFormat.Png);
byte[] bytes = new byte[stream.Length];
stream.Save(bytes, 0, steam.Length);

With 2 the last 2 lines being the problem. I fixed 1 it by doing this:

MemoryStream stream = new MemoryStream();
screenshot.Save(stream, ImageFormat.Png);
byte[] bytes = stream.ToArray();

And then this worked:

MemoryStream stream = new MemoryStream(bytes);
var newImage = System.Drawing.Image.FromStream(stream);
stream.Dispose();
Score: 1

System.InvalidArgument means The stream does not have a valid 2 image format, i.e. an image type that is 1 not supported.

Score: 1

Try this:

public Image byteArrayToImage(byte[] item)
{          
   Image img=Image.FromStream(new MemoryStream(item)); 
   img.Save(Response.OutputStream, ImageFormat.Gif);
   return img;
}

Hope it helps!

0

Score: 0

I've had the same problem in the past and 4 it was caused by a leak within the windows 3 GDI libraries, which is what 'Bitmap' uses. If 2 this happening all the time for you then 1 its probably unrelated, however.

Score: 0

this code is working

        string query="SELECT * from gym_member where Registration_No ='" + textBox9.Text + "'";

        command = new SqlCommand(query,con);
        ad = new SqlDataAdapter(command);
        DataTable dt = new DataTable();
        ad.Fill(dt);
        textBox1.Text = dt.Rows[0][1].ToString();
        textBox2.Text = dt.Rows[0][2].ToString();
        byte[] img = (byte[])dt.Rows[0][18];
        MemoryStream ms = new MemoryStream(img);

        pictureBox1.Image = Image.FromStream(ms);
        ms.Dispose();

0

Score: 0

Try to use something similar to what is 1 described here https://social.msdn.microsoft.com/Forums/vstudio/en-US/de9ee1c9-16d3-4422-a99f-e863041e4c1d/reading-raw-rgba-data-into-a-bitmap

Image ImageFromRawBgraArray(
    byte[] arr, 
    int charWidth, int charHeight,
    int widthInChars, 
    PixelFormat pixelFormat)
{
    var output = new Bitmap(width, height, pixelFormat);
    var rect = new Rectangle(0, 0, width, height);
    var bmpData = output.LockBits(rect, ImageLockMode.ReadWrite, output.PixelFormat);

    // Row-by-row copy
    var arrRowLength = width * Image.GetPixelFormatSize(output.PixelFormat) / 8;
    var ptr = bmpData.Scan0;
    for (var i = 0; i < height; i++)
    {
        Marshal.Copy(arr, i * arrRowLength, ptr, arrRowLength);
        ptr += bmpData.Stride;
    }

    output.UnlockBits(bmpData);
    return output;
}
Score: 0

After load from DataBase byteArray has more byte than one image. In my case it was 82.

MemoryStream ms = new MemoryStream();
ms.Write(byteArray, 82, byteArray.Length - 82);
Image image = Image.FromStream(ms);

And for save in the 5 DB I insert 82 byte to begin stream. Properties.Resources.ImgForDB - it 4 is binary file that contain those 82 byte. (I 3 get it next path - Load Image from DB to 2 MemoryStream and save to binary file first 1 82 byte. You can take it here - https://yadi.sk/d/bFVQk_tdEXUd-A)

        MemoryStream temp = new MemoryStream();
        MemoryStream ms = new MemoryStream();
        OleDbCommand cmd;
        if (path != "")
        {
            Image.FromFile(path).Save(temp, System.Drawing.Imaging.ImageFormat.Bmp);
            ms.Write(Properties.Resources.ImgForDB, 0, Properties.Resources.ImgForDB.Length);
            ms.Write(temp.ToArray(), 0, temp.ToArray().Length);

        cmd = new OleDbCommand("insert into Someone (first, Second, Third) values (@a,@b,@c)", connP);
        cmd.Parameters.AddWithValue("@a", fio);
        cmd.Parameters.AddWithValue("@b", post);
        cmd.Parameters.AddWithValue("@c", ms.ToArray());
        cmd.ExecuteNonQuery();

More Related questions