Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Images from Pocket PC to Desktop: An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.drawing.dll

Posted on 2006-05-19
5
Medium Priority
?
498 Views
Last Modified: 2013-11-18
I have a question. So far I can send text messages back and forth from my PDA (pocket PC 2003) to a remote pc. However, when it comes to images it fails. I am using sockets to enable connecting via IP addresses to perform this function. When I send an image, I get the following exception on the server side (remote pc).

An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in system.drawing.dll
Additional information: A generic error occurred in GDI+.

Below is my code:

On client side (PDA), I have:
//send the picture if user clicked send
private void cmdSend_Click(object sender, System.EventArgs e)
        {
            try
            {
                byte[] imgData = ConvertImageToByteArray();
                int size = imgData.Length;
                int totalSent =0, sent;
                int leftData = size;
                byte[] datasize = new byte[4];
                datasize = BitConverter.GetBytes(size);
                // sends size of image
                m_socClient.Send(datasize);
                // sends image data
                while (totalSent<size)
                {
                    sent = m_socClient.Send(imgData, totalSent, leftData, SocketFlags.None);
                    totalSent += sent;
                    leftData -= sent;
                }
            }
            catch(SocketException se)
            {
                MessageBox.Show (se.Message );
            }
       
        }

// converts image to byte array
        private byte[] ConvertImageToByteArray()
        {
            String filename = "\\My Documents\\testimg.bmp";
            Bitmap b = new Bitmap(filename);
            Stream ms1 = File.OpenRead(filename);
            byte[] Ret = new byte[(int)ms1.Length];
       
            try
            {
                    ms1.Read(Ret, 0, (int)ms1.Length);
            }
            catch(Exception)
            {
                throw;
            }
            return Ret;
        }



On the server side, I do this:
        public void RecieveVarData(Socket skt)
        {
            int recv;
            int totaldata = 0;

            //get the predefined datasize
            byte[] datasize = new byte[4];
            // get the sent image data size
            skt.Receive(datasize, 0, 4, SocketFlags.None);
            int size = BitConverter.ToInt32(datasize, 0);
            int leftdata = size;
            byte[] buffer = new byte[size];
         
             // get image data and put in buffer
            while (totaldata<size)
            {
                recv = skt.Receive(buffer, totaldata, leftdata, SocketFlags.None);
                if (recv == 0)
                {
                    buffer = Encoding.ASCII.GetBytes("exit");
                    break;
                }
                totaldata += recv;
                leftdata -= recv;
            }

            //write image buffer to stream ms, and get image from stream. flush and close stream when done
            MemoryStream ms = new MemoryStream(buffer, true);
            ms.Write(buffer, 0, buffer.Length);
               
            imageBox.Image = Image.FromStream(ms);
            ms.Flush();
            ms.Close();
        }


Debugger points to this statement as raising the exception:
imageBox.Image = Image.FromStream(ms);


Thanks.
0
Comment
Question by:fin_comp
  • 2
3 Comments
 
LVL 9

Expert Comment

by:checoo
ID: 16732792
You cant close the MemoryStream, you have to keep the same open.

Remove the following lines --

ms.Flush();
ms.Close();
0
 

Author Comment

by:fin_comp
ID: 16734518
I removed the following lines, still no luck. Please help!!!
0
 
LVL 9

Accepted Solution

by:
checoo earned 2000 total points
ID: 16761145
In the server code you have the line "buffer = Encoding.ASCII.GetBytes("exit");"

are you appending a delimiter "exit" at the end of the image stream on the client ?? If so then the same should be stripped on the server before you can build the image back.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When I was a beginner in Windows Mobile programming, even for small test I used to create a single MFC dialog-based application with one button, and put my test code in the OnLButtonDown method. For Pocket PC it worked fine, on some Windows CE d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question