如何将图片从URL转到PictureBox?(Windows Mobile) [英] How to get an image to a pictureBox from a URL? (Windows Mobile)

查看:70
本文介绍了如何将图片从URL转到PictureBox?(Windows Mobile)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用精简框架时,从URL获取图像的最佳方式是什么?

我发现的东西是这样的(用它做了一个函数):

    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;

    }

但它不会在PictureBox中显示任何图像。 有什么想法吗?

推荐答案

我现在找到了更好的解决方案,不过还是要感谢Steve Danner的答复。 以下是我的解决方案:

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;
    }

这篇关于如何将图片从URL转到PictureBox?(Windows Mobile)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆