C# 字节数组到位图图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11730373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:59:22  来源:igfitidea点击:

Byte Array to Bitmap Image

c#arraysbitmapbyte

提问by Tarek Adel

I made this code to receive an image and convert it to bitmap image but it doesn't work.

我制作了这段代码来接收图像并将其转换为位图图像,但它不起作用。

Here is the code:

这是代码:

public void ReceiveImage()
{
    NetworkStream stream = new NetworkStream(socket);
    byte[] data = new byte[4];
    stream.read(data,0,data.length,0)
    int size = BitConverter.ToInt32(data,0);
    data = new byte[size];
    stream.read(data,0,data.length)
    MemoryStream imagestream = new MemoryStream(data);
    Bitmap bmp = new Bitmap(imagestream);
    picturebox1.Image = bmp;
}

It gets to:

它得到:

Bitmap bmp = new Bitmap(imagestream);

And gives me this error:

并给我这个错误:

Parameter is not valid

参数无效

回答by Serj-Tm

Try this:

尝试这个:

int size = BitConverter.ToInt32(data.Reverse().ToArray(),0); 

回答by ALI VOJDANIANARDAKANI

I assume you have a table and want to receive the picture from database.

我假设您有一张表并希望从数据库接收图片。

int cout = ds.Tables["TableName"].Rows.Count;
                if (cout > 0)
                {
                    if (ds.Tables["TableName"].Rows[cout - 1]["Image"] != DBNull.Value)
                    {
                        var data = (byte[])(ds.Tables["TableName"].Rows[cout - 1]["Image"]);
                        var stream = new MemoryStream(data);
                        pictureBox1.Image = Image.FromStream(stream);
                    }
                    else
                    {
                        pictureBox1.Image = null;
                    }
                }

回答by L.B

You are probably not receiving enough bytes in stream.read(data,0,data.length)since Readdoes not ensure that it will read data.lengthbytes. you have to check its return value and continue to read till data.Lengthbytes are read.

您可能没有收到足够的字节,stream.read(data,0,data.length)因为Read不能确保它会读取data.length字节。您必须检查其返回值并继续读取直到读取data.Length字节。

See : Stream.Read Method's return value

请参阅:Stream.Read 方法的返回值

int read = 0;
while (read != data.Length)
{
    read += stream.Read(data, read, data.Length - read);
}

PS: I am assuming lengths and reads are typos.

PS:我假设lengths 和reads 是错别字。

回答by morishuz

This is an alternative method

这是另一种方法

int w= 100;
int h = 200;
int ch = 3; //number of channels (ie. assuming 24 bit RGB in this case)

byte[] imageData    = new byte[w*h*ch]; //you image data here
Bitmap bitmap       = new Bitmap(w,h,PixelFormat.Format24bppRgb);
BitmapData bmData   = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
IntPtr pNative      = bmData.Scan0;
Marshal.Copy(imageData,0,pNative,w*h*ch);
bitmap.UnlockBits(bmData);