这段代码有什么问题?使用 WCF Rest 服务显示图像
What's wrong with this code? Display Image Using WCF Rest Service
我想在客户端(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;
}
我在客户端使用这个服务 Like
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;
}
我收到类似
的错误
There was an error deserializing the object of type System.Web.UI.WebControls.ImageMap. Encountered unexpected character 'ÿ'.
为休息服务端尝试下面的代码
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;
}
我想在客户端(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;
}
我在客户端使用这个服务 Like
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;
}
我收到类似
的错误There was an error deserializing the object of type System.Web.UI.WebControls.ImageMap. Encountered unexpected character 'ÿ'.
为休息服务端尝试下面的代码
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;
}