Author: Unknown, please contact me
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)
g.drawImage (i, new point (40, 40))
g.rotateTransform (-40)
G.RotateTransform (90)
G. DrawImage (i, new rectangle (100, -400, 100, 50), New Rectangle (20, 20, I.Width-20, I.HeiGHT-20), GraphicsUnit.pixel
g.rotatetransform (-90)
'Stretch picture
G. DrawImage (I, New Rectangle (10, 10, 10, 50), New Rectangle (20, 20, I.Width-20, I.Height-20), GraphicsUnit.pixel
G. DrawImage (I, New Rectangle (50, 10, 10, 50), New Rectangle (20, 20, I.Width-20, I.HeiGHT-20), GraphicsUnit.pixel
G. DrawImage (I, New Rectangle (110, 10, 150, 50), New Rectangle (20, 20, I.Width-20, I.HeiGHT-20), GraphicsUnit.pixel
'Cutting pictures
G.drawImage (i, 50, 100, new rectangle (180, 80, 60, 110), graphicsunit.pixel
G.drawImage (i, 140, 100, new rectangle (180, 80, 60, 110), graphicsunit.pixel
'Rotating pictures
I.Rotateflip (System.drawing.RotateflipType.Rotate180flipx)
G.drawImage (i, 230, 100, new rectangle (180, 110, 60, 110), graphicsUnit.pixel
Response.contentType = "image / jpeg"
B.Save (response.outputstream, imageformat.jpeg)
b.dispose ()
%>
In the above programs, we see various techniques for realizing image processing, carefully observe, we can know that the rotating picture is actually using a RotateFlip method; while cutting and stretching pictures, it is completely different parameters of DrawImage. achieve.
Fourth, summary