如何从 URL 获取图像到 pictureBox? (Windows 移动版)

发布于 2024-08-24 17:20:37 字数 890 浏览 1 评论 0原文

使用 Compact Framework 时从 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;

    }

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分开我的手 2024-08-31 17:20:37

我现在找到了一些效果更好的东西,但感谢史蒂夫丹纳的回答。
这是我的解决方案:

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

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;
    }
自在安然 2024-08-31 17:20:37

由于您有一个静态声明的长度为 4096 的缓冲区,因此当它到达缓冲区末尾时,此行:

while ((bytes = ms.Read(temp, 0, temp.Length)) != 0)

尝试读取 4096 字节,而实际上可能要少得多。将你的循环更改为类似这样的内容。

using (MemoryStream ms2 = new MemoryStream())
        {
            int bytes = 0;            
            while (true)
            {
               int byteLen = ms.Length - ms.Position >= 4096 ? 4096 : ms.Length -  ms.Position;
               byte[] temp = new byte[byteLen];
               bytes = ms.Read(temp, 0, byteLen);
               ms2.Write(temp, 0, bytes);
               imageFromUrl = new Bitmap(ms2);
               if (ms.Position == ms.Length) break;
        }

Since you have a statically declared buffer of length 4096, when it gets to the end of the buffer, this line:

while ((bytes = ms.Read(temp, 0, temp.Length)) != 0)

is trying to read in 4096 bytes, when there are probably substantially less. Change your loop to something like this.

using (MemoryStream ms2 = new MemoryStream())
        {
            int bytes = 0;            
            while (true)
            {
               int byteLen = ms.Length - ms.Position >= 4096 ? 4096 : ms.Length -  ms.Position;
               byte[] temp = new byte[byteLen];
               bytes = ms.Read(temp, 0, byteLen);
               ms2.Write(temp, 0, bytes);
               imageFromUrl = new Bitmap(ms2);
               if (ms.Position == ms.Length) break;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文