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]
...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=northwind;");
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");
}
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More ASP.NET Code Articles More By Sushila Bowalekar developerWorks - FREE Tools! | You probably have thousands of lines of COBOL code loaded with business intelligence and being used to run your business, along with an army of developers maintaining these applications. Learn how to prepare your applications and developers so you can keep that competitive edge and move to a service-oriented architecture with the IBM Rational Enterprise Modernization solutions. Replay is available for 9 months. FREE! Go There Now!
| | | | Visit IBM developerWorks to download a free trial version of Lotus Quickr 8.0, which enables collaboration by transforming the way everyday business content such as documents, rich media, photos, and video can be shared. Lotus Quickr makes it faster and easier to share content of all types (not just documents) within virtual teams. It is designed to make it easier to collaborate across organizational boundaries, while continuing to work within the context of familiar desktop applications. FREE! Go There Now!
| | | | Download a free trial version of IBM Rational Developer for System z, software that can help you deliver core development capabilities; the power of Java Platform, Enterprise Edition (Java EE); and rapid application development support to diverse enterprise application development teams. With comprehensive development tools to help create, deploy and maintain traditional enterprise and composite applications, Rational Developer for System z enables developers with different technical backgrounds to easily participate in important technology projects. FREE! Go There Now!
| | | | Download a free trial version of IBM Rational Software Analyzer Developer Edition V7.0 to identify bug defects earlier in the software development cycle. Rational Software Analyzer is an extensible software development solution that reduces the expense of bug-fixes by enabling static analysis code reviews and bug identification very early in the development cycle. FREE! Go There Now!
| | | | Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms. FREE! Go There Now!
| | | | Join this webcast to discover the key requirements for successful change and release management. Learn how to extend your .NET environment to improve productivity and collaboration, and address core problems afflicting team development. In this webcast, we’ll review typical challenges faced by customers and how to resolve them with the IBM Rational Change and Release Management solution, including Rational ClearCase, Rational ClearQuest and Rational Build Forge. Replay is available for 9 months. FREE! Go There Now!
| | | | Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily. FREE! Go There Now!
| | | | Visit IBM developerWorks to try the IBM SOA Sandbox for process. The SOA Sandbox for process focuses on providing a trial environment with the necessary tooling and components required to gain a better understanding of business processes and how to best improve existing business processes to derive value quickly. FREE! Go There Now!
| | | | As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security. FREE! Go There Now!
| | | | The discipline of assembling and delivering software is maturing beyond standard developer-centric compile/test software builds. The end-to-end software development lifecycle is emerging as the new focus moves “Beyond the Build.” Join this on demand webcast to learn about methods for streamlining software delivery and key capabilities of the IBM Rational Build Forge framework for automating build and release management in environments of any size. FREE! Go There Now!
| | | | All FREE IBM® developerWorks Tools! | |