Send Image as System.IO.Stream thru WCF REST Service and Consume it in ASP.NET Website

Let’s go thru the code:

WCF Service:

Service1.svc.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. using System.IO;  
  9. using System.Drawing;  
  10. using System.Drawing.Imaging;  
  11.   
  12. namespace PhotoService {  
  13.     public class Service1: IService1 {  
  14.         //This method will be called by the application to get Thumbnail  
  15.         public Stream GetThumbnail(string path) {  
  16.             try {  
  17.                 Image imgThumb = null;  
  18.                 Image img = Image.FromFile("/\\" + path); //Path for Image Eg. http://localhost:35798/RestServiceImpl.svc/GetThumbnail///YourServer//Images//SampleImage.jpg  
  19.                 imgThumb = CreateThumbnail(img, 100, 100);  
  20.                 var stream = ToStream(imgThumb, ImageFormat.Jpeg);  
  21.                 WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";  
  22.                 return stream;  
  23.             } catch (Exception ex) {  
  24.                 throw ex;  
  25.             }  
  26.         }  
  27.         //This method will generate thumbanail of 100 x 100   
  28.         private Image CreateThumbnail(Image image, int thumbWidth, int thumbHeight) {  
  29.             try {  
  30.                 return image.GetThumbnailImage(  
  31.                 thumbWidth,  
  32.                 thumbHeight,  
  33.                 delegate() {  
  34.                     return false;  
  35.                 },  
  36.                 IntPtr.Zero);  
  37.             } catch (Exception ex) {  
  38.                 throw ex;  
  39.             }  
  40.         }  
  41.         //Method to convert image in stream  
  42.         public Stream ToStream(Image image, ImageFormat formaw) {  
  43.             try {  
  44.                 var stream = new System.IO.MemoryStream();  
  45.                 image.Save(stream, formaw);  
  46.                 stream.Position = 0;  
  47.                 return stream;  
  48.             } catch (Exception ex) {  
  49.                 throw;  
  50.             }  
  51.         }  
  52.     }  
  53. }
IService1.cs
  1. using System;  
  2. using System.Runtime.Serialization;  
  3. using System.ServiceModel;  
  4. using System.ServiceModel.Web;  
  5. using System.IO;  
  6. using System.Drawing;  
  7. using System.Drawing.Imaging;  
  8.   
  9. namespace PhotoService {  
  10.     [ServiceContract]  
  11.     public interface IService1 {  
  12.         [OperationContract]  
  13.         [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetThumbnail/{*path}")]  
  14.         Stream GetThumbnail(string path);  
  15.     }  
  16. }
Run this service by this URL in browser

http://localhost:35798/RestServiceImpl.svc/GetThumbnail///YourServer//Images//SampleImage.jpg



Figure 1 Service has been created, now creating website to consume this service:

Website

Default.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Consume PhotoService.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html>  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.     <body>  
  9.         <form id="form1" runat="server">  
  10.             <div>  
  11.                 <asp:Image ID="Image1" runat="server" />  
  12.                 <br />  
  13. Enter Path of Image  
  14.   
  15.                 <asp:TextBox ID="ImagePath" runat="server"></asp:TextBox>  
  16.                 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ImagePath" ErrorMessage="Enter Image Path Eg. //Server//Images//SampleImage.jpg "></asp:RequiredFieldValidator>  
  17.                 <br />  
  18.                 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Get Image" />  
  19.                 <br />  
  20.             </div>  
  21.         </form>  
  22.     </body>  
  23. </html>
Default.aspx.cs
  1. using System;  
  2. using System.Linq;  
  3. using System.Web;  
  4. using System.Web.UI;  
  5. using System.Web.UI.WebControls;  
  6. using System.Runtime.Serialization;  
  7. using System.Net;  
  8. using System.IO;  
  9.   
  10. public partial class _Default: System.Web.UI.Page {  
  11.     protected void Page_Load(object sender, EventArgs e) {  
  12.         Image1.Visible = false;  
  13.     }  
  14.     protected void Button1_Click(object sender, EventArgs e) {  
  15.         try {  
  16.             string path = ImagePath.Text;  
  17.             path.Trim();  
  18.             if (path != null) {  
  19.                 WebClient proxy = new WebClient();  
  20.                 byte[] responsedata = proxy.DownloadData(new Uri("http://localhost:21269/Service1.svc/GetThumbnail/" + path)); //Accept path like: //YourServer//Images//SampleImage.jpg  
  21.                 Stream stream = new MemoryStream(responsedata);  
  22.                 System.Drawing.Image img = System.Drawing.Image.FromStream(stream);  
  23.                 img.Save(Server.MapPath("~/Images/Test.jpg"));  
  24.                 Image1.ImageUrl = "~/Images/Test.jpg";  
  25.                 Image1.Visible = true;  
  26.             } else {  
  27.                 Response.Write("Enter Image Path Eg.//Server//Images//SampleImage.jpg");  
  28.             }  
  29.         } catch (Exception ex) {  
  30.             Response.Write("Error : " + ex.Message.ToString());  
  31.         }  
  32.     }  
  33. }


Thanks !!

Ebook Download
View all
Learn
View all