Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Send Image as System.IO.Stream thru WCF REST Service and Consume it in ASP.NET Website
WhatsApp
Akash Nagina
9y
31.3
k
0
1
25
Blog
Let’s go thru the code:
WCF Service:
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;
using
System.Drawing;
using
System.Drawing.Imaging;
namespace
PhotoService {
public
class
Service1: IService1 {
//This method will be called by the application to get Thumbnail
public
Stream GetThumbnail(
string
path) {
try
{
Image imgThumb =
null
;
Image img = Image.FromFile(
"/\\"
+ path);
//Path for Image Eg. http://localhost:35798/RestServiceImpl.svc/GetThumbnail///YourServer//Images//SampleImage.jpg
imgThumb = CreateThumbnail(img, 100, 100);
var stream = ToStream(imgThumb, ImageFormat.Jpeg);
WebOperationContext.Current.OutgoingResponse.ContentType =
"image/jpeg"
;
return
stream;
}
catch
(Exception ex) {
throw
ex;
}
}
//This method will generate thumbanail of 100 x 100
private
Image CreateThumbnail(Image image,
int
thumbWidth,
int
thumbHeight) {
try
{
return
image.GetThumbnailImage(
thumbWidth,
thumbHeight,
delegate
() {
return
false
;
},
IntPtr.Zero);
}
catch
(Exception ex) {
throw
ex;
}
}
//Method to convert image in stream
public
Stream ToStream(Image image, ImageFormat formaw) {
try
{
var stream =
new
System.IO.MemoryStream();
image.Save(stream, formaw);
stream.Position = 0;
return
stream;
}
catch
(Exception ex) {
throw
;
}
}
}
}
IService1.cs
using
System;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.ServiceModel.Web;
using
System.IO;
using
System.Drawing;
using
System.Drawing.Imaging;
namespace
PhotoService {
[ServiceContract]
public
interface
IService1 {
[OperationContract]
[WebInvoke(Method =
"GET"
, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate =
"GetThumbnail/{*path}"
)]
Stream GetThumbnail(
string
path);
}
}
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
<
%@ Page
Language
=
"C#"
AutoEventWireup
=
"true"
CodeFile
=
"Consume PhotoService.aspx.cs"
Inherits
=
"_Default"
%
>
<!DOCTYPE html
>
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
runat
=
"server"
>
<
title
>
</
title
>
</
head
>
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
div
>
<
asp:Image
ID
=
"Image1"
runat
=
"server"
/>
<
br
/>
Enter Path of Image
<
asp:TextBox
ID
=
"ImagePath"
runat
=
"server"
>
</
asp:TextBox
>
<
asp:RequiredFieldValidator
ID
=
"RequiredFieldValidator1"
runat
=
"server"
ControlToValidate
=
"ImagePath"
ErrorMessage
=
"Enter Image Path Eg. //Server//Images//SampleImage.jpg "
>
</
asp:RequiredFieldValidator
>
<
br
/>
<
asp:Button
ID
=
"Button1"
runat
=
"server"
OnClick
=
"Button1_Click"
Text
=
"Get Image"
/>
<
br
/>
</
div
>
</
form
>
</
body
>
</
html
>
Default.aspx.cs
using
System;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Runtime.Serialization;
using
System.Net;
using
System.IO;
public
partial
class
_Default: System.Web.UI.Page {
protected
void
Page_Load(
object
sender, EventArgs e) {
Image1.Visible =
false
;
}
protected
void
Button1_Click(
object
sender, EventArgs e) {
try
{
string
path = ImagePath.Text;
path.Trim();
if
(path !=
null
) {
WebClient proxy =
new
WebClient();
byte
[] responsedata = proxy.DownloadData(
new
Uri(
"http://localhost:21269/Service1.svc/GetThumbnail/"
+ path)); //Accept path like: //YourServer//Images//SampleImage.jpg
Stream stream =
new
MemoryStream(responsedata);
System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
img.Save(Server.MapPath(
"~/Images/Test.jpg"
));
Image1.ImageUrl =
"~/Images/Test.jpg"
;
Image1.Visible =
true
;
}
else
{
Response.Write(
"Enter Image Path Eg.//Server//Images//SampleImage.jpg"
);
}
}
catch
(Exception ex) {
Response.Write(
"Error : "
+ ex.Message.ToString());
}
}
}
Thanks !!
Send Image as System.IO.Stream thru WCF REST Service and Consume it in ASP.NET Website
Up Next
How To Crop Image And Save Image In ASP.NET Using jQuery
Ebook Download
View all
Hands on ASP.NET GridView
Read by 16.9k people
Download Now!
Learn
View all
Membership not found