如何从wcf服务返回图像?

时间:2013-01-16 04:41:24

标签: c# asp.net wcf

这里我试图从wcf服务返回一个图像。 IService1.cs代码

 [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
 public interface IService1
 {

    [OperationContract]
    [WebGet]
    [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    Stream GetImage(int width, int height);

 }

服务代码1.cs

  namespace SecondService
  {
  public class Service1 : IService1
  {
    public Stream GetImage(int width, int height)
    {
        // Although this method returns a jpeg, it can be
        // modified to return any data you want within the stream
        Bitmap bitmap = new Bitmap(width, height);
        for (int i = 0; i < bitmap.Width; i++)
        {
            for (int j = 0; j < bitmap.Height; j++)
            {
         bitmap.SetPixel(i, j, (Math.Abs(i - j) < 2) ? Color.Blue :  color.Yellow);
            }
        }
        MemoryStream ms = new MemoryStream();
        bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        ms.Position = 0;
        System.ServiceModel.Channels.TcpTransportBindingElement transport = new  
        System.ServiceModel.Channels.TcpTransportBindingElement();
        transport.TransferMode = TransferMode.Streamed;
        WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
        return ms;
     }
    }
   }

这是我的调用页面的代码,web.config

  <system.serviceModel>
<bindings>

  <wsHttpBinding>
    <binding name="WSHttpBinding_IService1" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
      maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
      textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">


      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00"
        enabled="false" />
      <security mode="Message">
        <transport clientCredentialType="Windows" proxyCredentialType="None"
          realm="" />
        <message clientCredentialType="Windows" negotiateServiceCredential="true"
          algorithmSuite="Default" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

<client>
  <endpoint address="http://localhost:8732/Design_Time_Addresses/SecondService/Service1/"
    binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
    contract="ServiceReference1.IService1" name="WSHttpBinding_IService1">

    <identity>
      <dns value="localhost" />
    </identity>
  </endpoint>
</client>

当我致电该服务时,我面临以下错误 -     响应消息的内容类型image / jpeg与绑定的内容类型(application / soap + xml; charset = utf-8)不匹配。如果使用自定义编码器,请确保正确实现IsContentTypeSupported方法。

我是wcf服务的新手,我需要帮助,我在过去48小时内面对它,如果有人为wcf初学者提供有用的链接,我会很感激。谢谢

1 个答案:

答案 0 :(得分:0)

您无法返回流,但您必须使用以下代码将图像编码为字节数组:

 ImageSourceConverter oConverter = new ImageSourceConverter();
 BitmapSource oImageSource = (BitmapSource)oConverter.ConvertFromString(sResourceName); // You
 resource name

 Byte[] oResult; // This is you byte array

 using (MemoryStream oStream = new MemoryStream()) 
 {
    oEncoder.Frames.Add(BitmapFrame.Create(oImageSource));
    oEncoder.Save(oStream);

    oResult = new byte[oStream.Length];     
    oStream.Position = 0;

    oStream.Read(oResult, 0, (int)oStream.Length);  
    oStream.Close(); 
 }
相关问题