0

I have some c# code that gets an image from a webpage then downloads it to my local machine. This is done in the background 1/sec. If I leave this running it works fine and my pictures get updated correctly. These pictures are basically feeds from a camera. I want to put these pictures into a picturebox or some other control so that I can display the images as if they were a camera feed. However when I tried doing this I've got errors saying the image is being used so I can not load it into my picturebox. Is there a better way to do this?

Thanks,

 byte[] lnBuffer;
                    byte[] lnFile;

                    HttpWebRequest lxRequest = (HttpWebRequest)WebRequest.Create(uri);

                    lxRequest.Credentials = credentials;
                    using (HttpWebResponse lxResponse = (HttpWebResponse)lxRequest.GetResponse())
                    {

                        using (BinaryReader lxBR = new BinaryReader(lxResponse.GetResponseStream()))
                        {
                            using (MemoryStream lxMS = new MemoryStream())
                            {
                                lnBuffer = lxBR.ReadBytes(1024);
                                while (lnBuffer.Length > 0)
                                {
                                    lxMS.Write(lnBuffer, 0, lnBuffer.Length);
                                    lnBuffer = lxBR.ReadBytes(1024);
                                }
                                lnFile = new byte[(int)lxMS.Length];
                                lxMS.Position = 0;
                                lxMS.Read(lnFile, 0, lnFile.Length);
                                lxMS.Close();
                                lxBR.Close();
                            }
                        }
                        lxResponse.Close();
                    }






                            using (System.IO.FileStream lxFS = new FileStream("images/camppic1.jpg", FileMode.Create))
                            {
                                lxFS.Write(lnFile, 0, lnFile.Length);
                                lxFS.Close();




                            }

This is what I use to create the file. Then in the same method after this code I do this:

image = Image.FromFile("C:\camppic1.jpg");

              pictureBox23.Image = image;
  • Your app could make a [clone](http://msdn.microsoft.com/en-us/library/system.drawing.image.clone.aspx) of the image it downloads and pass that to the picture box before saving to disk. Watch out for memory leaks with what you are doing though, Pictureboxes can be a pain for this – musefan May 17 '12 at 16:20
  • How would I go about making a clone. Or feeding the download picture straight to the picturebox? – Maxi Pereyra May 17 '12 at 16:37
  • Do you really want to store the image as a file? I would recommend to skip the file, both for faster data access, and possibly remove Your current problem... By the way, winforms or...? – erikH May 17 '12 at 18:49

1 Answers1

0

If you need the file, then load the file content and copy to a MemoryStream and use Image.FromStream. If you don't need the file, you could skip it and use the MemoryStream directly from the downloading... (Faster since no disc access would be needed.)

erikH
  • 2,286
  • 1
  • 17
  • 19
  • Keep a reference to the old Image when pictureBox.Image is updated, so the Image can be disposed when not used any more. – erikH May 17 '12 at 20:08