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"%>
Sub sendfile ()
Dimg as system.drawing.image = system.drawing.image.fromfile (Server.MAppath (Request ("src")))))))
DIM thisFormat = g.rawformat
DIM IMGOUTPUT AS New Bitmap (G, CINT (Request ("Width")), CINT ("HEIGHT"))))))))
IF thisformat.equals (system.drawing.imaging.imageformat.gif) THEN
Response.contentType = "image / gif"
Else
Response.contentType = "image / jpeg"
END IF
IMGOUTPUT.SAVE (Response.outputStream, thisFormat)
g.dispose ()
IMGOUTPUT.DISPOSE ()
End Sub
Sub senderror ()
DIM IMGOUTPUT AS New Bitmap (120, 120, Pixelformat.Format24bppRGB) DIM G AS Graphics = Graphics.FromImage (IMGOUTPUT)
g.clear (color.yellow)
g.drawstring ("Error!", New Font ("Black Body", 14, FontStyle.Bold, Systembrushes.WindowText, New Pointf (2, 2)))
Response.contentType = "image / gif"
Imgoutput.save (response.outputstream, imageformat.gif)
g.dispose ()
IMGOUTPUT.DISPOSE ()
End Sub
Script>
<%
Response.clear
If Request ("src") = "" "" "") = "" "" "" ") =" "" "
Call senderror ()
Else
IF file.exists (Server.MAppath (Request ("src")))
Call sendfile ()
Else
Call senderror ()
END IF
END IF
Response.end
%>
In the above programs, we see two functions, one is SendFile, this function is functional to display images on the display server, the size of the image is set by width and height, while the program will automatically detect the picture type; another It is Senderror. The main function of this function is not present, and the error message is displayed, and it is very interesting here. The error message is also given by the picture (Figure):
The above programs display pictures and change the picture size, now we will further display this program, display the picture and keep the picture of the picture as a long-width ratio, which may be relatively close, especially when making an electronic photo album or a picture website practical. Let's first see the main functions:
Function Newthumbsize (CurrentWidth, CurrentHeight)
DIM Tempmultiplier as Double
IF currentwidth then
TempMultiplier = 200 / Currentheight
Else
TemPmultiplier = 200 / CurrentWidth
END IF
Dim Newsize As New Size (CINT (CURRENTWIDTH * TEMPMULTIPLIER), CURRENTHEIGHT * TEMPMULTIPLIER)
Return newsize
END FUNCTION
The above program is an increased function newthumbsize, which deals with the size of the picture size, and the length width of this picture maintains the same ratio. Please refer to the above program code.
Third, drawing special effects
If you just display the picture on the web, this is not easy. Now let's further feel the powerful features of ASP.NET. We will learn the image of the image commonly used in image processing, image cutting, image stretching and other techniques. First look at the program effect:
Take a closer look, we can find various image processing effects. Now let's take a look at the program code:
<% @ Page language = "vb" debug = "true"%>
<% @ Import namespace = "system.drawing"%>
<% @ Import namespace = "system.drawing.imaging"%>
<% @ Import namespace = "system.drawing.drawing2d"%>
<%
DIM STRFILENAME AS STRING
Dim i as system.drawing.image
Strfilename = Server.mappath ("./ chris-fsck.jpg")
i = system.drawing.image.fromfile (strfilename)
Dim b as new system.drawing.bitmap (i.Width, I.Height, Pixelformat.Format24bpprgb)
DIM G as graphics = graphics.fromimage (b)
g.clear (color.blue)
'Rotating pictures
I.Rotateflip (System.drawing.RotateFlipType.Rotate90Flipx)
G.DrawImage (i, new point (0, 0))
I.Rotateflip (System.drawing.RotateFlipType.Rotate270FLIPY)
G.RotateTransform (10)
G.DrawImage (i, new point (0, 0))
G.RotateTransform (10)
G.drawImage (i, new point (20, 20))
G.RotateTransform (10)
g.drawImage (i, new point (40, 40))
G.RotateTransform (10)