| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
||||
|
||||
|
Create webservice to retrieve image from database
[WebMethod]
...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] public byte [] showImage(Int32 empid) { SqlConnection myconnection = new SqlConnection ("Server=localhost;uid=sa;password=;database=northwi nd;"); SqlCommand mycommand = new SqlCommand ("Select Employeeid, FirstName,Photo from Employees where employeeid =" + empid, myconnection); myconnection.Open (); SqlDataReader dr= mycommand.ExecuteReader(); dr.Read(); byte[] imgbyte =(byte[]) dr["Photo"]; return imgbyte; } [bold]Step 2: Client[/bold] private void Page_Load(object sender, System.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(data, offset, data.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"); } Read Complete Articles |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > Create webservice to retrieve image from database |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|