如何从 URL 获取图像到 pictureBox? (Windows 移动版)
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我现在找到了一些效果更好的东西,但感谢史蒂夫丹纳的回答。
这是我的解决方案:
I now found something that works better, but thanks for an answer Steve Danner.
Here is my solution:
由于您有一个静态声明的长度为 4096 的缓冲区,因此当它到达缓冲区末尾时,此行:
尝试读取 4096 字节,而实际上可能要少得多。将你的循环更改为类似这样的内容。
Since you have a statically declared buffer of length 4096, when it gets to the end of the buffer, this line:
is trying to read in 4096 bytes, when there are probably substantially less. Change your loop to something like this.