【问题标题】:get image from web service and show on Winforms从 Web 服务获取图像并在 Winforms 上显示
【发布时间】:2013-09-04 00:00:23
【问题描述】:

我正在开发一个使用 Web 服务作为后端的 C# Window 应用程序, 如何从指定的 URL 获取图像: 服务器上的图片为JPEG格式

var client = new RestClient();
client.BaseUrl = "http://www.*****.com/images/12345.jpg";
var request = new RestRequest();
IRestResponse response = client.Execute(request);

【问题讨论】:

标签: c# xml web-services rest restsharp


【解决方案1】:

如果客户端有图片 URL,为什么不直接使用 HTTP 下载呢?或者您是说图像将始终驻留在运行 WebService 的同一台服务器上,并且 WebService 方法应该接受 URL,将其转换为本地路径,并将图像作为字节数组返回?

我们有一个 WSDL WebService 的方法,它做同样的事情,我们不包括 URL 的协议和主机部分(它们是多余的。)

[WebMethod]
public byte[] GetPicture(string ImageURL)
{
    if (ImageURL.StartsWith("http"))
            return new byte[0]; 
    string tmp = System.Web.Hosting.HostingEnvironment.MapPath("/" + ImageURL);
    string FileName = Microsoft.JScript.GlobalObject.unescape(tmp);

    if (System.IO.File.Exists(FileName))
    {
        FileStream fs = System.IO.File.OpenRead(FileName);
        byte[] buf = new byte[fs.Length];
        fs.Read(buf, 0, (int)fs.Length);
        fs.Close();
        return buf;
    }
    else
        return new byte[0];
}

这能回答你的问题吗?

【讨论】:

    【解决方案2】:

    使用 RestSharp 我是这样做的

    var client = new RestClient();
    client.BaseUrl = "http://www.abcd.com/image1.jpg";
    var request = new RestRequest();
    picturebox1.Image = new Bitmap(new MemoryStream(client.DownloadData(request)));
    

    在图片框显示图像

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多