Image operations and scaling in ASP. NET

Source: Internet
Author: User
Image operations and scaling in ASP. NET

Original article: ASP. NET image manipulation and zoom
Author: Unknown Translation: Yul

Two ASP. NET pages are required: Zoom. aspx and zoomprocessor. aspx. Zoomprocessor. aspx is used for actual image operations.
. This page outputs images instead of HTML. The output image is used as the image source of zoom. aspx, And Then scaled, roaming, and displayed on the zoom. ASPX page using the button control.
Image Display. The following describes the functions and details. You may also notice that the re-image takes longer. This is mainly because the image is created on the server. If we place the image
To a slightly smaller window, and the window does not have HTML under it or on the right side, we may move our attention to other places.

Zoom. aspx design:

<Table>
<Tr>
<TD valign = "TOP">
<Table>
<Tr>
<TD> </TD>
<TD> <asp: imagebutton id = "btnup" runat = "server" imageurl = "up.jpg"
Alternatetext = "up"> </ASP: imagebutton> </TD>
<TD> </TD>
</Tr>
<Tr>
<TD> <asp: imagebutton id = "btnleft" runat = "server" imageurl = "left.jpg"
Alternatetext = "Left"> </ASP: imagebutton> </TD>
<TD> <asp: imagebutton id = "btnreset" runat = "server" imageurl = "reset.jpg">
</ASP: imagebutton> </TD>
<TD> <asp: imagebutton id = "btnright" runat = "server" imageurl = "right.jpg"
Alternatetext = "right"> </ASP: imagebutton> </TD>
</Tr>
<Tr>
<TD> </TD>
<TD> <asp: imagebutton id = "btndown" runat = "server" imageurl = "down.jpg"
Alternatetext = "down"> </ASP: imagebutton> </TD>
<TD> </TD>
</Tr>
<Tr>
<TD> <asp: imagebutton id = "btnout" runat = "server" imageurl = "out.jpg"
Alternatetext = "out"> </ASP: imagebutton> </TD>
<TD> </TD>
<TD> <asp: imagebutton id = "btnzoom" runat = "server" imageurl = "in.jpg"
Alternatetext = "in"> </ASP: imagebutton> </TD>
</Tr>
</Table>
<Input id = "HX" type = "hidden" runat = "server" name = "HX"> <input id = "hy"
Type = "hidden" runat = "server" name = "hy">
</TD>
<TD> <asp: imagebutton id = "btnmainimage" runat = "server" imageurl = "">
</ASP: imagebutton> </TD>
</Tr>
</Table>

Zoom. aspx code-behind.

# Region "properties"
'X holds the X coordinate of
'Center of the image
Public property X () as integer
Get
Return CINT (viewstate ("X "))
End get
Set (byval value as integer)
Viewstate ("X") = Value
End set
End Property
'Y holds the Y coordinate of
'Center of the image
Public property y () as integer
Get
Return CINT (viewstate ("Y "))
End get
Set (byval value as integer)
Viewstate ("Y") = Value
End set
End Property
'Z holds the zoom level
Public property Z () as integer
Get
Return CINT (viewstate ("Z "))
End get
Set (byval value as integer)
If value <1 then value = 1
Viewstate ("Z") = Value
End set
End Property
'Imagewidth holds the original image width
Public property imagewidth () as integer
Get
Return CINT (viewstate ("imagewidth "))
End get
Set (byval value as integer)
Viewstate ("imagewidth") = Value
End set
End Property
'Imageheight hods The Original Image Height
Public property imageheight () as integer
Get
Return CINT (viewstate ("imageheight "))
End get
Set (byval value as integer)
Viewstate ("imageheight") = Value
End set
End Property
# End Region

Private sub page_load (byval sender as system. Object, byval e _
System. eventargs) handles mybase. Load
If not ispostback () then
'On the first page load, we need to know
The origanal image's size and get
'Center X and Y coordinates
Dim I as system. Drawing. Image = _
System. Drawing. image. fromfile (server. mappath ("."&_
"\" & "Biglogo" & ". jpg "))
Imagewidth = I. Width
Imageheight = I. Height
X = CINT (imagewidth/2)
Y = CINT (imageheight/2)
Z = 1
I. Dispose ()
Getimage ()
End if
End sub

Private sub btnmainimage_click (byval sender as system. Object, byval e _
System. Web. UI. imageclickeventargs) handles btnmainimage. Click
If Z = 1 then
X = E. x
Y = E. Y
Z = 2
Else
X = CINT (HX. Value)-CINT (imagewidth/2)-E. X)/Z)
Y = CINT (Hy. Value)-CINT (imageheight/2)-E. Y)/Z)
End if
Getimage ()
End sub

Private sub btnreset_click (byval sender as system. Object, byval e _
System. Web. UI. imageclickeventargs) handles btnreset. Click
X = CINT (imagewidth/2)
Y = CINT (imageheight/2)
Z = 1
Getimage ()
End sub

Private sub btnleft_click (byval sender as system. Object, byval e _
System. Web. UI. imageclickeventargs) handles btnleft. Click
X = x-20
Getimage ()
End sub

Private sub btnright_click (byval sender as system. Object, byval e _
System. Web. UI. imageclickeventargs) handles btnright. Click
X = x + 20
Getimage ()
End sub

Private sub btnup_click (byval sender as system. Object, byval e _
System. Web. UI. imageclickeventargs) handles btnup. Click
Y = Y-20
Getimage ()
End sub

Private sub btndown_click (byval sender as system. Object, byval e _
System. Web. UI. imageclickeventargs) handles btndown. Click
Y = Y + 20
Getimage ()
End sub

Private sub btnzoom_click (byval sender as system. Object, byval e _
System. Web. UI. imageclickeventargs) handles btnzoom. Click
If z <8 then
Z = z * 2
End if

Getimage ()
End sub

Private sub btnout_click (byval sender as system. Object, byval e _
System. Web. UI. imageclickeventargs) handles btnout. Click
If z> 1 then
Z = CINT (z/2)
End if
Getimage ()
End sub

Private sub getimage ()
Dim imagename as string = "biglogo"
Dim srcx, srcy as integer

'Convert x value to top left of Image
Srcx = x-CINT (imagewidth/2)/Z)
If srcx <0 then srcx = 0
If srcx> imagewidth then srcx = imagewidth

'Convert y value to top left of Image
Srcy = Y-CINT (imageheight/2)/Z)
If srcy <0 then srcy = 0
If srcy> imageheight then srcy = imageheight

'Set the source of the image to be our processing ASPX page
Btnmainimage. imageurl = "zoomprocessor. aspx? X = "& srcx &_
"& Y =" & srcy & "& Z =" & Z & "& IMG =" & imagename
HX. value = x. tostring
Hy. value = Y. tostring

'Enable/disable buttons
If srcx> 0 then
Btnleft. Enabled = true
Else
Btnleft. Enabled = false
End if

If srcy> 0 then
Btnup. Enabled = true
Else
Btnup. Enabled = false
End if

If Z = 1 then
Btnout. Enabled = false
Else
Btnout. Enabled = true
End if

If Z = 8 then
Btnzoom. Enabled = false
Else
Btnzoom. Enabled = true
End if

End sub

Zoomprocessor. aspx code-behind.

Private sub page_load (byval sender as system. Object, byval e _
System. eventargs) handles mybase. Load

'Create a new image object from our source Image
Dim I as system. Drawing. Image = _
System. Drawing. image. fromfile (server. mappath ("./"_
& Request ("IMG") & ". jpg "))

'Create a workable bitmap image
Dim B as new system. Drawing. Bitmap (CINT (I. width ),_
CINT (I. Height), system. Drawing. imaging. pixelformat. format24bpprgb)

'Place the bitmap image in a graphics object
Dim G as graphics = graphics. fromimage (B)

'Set the default image background color
G. Clear (color. White)

'Crop the main Image
'Get coordinates and zoom values from querystring
Dim X as integer = CINT (Request ("X "))
Dim y as integer = CINT (Request ("Y "))
Dim Z as integer = CINT (Request ("Z "))
'Create 2 rectangles. We can grab a rectangle portion
'Of the original image and stretch it to fit the size
'Of the larger rectangle.
Dim SRC as new rectangle ()
Dim DST as new rectangle ()

'Set source rectangle Properties
SRC. x = x
SRC. Y = y
SRC. width = CINT (I. width/Z)
SRC. Height = CINT (I. Height/Z)

'Set destination rectangle Properties
DST. x = 0
DST. Y = 0
DST. width = CINT (I. width)
DST. Height = CINT (I. Height)

'Create the image from our rectangles
G. drawimage (I, DST, SRC, graphicsunit. pixel)

'Set the content type
Response. contenttype = "image/JPEG"

'Save the image as the output of this page
B. Save (response. outputstream, system. Drawing. imaging. imageformat. JPEG)

'Clean up
Src = nothing
DST = nothing
G = nothing
B = nothing
I = nothing
End sub
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.