Asp. NET to realize the detailed image processing

Source: Internet
Author: User
Tags bmp image file size implement sendfile
When using ASP, we often have to use third-party controls to implement some image functions. And now, ASP. NET, we have no need to use the third party control to implement, because ASP.net already has the powerful function to realize some image processing. Now, let's look at how to use this powerful feature of ASP.net.

First, the use of System.Drawing

The following example shows you how to generate a picture in memory and then displays the picture through a Web page. What you need to know is that we're not exporting HTML effects here, but real pictures (images) where we can save the output image using the Save as ....

Let's take a look at the effects first:


We see that this picture is a gradient background there are "see" a few words, of course, this effect in Photoshop and other image processing software is very easy to implement, but some of the database with the application of the combination we can not have all the pictures are designed, this time, It is important to use ASP.net to implement these functions. Let's look at the source code:

<%@ page language= "vb" contenttype= "Image/jpeg"%>
<%@ import namespace= "System.Drawing"%>
<%@ Import Name Space= "System.Drawing.Imaging"%>
<%@ import namespace= "System.Drawing.Drawing2D"%>

<%
' empty response
Response.Clear

' Creates a 120*30 size, 24bit BMP image;
Dim imgoutput As New Bitmap (120, 30, PIXELFORMAT.FORMAT24BPPRGB)

' Creates a new image based on BMP above;
Dim g As Graphics = Graphics.fromimage (imgoutput)

G.clear (color. Green)
G.smoothingmode = Smoothingmode.antialias

g.drawstring ("See?"). , New Font ("bold", 16,fontstyle.bold), New SolidBrush (Color.White), New PointF (2,4))

G.fillrectangle (new LinearGradientBrush (new Point (0,0), new Point (120,30), Color.FromArgb (0,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 see different from the database program, here specifically introduced the image processing of the name space System.Drawing and so on. The program first clears the response to ensure there is no output; Then, the program built a 120 times 30 large BMP image, and then build a new image on this basis, after the establishment of the image, we first "draw" the string "See", the string is 16 large bold black, color is white, Position is (2,4); Finally, we implement the gradient effect.

The above example is very simple, but if combined with the database, we can achieve many use of ASP may not think of the effect.
Second, read and change image file size

Read a picture? Do you want to use HTML directly? Of course, we are here to provide a choice and a way to achieve this function, specific to the use of this function, we may need more learning in practice. First 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"%>

<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 (PIXELFORMAT.FORMAT24BPPRGB)
Dim g As Graphics = Graphics.fromimage (imgoutput)
G.clear (Color.yellow)
g.DrawString ("Wrong!") ", New Font (" boldface ", 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 see two functions, one is sendfile, this function is mainly to display the picture on the server, the size of the picture through the width and height settings, while the program will automatically detect the type of picture; the other is Senderror, The main function of this function is to display an error message when a picture file on the server does not exist, and it is interesting to see that the error message is also given by the picture (pictured):



The above program displays the picture and changes the picture size, now, we will this program further, displays the picture and maintains the picture the aspect proportion, thus, and the actual application may be close, especially needs to make the electronic album or is the picture website time is more practical. Let's look at the main functions first:

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 deals specifically with changing the size of a picture, and the length and width of the original picture remain the same. Please refer to the above program code for other parts.
Third, paint special effects

It would be easy if you just showed the picture on the page. Now, let's go further and feel the power of asp.net. We will learn the techniques commonly used in image processing such as image inversion, image cutting and image stretching.
Let's take a look at the program effect:


Looking closely, we can find a variety of 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)

' Rotate picture
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,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 picture
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)

' Rotate picture
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 see the implementation of the various techniques of image processing, careful observation, we can know that the rotation of the picture is actually using a rotateflip method, while cutting and stretching the picture, completely by setting drawimage different parameters to achieve.

Iv. Summary

Asp. NET image processing can realize a lot of functions, we are here in fact just simple introduction, more multi-functional application, we need to explore in practice, summed up.


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.