Search code examples
c#imagesocketsstreamgdi

out of memory exception in image.fromFile(Stream)


i searched in all similar problem but still couldn't solve the problem

this is a server code it is work successfully and the image File created successfully BUT if i cannot access the image file

image i = Image.FromStream(StreamObject);

NOTES: 1- the image not too large 2- the image have valid image format

I know the problem related to the stream ... how can i control this problem i want to retrive the saved image in the image object for some reason.. How can i keep the stream open for the lifetime of the image.??

static void Main(string[] args)
    {
        IPAddress ipAdress = IPAddress.Parse("192.160.1.8");
        // Initializes the Listener
        TcpListener tcpListener = new TcpListener(ipAdress, 8001);
        tcpListener.Start();
        int no;
        for (;;)
        {
            Socket socket = tcpListener.AcceptSocket();
            if (socket.Connected)
            {

                Stream os = File.OpenWrite("Target.jpg",);
                byte[] buffer = new byte[8000000];
                NetworkStream networkStream = new NetworkStream(socket);
                no = networkStream.Read(buffer, 0, 8000000);
                os.Write(buffer, 0, no);
                ///here the problem in the following line
                ///
                   Image i = Image.FromFile("Target.jpg");
                ///
                networkStream.Close();
                socket.Close();
                break;

            }
        }

    }

Solution

  • While bytes have been written to the buffer, the bytes may not have been flushed to the disk. Additionally, the code example provided keeps the file open for writing while it is getting read into an image. For things like streams, you should wrap the usage in using statements in order to avoid these types of memory errors.

    static void Main(string[] args)
    {
        IPAddress ipAdress = IPAddress.Parse("192.160.1.8");
        // Initializes the Listener
        TcpListener tcpListener = new TcpListener(ipAdress, 8001);
        tcpListener.Start();
        int no;
        for (;;)
        {
            Socket socket = tcpListener.AcceptSocket();
            if (socket.Connected)
            {
                byte[] buffer = new byte[8000000];
                using (Stream os = File.OpenWrite("Target.jpg")) 
                {
                    using (NetworkStream networkStream = new NetworkStream(socket)) 
                    {
                        no = networkStream.Read(buffer, 0, 8000000);
                        os.Write(buffer, 0, no);
                    }
                }
    
    
                ///here the problem in the following line
                ///
                   Image i = Image.FromFile("Target.jpg");
                ///
    
                socket.Close();
                break;
    
            }
        }
    
    }
    

    Alternatively, and probably more appropriately, you should consider creating your GDI+ image directly from the stream using Image.FromStream. The Image.FromStream method is documented here: https://msdn.microsoft.com/en-us/library/system.drawing.image.fromstream%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396