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
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)
                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
                // 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];
                    ms1.Read(Ret, 0, (int)ms1.Length);
            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");
                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);

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

Question by:fin_comp
    LVL 9

    Expert Comment

    You cant close the MemoryStream, you have to keep the same open.

    Remove the following lines --


    Author Comment

    I removed the following lines, still no luck. Please help!!!
    LVL 9

    Accepted Solution

    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.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    This article shows how to setup the internet connectivity on Windows Mobile Emulator.   I assume that you already have Microsoft Visual Studio, Microsoft Windows Mobile SDK's and the emulator installed. The emulator is available with Visual Studi…
    Preface: This article is part of a series focused on cross platform mobile app development (specifically Android and iOS) using the Alloy framework and Titanium Studio made by Appcelerator ( This article presumes a wor…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now