How to display the image generated in the background to the client

xiaoxiao2021-03-06  100

How do I display the image generated in the background to the client?

Generate from C # and VB.NET

Thanks!

-------------------------------------------------- -------------

Thank you for using Microsoft products.

Here is the previous post for your reference:

/

Main topic: There is an Image control in the ASP.NET page, want to generate a 4-bit random number with C #, and write it into a picture, display it

This problem may need to use the .NET drawing function.

In a page, we use DrawString to draw numbers based on the submitted content, and then enter it into the imageur of another page of the Image control.

There may be some techniques in the middle, for example, how to make a picture on the ASP.NET server side, then how to bind the picture directly to the image control, etc. I wrote the following program for your reference:

Webform1.aspx:

============

A TextBox control used to enter numbers;

A image control used to display pictures;

A Button control is used to submit the TextBox;

Add the following code in the Button Click event:

This.image1.imageurl = @ "DRAW.ASPX? Number =" this.TextBox1.Text;

Draw.aspx:

=======

Using system.drawing.drawing2d;

Using system.drawing.image;

Private Void Page_Load (Object Sender, System.EventArgs E)

{

// put user code to initialize the page

IF (Null! = this.Request ["Number"])

{

String myNumber = this.Request ["Number"]. TOSTRING ();

Response.clear ();

INT height = 100;

Int width = 300;

Bitmap bmp = new bitmap (width, height, pixelformat.format24bpprgb);

Graphics g = graphics.FromImage (bmp);

g.smoothingmode = smoothingmode.antialias;

g.clear (color.lightgray);

g.drawRectangle (Pens.Whitesmoke, 0, 0, Width, Height);

G. DrawString (MyNumber, New Font ("Arial", 10), Systembrushes.WindowText, New Pointf (10, 50));

Bmp.save (this.Response.outputstream, Imageformat.gif);

g.dispose ();

bmp.dispose ();

Response.end ();

}

}

/

I hope to help you.

- Microsoft Global Technology Center - ZGH

This post is provided with "status quo" and there is no guarantee, and there is no right to grant any rights. Specific matters can be found in the Terms of Use (http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp). In order to create a better discussion environment, please participate in our user satisfaction survey (http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?Key= (S, 49854782) ).

-------------------------------------------------- -------------

When using ASP, we often use third-party controls to implement some image functions. Now, ASP.NET is launched, we have no need to use third-party controls, because ASP.NET has a powerful function to implement some image processing. Now let's take a look at how to use this powerful features of ASP.NET.

First, use of system.drawing

The following examples generate a picture in memory, then display this picture through the web page. What you need to know is that we are output here that it is not an HTML effect, but a real picture (image), we can use "Save As ..." to save the output image.

Let's take a look at the effect:

We see that this picture is "seeing a few words on a gradient background, of course, this effect is easy to achieve in image processing software such as Photoshop, but some applications with databases We cannot All pictures are designed in advance, this time, it is very important to use ASP.NET to implement these features. Let's see the source code:

<% @ Page language = "vb" ContentType = "image / jpeg"%>

<% @ Import namespace = "system.drawing"%>

<% @ Import namespace = "system.drawing.imaging"%>

<% @ Import namespace = "system.drawing.drawing2d"%>

<%

'Clearing Response

Response.clear

'Create a 120 * 30 size, 24bit of BMP image;

DIM IMGOUTPUT AS New Bitmap (120, 30, Pixelformat.Format24bppRGB)

'Establish a new image according to the above BMP;

DIM G as graphics = graphics.fromimage (IMGOUTPUT)

g.clear (color.green)

g.smoothingmode = smoothingmode.ntialias

g.drawstring ("Have you seen it?", New Font ("Black Body", 16, FontStyle.Bold, New Solidbrush (Color.White), New Pointf (2, 4))

G. FillRectangle (New Lineargradientbrush (New Point (0), New Point (120, 30), Color.Fromargb (0, 0, 0), Color.Fromargb (255, 255, 255, 255)), 0, 0, 120, 30) Imgoutput.save (response.outputstream, imageformat.jpeg)

g.dispose ()

IMGOUTPUT.DISPOSE ()

Response.end

%>

In the above code, we have different and the database program, which is specifically introduced into the image processing namespace system.drawing, etc. The program first cleared the response, ensuring no output; then, the program established a 120-large BMP image, and then established a new image on this basis. After building an image, we first "draw" string "See it," the string is 16 large crude black body, the color is white, the location is (2, 4); Finally, we achieve the gradient effect.

The above example is simple, but if we combine with the database, we can achieve many effects that use ASP may not dare.

Second, read and change the image file size

Read the picture? Can you use HTML directly? Of course, we only provide a selection and method to implement this feature, the specific use of this feature, we may need more learning in practice. Let's look at the program source code:

<% 'Import All Relevant Namespaces%>

<% @ Import namespace = "system"%>

<% @ Import namespace = "system.drawing"%>

<% @ Import namespace = "system.drawing.imaging"%>

<% @ Import namespace = "system.io"%>