13

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 server).

The problem is that i get System.InvalidArgument exception at runtime.

private Image byteArrayToImage(byte[] byteArray) 
{
    if(byteArray != null) 
    {
        MemoryStream ms = new MemoryStream(byteArray);
        return Image.FromStream(ms, false, false); 
        /*last argument is supposed to turn Image data validation off*/
    }
    return null;
}

I have searched this problem in many forums and tried the suggestions given by many experts but nothing helped.

I dont think there is any problem with the byte array as such because When i feed the same byte array into my VC++ MFC client application, i get the image. But this doesn't somehow work in C#.NET.

Can anyone help me ?

P.S :

Other methods i've tried to accomplish the same task are:

1.

private Image byteArrayToImage(byte[] byteArray)
{
    if(byteArray != null) 
    {
        MemoryStream ms = new MemoryStream();
        ms.Write(byteArray, 0, byteArray.Length);
        ms.Position = 0; 
        return Image.FromStream(ms, false, false);
    }
    return null;
}

2.

private Image byteArrayToImage(byte[] byteArray) 
{
    if(byteArray != null) 
    {
        TypeConverter tc = TypeDescriptor.GetConverter(typeof(Bitmap));
        Bitmap b = (Bitmap)tc.ConvertFrom(byteArray);
        return b;
    }
    return null;
}

None of the above methods worked. Kindly help.

4

10 回答 10

10

Image.FromStream()需要一个只包含一个图像的流!

它将 stream.Position 重置为 0。我有一个包含多个图像或其他内容的流,您必须将图像数据读入字节数组并用它初始化MemoryStream

Image.FromStream(new MemoryStream(myImageByteArray));

只要图像在使用中,MemoryStream 就必须保持打开状态

我也以艰难的方式学会了这一点。

于 2013-10-09T10:02:08.133 回答
4

也许图像嵌入在 OLE 字段中,您必须考虑 88 字节 OLE 标头加上有效负载:

byteBlobData = (Byte[]) reader.GetValue(0);
stream = new MemoryStream(byteBlobData, 88, byteBlobData.Length - 88);
img = Image.FromStream(stream);
于 2010-02-02T15:47:44.180 回答
3

我猜从服务器接收文件时出了点问题。在尝试将其转换为Image? 您确定它与您提供给 C++ 应用程序的字节数组完全相同吗?

尝试将流保存到文件中,看看你得到了什么。你也许可以在那里发现一些线索。

您还可以添加断点并手动将字节数组中的某些字节与它们应该是的(如果您知道的话)进行比较。


编辑:看起来接收数据没有问题。问题在于它是原始格式(不是可以Image.FromStream理解的格式)。Bitmap(Int32, Int32, Int32, PixelFormat, IntPtr)构造函数在这里可能有用。或者,您可以创建空白位图并从原始数据手动对其进行 blt。

于 2009-03-24T03:55:02.343 回答
3

这样做时我遇到了这个问题:

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

最后两行是问题所在。我通过这样做来修复它:

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

然后这奏效了:

MemoryStream stream = new MemoryStream(bytes);
var newImage = System.Drawing.Image.FromStream(stream);
stream.Dispose();
于 2009-10-27T21:59:29.837 回答
1

System.InvalidArgument表示流没有有效的图像格式,即不支持的图像类型。

于 2009-03-24T03:47:05.267 回答
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!

于 2017-05-05T06:21:23.220 回答
0

我过去也遇到过同样的问题,它是由 Windows GDI 库中的泄漏引起的,这就是“位图”所使用的。但是,如果这种情况一直发生在您身上,那么它可能无关紧要。

于 2009-03-24T03:53:54.937 回答
0

此代码正在运行

        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();
于 2015-12-07T17:37:05.830 回答
0

尝试使用类似于此处描述的内容https://social.msdn.microsoft.com/Forums/vstudio/en-US/de9ee1c9-16d3-4422-a99f-e863041e4c1d/reading-raw-rgba-data-into-位图

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;
}
于 2018-01-05T17:56:48.310 回答
0

从数据库 byteArray 加载后,字节数多于一张图像。就我而言,它是82。

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

为了保存在数据库中,我插入 82 个字节开始流。Properties.Resources.ImgForDB- 它是包含那些 82 字节的二进制文件。(我得到它的下一条路径 - 将图像从 DB 加载到 MemoryStream 并保存到二进制文件的前 82 个字节。你可以在这里拿它 - 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();
于 2020-05-20T20:20:33.250 回答