SIGN UP MEMBER LOGIN:    
ARTICLE

Fetching Image Using WCF REST Service

Posted by Dhananjay Kumar Articles | WCF with C# March 18, 2010
This article will give a very simple and basic explanation of, how to fetch an image using WCF REST service.
Reader Level:

Objective 

This article will give a very simple and basic explanation of, how to fetch an image using WCF REST service. 

Step 1 

Create a WCF application.  To create a new application File -> New -> Web-> WCF Service Application.

1.gif

Step 2

Remove all the default code created by WCF. Remove code from IService 1 interface and Service1 class.  Remove the code from Web.Config also. Open Web.Config and remove System.Servicemodel codes. 

Step 3

Right click on Service1.svc  select View markup and add below code 

2.gif

<%@ ServiceHost Language="C#" Debug="true" Service="FetchingImageinBrowser.Service1" CodeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

Step 4

Create contract. Operation contract will return Stream. Stream is in the namespace System.IO. By putting WebGet attribute make operation contract  

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO ;

namespace FetchingImageinBrowser
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(UriTemplate = "GetImage", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
        Stream GetImage();
    }  
}

Step 5

Implement service. In service class write the below code.  Using FileStream  open and read the image. Then set outgoing response as image/jpeg using WebOperationContext class. 

Service1.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO ;

namespace FetchingImageinBrowser
{
    public class Service1 : IService1
    {
        public Stream GetImage()
        {
            FileStream fs = File.OpenRead(@"D:\a.jpg");
            WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
            return fs;
        }
    }
}

Step 6

Press F5 to run the application. Append GetImage/ in URL to get the output. 

3.gif

See the URL 

4.gif

Conclusion 

In this article, I discussed how to get an image from WCF REST service.  In next articles I will show complete Get and Set image using WCF Rest service.  Thanks for reading. 

Login to add your contents and source code to this article
share this article :
post comment
 

Cool article but what if I want to return a method that looks like:
[OperationContract]
List<MyObjects> getData();

where MyObjects look like:
class MyObjects
{
  string Name;
  string address;
  image picture;
}

I can imagine creating XML to do this including the encoding of the image but was wondering if there was a way to keep using the Stream idea

Posted by John Mcfetridge Sep 20, 2010

Hi i found many WCF articles submitted by you. It's wonderful. Currently i'm looking some help on WCF REST. The quest is how to publish private feeds using WCF REST (WebGet / WebInvoke). That too the requirement is pass the username and password with urls. Something like http://user:pass@service/GetNews?format=rss". I tried in many ways, but i didn't find right channel. First of all i confused in urltemplate itself. If you have any idea on this, please share with me... 
- Vijay, Singapore

Posted by V J Aug 18, 2010

I am using VS 2010, silverlight 4, ria services 4.

Recently I created plain WCF REST services (no RIA, no SOAP) with my endpoint (using wsHttpBinging):

<endpoint address="" behaviorConfiguration="wsBehavior" binding="wsHttpBinding" bindingConfiguration="wsbinding" contract="WcfService1.IService1"/>

<behaviors>
 <endpointBehaviors>
 <behavior name="wsBehavior">
  <webHttp/>
 </behavior>.........

 

I use this service from silverlight 4 client and everything works fine.

THEN, i created new project using "silverlight Business application" template which used RIA service. Now the web.config uses DomainServices and when i add wsHttpBind endpoint I doesnot work. I know i am not doing this correctly and i cant find any help online so far.

What I am trying to do is creat a RESTful WCF application with RIA (no SOAP) and that i can use from silverlight 4 client. For some reason i cannot get the service working.

Any help would be great.
Thanks

Posted by netlogging netlogging May 04, 2010

Very nice

Posted by Quader Khan May 04, 2010

Thank you :)

Posted by Dhananjay Kumar Mar 28, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor