ASP.NET中图象操作与缩放

ASP.NET中图象操作与缩放
原文: ASP.NET Image Manipulation and Zoom
作者:不详    翻译:YuL

这里需要两个ASP.NET页面:Zoom.aspx和ZoomProcessor.aspx。ZoomProcessor.aspx用于实际的图片操 作。这个页面输出的不是HTML,而是图象。这个输出图象就作为Zoom.aspx的图象源,然后在Zoom.aspx页面通过按钮控件来缩放、漫游和显 示图象。通过下面的内容,将向你介绍功能及其细节。你可能还会注意到重画图象的用时会更长一些。这主要是因为图象是在服务器端被创建的。如果我们把图象放 到一个稍小一点的窗口里,而且这个窗口没有HTML在它的下面或右边,那么我们也许就会把注意力转移到其它地方了。

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 the
'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 the
'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 As _
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 the
'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 As _
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((CInt(ImageWidth / 2) - e.X) / Z)
Y = CInt(hy.Value) - CInt((CInt(ImageHeight / 2) - e.Y) / Z)
End If
getimage()
End Sub

Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As _
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 As _
System.Web.UI.ImageClickEventArgs) Handles btnLeft.Click
X = X - 20
getimage()
End Sub

Private Sub btnRight_Click(ByVal sender As System.Object, ByVal e As _
System.Web.UI.ImageClickEventArgs) Handles btnRight.Click
X = X + 20
getimage()
End Sub

Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As _
System.Web.UI.ImageClickEventArgs) Handles btnUp.Click
Y = Y - 20
getimage()
End Sub

Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As _
System.Web.UI.ImageClickEventArgs) Handles btnDown.Click
Y = Y + 20
getimage()
End Sub

Private Sub btnZoom_Click(ByVal sender As System.Object, ByVal e As _
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 As _
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(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(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 As _
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值