Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a client/server setup that when the client sends the message of screen/ the server takes a screenshot, converts it to a byte[] (array) and sends it to my client. Upon recieving the byte array the client converts it to a stream and back to an image file using Image.FromStream and displays it in a picturebox. When testing this the picturebox shows about and inch of the top of the picture while everything under it becomes a dark grey and then it gives me an error saying "Parameter is not valid". I would like for the picture box to show the entire image. My code is below.
C#
public void UpdateArray(byte[] array)  // update array of bytes
{
    MemoryStream ms = new MemoryStream(array);
    Image returnImage = Image.FromStream(ms);  // <-- Parameter Error comes from here.
    pictureBox1.Image = returnImage;
}

Thank you :)
Posted
Updated 15-Feb-10 5:11am
v3

The problem is with the data you load into the byte array. Show what you load into that and I'm sure we can help you. It should be the image data as it would be saved to a file (so, includes header information and such).
 
Share this answer
 
Thanks for the reply. The code I used to load the image into a byte array on the server side is shown below.
if (str == "screen/")
                {
                    int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
                    int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
                    Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
                    Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
                    gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
                    {
                        try
                        {
                            using (MemoryStream ms = new MemoryStream())
                            {
                                bmpScreenShot.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                                byte[] array = ms.ToArray();
                                server.Send(array);
                            }
                        }

If I use those same code snippets all in a single windows form without sending through a socket and to a client it works fine. So im assuming it has something to do with the transfer between server to client. Help is greatly appriciated. Thank you.
 
Share this answer
 
v2
Sounds like the problem is with your network code. More than likely, you are not getting all of the data during the transfer. This is pretty common. This would make it so only part of your image gets displayed, because only part of the data made it across the wire. Check the length of the array and that should confirm this (the array will be shorter than you expect).
 
Share this answer
 
Nevermind I figured it out. The file was too big I guess, instead I split it into several chunks and sent them individualy.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900