Create webservice to retrieve image from database

A sample code to create webservice to retrieve image from database- The Webservice uses DataReader to read the data from the database table and returns byte array. - The client retrieves the data in form of byte array and saves it in the file with specified format . - The client code uses System.IO.MemoryStream , System.Drawing.Bitmap and System.Drawing.Imaging.ImageFormat namespace to save the image.[bold]Step 1: WebService (WSIMGjpeg.asmx)[/bold][code][WebMethod] ...

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 16
June 22, 2002
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement
A sample code to create webservice to retrieve image from database- The Webservice uses DataReader to read the data from the database table and returns byte array. - The client retrieves the data in form of byte array and saves it in the file with specified format . - The client code uses System.IO.MemoryStream , System.Drawing.Bitmap and System.Drawing.Imaging.ImageFormat namespace to save the image.[bold]Step 1: WebService (WSIMGjpeg.asmx)[/bold]


[WebMethod]

public 
byte [] showImage(Int32 empid

{

SqlConnection myconnection = new SqlConnection ("Server=localhost;uid=sa;password=;database=northwind;");

SqlCommand mycommand = new SqlCommand ("Select Employeeid, FirstName,Photo from Employees where employeeid =" empidmyconnection);

myconnection.Open ();

SqlDataReader drmycommand.ExecuteReader();

dr.Read();



byte[] imgbyte =(byte[]) dr["Photo"]; 


return 
imgbyte;

}

[bold]Step 2: Client[/bold]


private void Page_Load(object senderSystem.EventArgs e)

{

// Put user code to initialize the page here

localhost.WSIMGjpeg obj= new localhost.WSIMGjpeg();

byte[] data obj.showImage (2);

Int32 offset =78;

System.IO.MemoryStream mstream = new System.IO.MemoryStream ();

mstream.Write(dataoffsetdata.Length offset);


System.Drawing.Bitmap bmp= new System.Drawing.Bitmap(mstream);

bmp.Save(Server.MapPath ("empimg.jpeg"), System.Drawing.Imaging.ImageFormat.Jpeg );

mstream.Close();

// Drag and drop <asp:image> tag on webform

Image1.ImageUrl Server.MapPath("empimg.jpeg");

}

blog comments powered by Disqus
ASP.NET CODE ARTICLES

- How to Use the ListBox Control in ASP.NET 2.0
- How to Load XML Documents in ASP.NET 2.0
- DataGrid Code
- ASP.NET Guestbook
- User Controls and Client Side Scripting
- ASP.NET Programming with Microsoft's AS...
- ASP.NET Basics (part 3): Hard Choices
- ASP.NET Basics (part 2): Not My Type
- ASP.NET Basics (part 1): Nothing But .Net
- Directory Tree Browser
- How to get the confirmation of Yes/No from a...
- Complete example using custom errors and wri...
- Paging Certain # records per page .NET style
- General Methods of formatting and Subtractin...
- .NET LinkButton web control

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials