title: How to return an image from a WCF service?
body: <p>I am trying to return an image from a WCF service.</p>
  
  <p>Code of IService1.cs:</p>
  
  <pre><code>[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
  public interface IService1
  {
      [OperationContract]
      [WebGet]
      [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest)]
      Stream GetImage(int width, int height);
  }
  </code></pre>
  
  <p>Code of service1.cs:</p>
  
  <pre><code>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 &lt; bitmap.Width; I++)
              {
                  for (int j = 0; j &lt; bitmap.Height; j++)
                  {
                      bitmap.SetPixel(I, j, (Math.Abs(I - j) &lt; 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;
          }
      }
  }
  </code></pre>
  
  <p>This is the web.config of my calling page:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;system.serviceModel&gt;
    &lt;bindings&gt;
      &lt;wsHttpBinding&gt;
        &lt;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"&gt;
  
          &lt;readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" /&gt;
          &lt;reliableSession ordered="true" inactivityTimeout="00:10:00"
                           enabled="false" /&gt;
          &lt;security mode="Message"&gt;
            &lt;transport clientCredentialType="Windows" proxyCredentialType="None"
                       realm="" /&gt;
            &lt;message clientCredentialType="Windows" negotiateServiceCredential="true"
                     algorithmSuite="Default" /&gt;
          &lt;/security&gt;
        &lt;/binding&gt;
      &lt;/wsHttpBinding&gt;
    &lt;/bindings&gt;
  
    &lt;client&gt;
      &lt;endpoint address="http://localhost:8732/Design_Time_Addresses/SecondService/Service1/"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
                contract="ServiceReference1.IService1" name="WSHttpBinding_IService1"&gt;
  
        &lt;identity&gt;
          &lt;dns value="localhost" /&gt;
        &lt;/identity&gt;
      &lt;/endpoint&gt;
    &lt;/client&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>When I call the service, I get the following error:</p>
  
  <blockquote>
    <p>The content type <code>image/jpeg</code> of the response message does not match the content type of the binding (<code>application/soap+xml; charset=utf-8</code>). If using a custom encoder, be sure that the <code>IsContentTypeSupported</code> method is implemented properly.</p>
  </blockquote>
  
  <p>I am new at WCF services, and would appreciate useful links for WCF beginners.</p>