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

Sir Crispalot
  • 4,792
  • 1
  • 39
  • 64
Tarek Adel
  • 161
  • 1
  • 6
  • 15

4 Answers4

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);
morishuz
  • 2,302
  • 4
  • 21
  • 21
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.

L.B
  • 114,136
  • 19
  • 178
  • 224
  • 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.' – Tarek Adel Jul 30 '12 at 23:01
  • Check the `size`. It can be in [network byte order](http://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/). You can try `IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data,0))` – L.B Jul 30 '12 at 23:06
  • not working, comes to 'data = new byte[size];' then gives me this error 'Arithmetic Operation Resulted in an overflow' – Tarek Adel Jul 30 '12 at 23:26
  • @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? – L.B Jul 30 '12 at 23:31
  • 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 tarek55544@live.com – Tarek Adel Jul 30 '12 at 23:36
  • @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*" – L.B Jul 30 '12 at 23:38
  • i cant the code is more than 800 chars and i cant post except only 600 Chars – Tarek Adel Jul 30 '12 at 23:39
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;
                    }
                }
Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47
-1

Try this:

int size = BitConverter.ToInt32(data.Reverse().ToArray(),0); 
Serj-Tm
  • 16,581
  • 4
  • 54
  • 61
  • 6
    `Try this` is just a comment. `Try this. This should work because...` is an answer. – L.B Jul 30 '12 at 22:48