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

5
  • @Tarek...is this C# Code? When I copy it into Visual Studio, it has some typos. Commented Jul 30, 2012 at 22:11
  • yes its C# , but you have to type using System.IO; using System.Net.Sockets; using System.Net; Commented Jul 30, 2012 at 22:14
  • And also using System.Drawing.Imaging; Commented Jul 30, 2012 at 22:16
  • Maybe you are not being sent valid image data? Commented Jul 30, 2012 at 22:18
  • 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 file Commented Jul 30, 2012 at 22:20

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

Comments

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.

7 Comments

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.'
Check the size. It can be in network byte order. You can try IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data,0))
not working, comes to 'data = new byte[size];' then gives me this error 'Arithmetic Operation Resulted in an overflow'
@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?
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]
|
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;
                    }
                }

1 Comment

Your assumption is probably wrong. see the code NetworkStream
-1

Try this:

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

1 Comment

Try this is just a comment. Try this. This should work because... is an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.