Blue Theme Orange Theme Green Theme Red Theme
 
DevExpress Free UI Controls
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » WCF » Fetching image using WCF REST service

Fetching image using WCF REST service

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

Author Rank :
Page Views : 9546
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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. 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Dhananjay Kumar
Dhananjay Kumar is a developer who blogs at http://debugmode.net/. He is Microsoft MVP ,Telerik MVP and Mindcracker MVP. You can follow him on twitter  @debug_mode
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
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.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
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.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
Its nice by jeyachandran On March 23, 2010
This article is very nice and useful
Reply | Email | Modify 
Re: Its nice by Dhananjay On March 28, 2010
Thank you :)
Reply | Email | Modify 
Nice one by Quader On May 4, 2010
Very nice
Reply | Email | Modify 
WCF service with RIA by netlogging On May 4, 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

Reply | Email | Modify 
Publish private feed using WCF Rest by V On August 18, 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

Reply | Email | Modify 
class with contained image by John On September 20, 2010
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
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.