gpt4 book ai didi

C# WCF JSON WebService 返回 mime 类型图像/png

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:03 25 4
gpt4 key购买 nike

我正在使用 JSON 格式运行 WCF 网络服务,如下所示。我的问题是响应格式是 alowas Json 或 XML,但对于 GetImage,我想将图像作为 mime 类型的图像-png 返回。知道如何在 WCF 中执行此操作吗?提前致谢。

[ServiceContract]
public interface IEditor
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GetImage", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
byte[] GetImage();

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GetBounds", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
void GetBounds(out Rectangle bounds, out Point[] viewport);

最佳答案

  1. 使用 WebOperationContext.Current

  2. 返回

你的方法应该是这样的

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GetImage", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
public Stream GetImage()
{
var m = new MemoryStream();
//Fill m

// very important!!! otherwise the client will receive content-length:0
m.Position = 0;

WebOperationContext.Current.OutgoingResponse.ContentType = "image/png";
WebOperationContext.Current.OutgoingResponse.ContentLength = m.Length;
return m;
}

关于C# WCF JSON WebService 返回 mime 类型图像/png,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18714256/

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