Image.FromStream() method returns Invalid Argument exception

前端 未结 10 2448
臣服心动
臣服心动 2020-11-29 09:18

I am capturing images from a smart camera imager and receiving the byte array from the camera through socket programming (.NET application is the client, camera is the serve

相关标签:
10条回答
  • 2020-11-29 09:58

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

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

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


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

    0 讨论(0)
  • 2020-11-29 09:59

    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 讨论(0)
  • 2020-11-29 10:01

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

    0 讨论(0)
  • 2020-11-29 10:02

    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 DB I insert 82 byte to begin stream. Properties.Resources.ImgForDB - it is binary file that contain those 82 byte. (I get it next path - Load Image from DB to MemoryStream and save to binary file first 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();
    
    0 讨论(0)
  • 2020-11-29 10:11

    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 the last 2 lines being the problem. I fixed 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();
    
    0 讨论(0)
  • 2020-11-29 10:11

    Try to use something similar to what is 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;
    }
    
    0 讨论(0)
提交回复
热议问题