Detailed image processing process in ASP. NET

Source: Internet
Author: User
Tags bmp image

When using ASP, we often need to use third-party controls to implement some image functions. Now,ASP. NETWe no longer need to use third-party controls to implement the release, because ASP. NET already has powerful functions to implement someImage 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 look at the source code:

<% @ Pagelanguage = "vb" contenttype = "image/jpeg" %>

<% @ Importnamespace = "system. drawing" %>

<% @ Importnamespace = "system. drawing. imaging" %>

<% @ Importnamespace = "system. drawing. drawing2d" %>

<%

'Clear Response

Response. clear

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

DimimgOutputasNewbitmap (120,30, pixelformat. format24bpprgb)

'Create a new image based on the above BMP;

Dimgasgraphics = graphics. fromimage (imgOutput)

G. clear (color. Green)

G. smoothingMode = smoothingMode. antiAlias

G. drawString ("have you seen it? ", Newfont (" ", 16, fontstyle. bold), newSolidBrush (Color. White), NewpointF (2, 4 ))

G. FillRectangle (NewlinearGradientBrush (Newpoint (), Newpoint (), color. fromArgb ),

Color. fromArgb (255,255,255,255)

ImgOutput.save(response.outputstream,imageformat.jpeg)

G. dispose ()

ImgOutput. dispose ()

Response. end

%>

In the above code, we can see that, unlike the database program, the image processing namespace system. drawing is introduced here. The program first clears the Response to ensure there is no output; then, the program creates a 120x30 BMP image, and then creates a new image on this basis. After the image is created, first, we will draw a string "have you seen it?". The character string is a 16-character bold string with a white color and a position of (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:

<% 'Importallrelevantnamespaces %>

<% @ Importnamespace = "System" %>

<% @ Importnamespace = "System. Drawing" %>

<% @ Importnamespace = "System. Drawing. Imaging" %>

<% @ Importnamespace = "System. IO" %>

<Scriptrunat = "server">

SubsendFile ()

DimgasSystem. Drawing. Image = System. Drawing. Image. FromFile (server. mappath (request ("src ")))

DimthisFormat = g. rawformat

DimimgOutputasNewBitmap (g, cint (request ("width"), cint (request ("height ")))

Ifthisformat. equals (system. drawing. imaging. imageformat. Gif) then

Response. contenttype = "image/gif"

Else

Response. contenttype = "image/jpeg"

Endif

ImgOutput. save (response. outputstream, thisformat)

G. dispose ()

ImgOutput. dispose ()

Endsub

SubsendError ()

DimimgOutputasNewbitmap (120,120, pixelformat. format24bpprgb)

Dimgasgraphics = graphics. fromimage (imgOutput)

G. clear (color. yellow)

G. drawString ("error! ", Newfont (" ", 14, fontstyle. bold), systembrushes. windowtext, NewpointF (2, 2 ))

Response. contenttype = "image/gif"

ImgOutput.save(response.outputstream,imageformat.gif)

G. dispose ()

ImgOutput. dispose ()

Endsub

</Script>

<%

Response. clear

Ifrequest ("src") = "" orrequest ("height") = "" orrequest ("width") = "" then

CallsendError ()

Else

Iffile. exists (server. mappath (request ("src") then

CallsendFile ()

Else

CallsendError ()

Endif

Endif

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:

FunctionNewthumbSize (currentwidth, currentheight)

DimtempMultiplierasDouble

Ifcurrentheight> currentwidththen

TempMultiplier = 200/currentheight

Else

TempMultiplier = 200/currentwidth

Endif

DimNewSizeasNewSize (CInt (currentwidth * tempMultiplier), CInt (currentheight * tempMultiplier ))

ReturnNewSize

EndFunction

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:

<% @ PageLanguage = "vb" Debug = "True" %>

<% @ Importnamespace = "system. drawing" %>

<% @ Importnamespace = "system. drawing. imaging" %>

<% @ Importnamespace = "system. drawing. drawing2d" %>

<%

DimstrFilenameasstring

DimiasSystem. Drawing. Image

StrFilename = server. mappath ("./chris-fsck.jpg ")

I = System. Drawing. Image. FromFile (strFilename)

DimbasNewsystem. drawing. bitmap (I. width, I. height, pixelformat. format24bpprgb)

Dimgasgraphics = graphics. fromimage (B)

G. clear (color. blue)

'Rotate the image

I. RotateFlip (System. Drawing. RotateFlipType. Rotate90FlipX)

G. drawimage (I, Newpoint (0, 0 ))

I. RotateFlip (System. Drawing. RotateFlipType. Rotate270FlipY)

G. RotateTransform (10)

G. drawimage (I, Newpoint (0, 0 ))

G. RotateTransform (10)

G. drawimage (I, Newpoint (20, 20 ))

G. RotateTransform (10)

G. drawimage (I, Newpoint (40, 40 ))

G. RotateTransform (10)

G. drawimage (I, Newpoint (40, 40 ))

G. RotateTransform (-40)

G. RotateTransform (90)

G. drawimage (I, Newrectangle (100,-400,100, 50), Newrectangle (20, 20, I. width-20, I. height-20), GraphicsUnit. Pixel)

G. RotateTransform (-90)

'Stretch the image

G. drawimage (I, Newrectangle (10, 10, 50, 50), Newrectangle (20, 20, I. width-20, I. height-20), GraphicsUnit. Pixel)

G. drawimage (I, Newrectangle (50,10, 90,50), Newrectangle (20,20, I. width-20, I. height-20), GraphicsUnit. Pixel)

G. drawimage (I, Newrectangle (110,10, 150,50), Newrectangle (20,20, I. width-20, I. height-20), GraphicsUnit. Pixel)

'Cut the image

G. drawimage (I, 50,100, Newrectangle (1860,110,), GraphicsUnit. Pixel)

G. drawimage (I, 140,100, Newrectangle (1860,110,), GraphicsUnit. Pixel)

'Rotate the image

I. RotateFlip (System. Drawing. RotateFlipType. Rotate180FlipX)

G. drawimage (I, 230,100, Newrectangle (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 implement in image processing. Here we are a simple introduction.

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.