asp.net 2.0 output image with response object

Source: Internet
Author: User

This article sample project will show you how to use the response object to output image data stored directly in the SQL Server 2005 database in the ASP.net 2.0 Web page. Draw and output image data and event design directly on the Web page.

First, Introduction

The response object, as one of the ASP.net basic objects, can output string data directly on the page through the Write () method, and can also use the BinaryWrite () method to directly display binary representations of data, such as images, pictures, and so on.

This article sample project will show you how to use the response object to output image data stored directly in the SQL Server 2005 database in the ASP.net 2.0 Web page.

"Note" In this case, we do not discuss the case where the URL of an image is stored only in a SQL Server 2005 table. Because this situation is widely used in practical development, it is not difficult for readers to search the web for appropriate usage cases.

First, let's analyze a simple example of drawing and outputting image data directly on a Web page.

Draw and output image data directly on the Web page

The following code example draws three partially overlapping rectangles when the page is requested. The code first sets the ContentType property to Image/jpeg to render the entire page as a JPEG image. In the second step, the code calls the clear method to ensure that extraneous content, including headers, is not sent with this response. In the third step, the code sets the BufferOutput property to True so that the page is sent to the requesting client after it has been fully processed. In step fourth, you create two objects for drawing rectangles, namely bitmap and graphics objects. The variables created in the page are the coordinates of the drawing rectangle and the string that is displayed in the largest rectangle.

When you draw the three rectangles and the strings that appear in them, save the bitmap to the stream object associated with the OutputStream property and format it as a JPEG. Next, the code calls the Dispose and Dispose methods to free resources-these resources are used by two drawing objects. Finally, the code calls the Flush method to send the buffered response to the requesting client.

The complete implementation code looks like this:

<%@ Page language= "C #"%>
<%@ import namespace= "System.Drawing"%>
<%@ import namespace= "System.Drawing.Imaging"%>
<%@ import namespace= "System.Drawing.Drawing2D"%>
<script runat= "Server" >
private void Page_Load (object sender, EventArgs e)
... {
Set the content type of the page to be a JPEG file
and clears all response header information.
Response.ContentType = "Image/jpeg";
Response.Clear ();
Buffer the response to send the page after processing completes
Response.bufferoutput = true;
Create a font style
Font rectanglefont = new Font (
"Arial", ten, FontStyle.Bold);
To create an integer variable
int height = 100;
int width = 200;
Creates a random number generator and creates it based on it
Variable Value
Random r = new Random ();
int x = R.next (75);
int a = R.next (155);
int x1 = r.next (100);
Create a bitmap and use it to create a
Graphics objects
Bitmap bmp = New Bitmap (
width, height, pixelformat.format24bpprgb);
Graphics g = graphics.fromimage (BMP);
G.smoothingmode = Smoothingmode.antialias;
G.clear (Color.lightgray);
Draw 3 rectangles with this graphics object
G.drawrectangle (Pens.white, 1, 1, width-3, height-3);
G.drawrectangle (Pens.aquamarine, 2, 2, width-3, height-3);
G.drawrectangle (pens.black, 0, 0, width, height);
Output a string using this Graphics object
On the rectangles.
g.DrawString (
"ASP.net Samples", Rectanglefont,
Systembrushes.windowtext, New PointF (10, 40));
Add color to two rectangles
G.fillrectangle (
New SolidBrush (
Color.FromArgb (A, 255, 128, 255)),
X, 20, 100, 50);
G.fillrectangle (
New LinearGradientBrush (
New Point (X, 10),
New Point (x1 + 75, 50 + 30),
Color.FromArgb (128, 0, 0, 128),
Color.FromArgb (255, 255, 255, 240)),
X1, 50, 75, 30);
Save the bitmap in the response stream and convert it to the JPEG format
Bmp. Save (Response.outputstream, imageformat.jpeg);
Frees the memory space used by graphics objects and bitmaps
G.dispose ();
Bmp. Dispose ();
Send the output to the client
Response.Flush ();
}
</script>
<body>
<form runat= "Server" >
</form>
</body>

Next, let's discuss the focus of this article-how to output image data stored directly in a database in an ASP page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.