【问题标题】:What's wrong with this code? Display Image Using WCF Rest Service这段代码有什么问题?使用 WCF Rest 服务显示图像
【发布时间】:2015-02-07 05:42:50
【问题描述】:

我想在(Asp.Net 的图像控制)客户端显示图像(.jpeg)。

我的休息服务代码如下,我正在从数据库中获取 ImagePath。

public Stream getImage(string width, string height)
{          
    int iRecordid = 1;           
    var q = from c in db.TblNames
            where c.RecordId == iRecordid
            select new { c.ImgName, c.ImgPath };

    foreach (var obj1 in q)
    {
        sImageName = obj1.ImgName;
        sImagePath = obj1.ImgPath;
    }           
    MemoryStream ms = new MemoryStream();

    ms.Position = 0;
    return ms;
}

我在客户端使用这个服务喜欢

 public void getImage()
 {  
     string url = @"http://localhost:50353/CustomerService.svc/getImage/100/200";

     var service = new ServiceReference(url);

     WebClient wc = new WebClient();

     byte[] res1 = wc.DownloadData(url);

     Stream res2 = new MemoryStream(res1);

     DataContractJsonSerializer res3 = new DataContractJsonSerializer(typeof(ImageMap));
     string ss = res3.ReadObject(res2).ToString();
     Image1.ImageUrl = ss;
 }

我收到类似的错误

反序列化 System.Web.UI.WebControls.ImageMap 类型的对象时出错。遇到了意外的字符“ÿ”。

【问题讨论】:

标签: c# asp.net wcf rest


【解决方案1】:

试试下面的休息服务端代码

    public string getImage(string width, string height)
        {

        int iRecordid = 1;

        var q = from c in db.MasterStructures 
                where c.RecordId == iRecordid
                select new { c.ImgName, c.ImgPath };

        foreach (var obj1 in q)
        {
            sImageName = obj1.ImgName;
            sImagePath = obj1.ImgPath;
        }           
        MemoryStream ms = new MemoryStream();

        ms.Position = 0;
        WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
        return sImagePath;

}

【讨论】:

    猜你喜欢
    • 2016-12-08
    • 2015-04-25
    • 2018-09-09
    相关资源
    最近更新 更多