在C#图像加载从Web

在C#图像加载从Web

本文介绍了在C#图像加载从Web URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在weburl的toolStripi c#中显示图片。使用以下方法获取图像

am trying to display an image in a "toolStrip" i c# from a weburl. Am using the following methode to get the image

WebRequest requestPic5 = WebRequest.Create(icon_path);
                         requestPic5.Timeout = 5000;
                         WebResponse responsePic5 = null;
                         Image Myimg5 = null;

if (requestPic5 != null)
   {
      responsePic5 = requestPic5.GetResponse();
      if (responsePic5 != null)
         {
            Myimg5 = Image.FromStream(responsePic5.GetResponseStream());
          }
   }



Myimg5 = Image.FromStream(responsePic5.GetResponseStream()); 抛出异常,但图像仍在URL

its failing when Myimg5 = Image.FromStream(responsePic5.GetResponseStream()); throws an exception but the image is still there in the url

但是不幸的是,时间它没有正确加载,有时会抛出404错误

But unfortunately most of the time its not loading properly and sometimes throwing a 404 error

推荐答案

响应流有其他的东西,你不一定需要。你真的只想要imgage文件的原始数据,所以你可以使用:

The response stream has other stuff that you don't necessarily want. You really just want the raw data of the imgage file, so you can use:

new MemoryStream(new WebClient ://address/file.ico));

这篇关于在C#图像加载从Web URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 11:00