Byte Array to Bitmap Image
Asked Answered
P

4

8

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

Peba answered 30/7, 2012 at 22:6 Comment(5)
@Tarek...is this C# Code? When I copy it into Visual Studio, it has some typos.Milano
yes its C# , but you have to type using System.IO; using System.Net.Sockets; using System.Net;Peba
And also using System.Drawing.Imaging;Peba
Maybe you are not being sent valid image data?Karmakarmadharaya
Parameter is not valid is given when the parameter is not a valid image. I assume there is some issue with the way you are reading the fileQuezada
C
9

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);
Cunnilingus answered 30/4, 2013 at 12:55 Comment(0)
M
1

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

See : Stream.Read Method's return value

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

PS: I am assuming lengths and reads are typos.

Mcburney answered 30/7, 2012 at 22:21 Comment(7)
this error happened 'Unable to read data from the transport connection. An operation on a socket could be performed because the system lacked sufficient buffer space or because a queue was full.'Peba
Check the size. It can be in network byte order. You can try IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data,0))Mcburney
not working, comes to 'data = new byte[size];' then gives me this error 'Arithmetic Operation Resulted in an overflow'Peba
@TarekAdel What do you expect me to do with so little info shown in your question? OK, I found an explicit bug in your code but I am not a magician. What kind of server/client do you have on the other side of your connection? in which protocol does it send the image?Mcburney
i cant send you the code cuz its more than 800 Chars , how can i send it to you?, if by Facebook, this is my email [email protected]Peba
@TarekAdel SO doesn't work that way. If you want an answer post the code here (or somewhere else and give a link) But please think: "Is this info enough to answer my question"Mcburney
i cant the code is more than 800 chars and i cant post except only 600 CharsPeba
C
0

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;
                    }
                }
Citrate answered 30/7, 2012 at 22:20 Comment(1)
Your assumption is probably wrong. see the code NetworkStreamMcburney
C
-1

Try this:

int size = BitConverter.ToInt32(data.Reverse().ToArray(),0); 
Cylinder answered 30/7, 2012 at 22:20 Comment(1)
Try this is just a comment. Try this. This should work because... is an answer.Mcburney

© 2022 - 2024 — McMap. All rights reserved.