Search code examples
c#compact-framework

How to get an image to a pictureBox from a URL? (Windows Mobile)


What and how is the best way to get an image from a URL when using the Compact Framework?

Something I found was this (made a function out of it):

    public Bitmap getImageFromUrl()
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(this.SImageUrl);
        request.Timeout = 5000; // 5 seconds in milliseconds
        request.ReadWriteTimeout = 20000; // allow up to 20 seconds to elapse
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream ms = response.GetResponseStream();
        Bitmap imageFromUrl;
        using (MemoryStream ms2 = new MemoryStream())
        {
            int bytes = 0;
            byte[] temp = new byte[4096];
            while ((bytes = ms.Read(temp, 0, temp.Length)) != 0)
                ms2.Write(temp, 0, bytes);
            imageFromUrl = new Bitmap(ms2);
        }

        return imageFromUrl;

    }

But it won't show any images in the pictureBox. Any ideas?


Solution

  • I now found something that works better, but thanks for an answer Steve Danner. Here is my solution:

    public Bitmap getImageFromURL(String sURL)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(sURL);
            myRequest.Method = "GET";
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myResponse.GetResponseStream());
            myResponse.Close();
    
            return bmp;
        }