ASP. NET Image Processing

Source: Internet
Author: User
Tags bmp image sendfile
When using ASP, we often need to use third-party controls to implement some image functions. Now, with the release of ASP. NET, we do not need to use third-party controls to implement them, because ASP. NET already has powerful functions to implement some image processing. Now let's take a look at how to use this powerful feature of ASP. NET.

1. Use of system. Drawing

The following example shows how to generate an image in the memory and then display the image on the webpage. We need to know that what we output here is not the HTML effect, but a real image. We can use "Save ..." Save the output image.

Let's take a look at the effect:

We can see that this image is a gradient with the words "have you seen it?". Of course, this effect is easy to implement in Photoshop and other image processing software. However, some applications combined with databases cannot be designed in advance for all images. At this time, ASP is used. net. Let's seeSource code:

<% @ Page Language = "VB" contenttype = "image/JPEG" %>
<% @ Import namespace = "system. Drawing" %>
<% @ Import namespace = "system. Drawing. Imaging" %>
<% @ Import namespace = "system. Drawing. drawing2d" %>

<%
'Clear response
Response. Clear

'Create a 24-bit BMP image of 120*30;
Dim imgoutput as new Bitmap (120, 30, pixelformat. format24bpprgb)

'Create a new image based on the above BMP;
Dim G as graphics = graphics. fromimage (imgoutput)

G. Clear (color. Green)
G. smoothingmode = smoothingmode. antialias

G. drawstring ("have you seen it? ", New font (" ", 16, fontstyle. Bold), new solidbrush (color. White), new pointf (2, 4 ))

G. fillrectangle (New lineargradientbrush (new point (255,255,255,255), new point (), color. fromargb (,), color. fromargb)

Imgoutput. Save (response. outputstream, imageformat.jpeg)

G. Dispose ()
Imgoutput. Dispose ()
Response. End
%>

AboveCode, We can see and databaseProgramDifferent, the image processing namespace system. drawing is introduced here. The program first clears response to ensure there is no output; then, the program creates a 120 × 30 BMP image, and then creates a new image based on this, after the image is created, first, we will draw a string "have you seen it?". The string is a 16-character bold string with the white color and the position is (2, 4). Finally, we will implement the gradient effect.

The above example is very simple, but if combined with the database, we can achieve a lot of effects that may not be imagined by Using ASP.
Ii. Reading and changing the image file size

Reading images? Can I simply use HTML? Of course we can. Here we only provide a choice and method to implement this function. We may need to learn more about the use of this function in practice. Let's first look at the source code of the program:

<% 'Import all relevant namespaces %>
<% @ Import namespace = "system" %>
<% @ Import namespace = "system. Drawing" %>
<% @ Import namespace = "system. Drawing. Imaging" %>
<% @ Import namespace = "system. Io" %>

<SCRIPT runat = "server">
Sub sendfile ()
Dim G 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 (Request ("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 (" ", 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") = "" or request ("height") = "" or request ("width") = "" then
Call senderror ()
Else
If file. exists (server. mappath (Request ("src") then
Call sendfile ()
Else
Call senderror ()
End if
End if
Response. End
%>

In the above program, we can see two functions, one is sendfile. This function is mainly used to display images on the server. The image size is set through width and height. At the same time, the program will automatically detect the image type; the other is senderror. The main function of this function is to display the error message when the image file on the server does not exist. This is very interesting, the error message is also given through the image ():

The above program shows the image and changes the image size. Now, we will further display the image and keep the length and width ratio of the image. In this way, it may be close to the actual application, this is especially useful when you need to create an electronic album or an image website. Let's take a look at the main functions:

Function newthumbsize (currentwidth, currentheight)
Dim tempmultiplier as double
If currentheight> currentwidth then
Tempmultiplier = 200/currentheight
Else
Tempmultiplier = 200/currentwidth
End if
Dim newsize as new size (CINT (currentwidth * tempmultiplier), CINT (currentheight * tempmultiplier ))
Return newsize
End Function

The above program is an added function newthumbsize, which is used to change the image size for a moment. The image length and width are the same as the original image length and width. For other parts, see the above program code.
3. Graphic Effects

If you only display images on the web page, it is easy. Now, let's take a closer look at the powerful functions of ASP. NET. We will learn image inversion, image cutting, image stretching and other techniques commonly used in image processing.
Let's take a look at the program effect:

Take a closer look, we can find a variety of image processing effects. Now let's 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)

'Rotate the image
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 the image
G. drawimage (I, new rectangle (10, 10, 50, 50), new rectangle (20, 20, I. Width-20, I. Height-20), graphicsunit. pixel)
G. drawimage (I, new rectangle (50,10, 90,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)

'Cut the image
G. drawimage (I, 50,100, new rectangle (1860,110,), graphicsunit. pixel)
G. drawimage (I, 140,100, new rectangle (1860,110,), graphicsunit. pixel)

'Rotate the image
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 program, we can see various techniques for image processing. After careful observation, we can know that the rotating image actually uses a rotateflip method, while cutting and stretching the image, it is implemented by setting different drawimage parameters.

Iv. Summary

There are many functions that ASP. NET can achieve in image processing. Here we are a simple introduction. More application functions need to be explored and summarized in practice.

Related Article

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.