gpt4 book ai didi

c# - 如何从 URL 获取图像到 pictureBox? ( Windows Mobile )

转载 作者:太空狗 更新时间:2023-10-30 00:58:40 25 4
gpt4 key购买 nike

使用 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;

}

但它不会在 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;
}

关于c# - 如何从 URL 获取图像到 pictureBox? ( Windows Mobile ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2396288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com