iT邦幫忙

0

請問imagebutton內的圖片旋轉sub傳值無法累加問題

請問各位大大,我目前在使用imagebutton做圖片旋轉的功能,但是遇到傳rangle值給rangletotal無法正常累加問題,程式碼如下:
程式介面設計如下:

                        <table>
              <tr>
            <td style="width: 50%; text-align: center">
              <table>
                <tr>
                  <td>
                    <asp:ImageButton ID="ButPhoto" runat="server"  Width="400px" ImageUrl="~/images/nopass.png"/>
                  </td>
                </tr>
                <tr>
                  <td>
                  <asp:Label ID="LabPhoto" runat="server" ForeColor="#004080"></asp:Label>
                  </td>
                </tr>
              </table>
              <table class="table table-bordered tableShow tableshadow">
                <tr>
                  <th class="col-xs-1">右轉90度
                  </th>
                                      <td style="text-align: center">
                      <asp:ImageButton ID="ButR090" runat="server" AutoPostBack="True" ImageUrl="~/Images/Turn90.png"/>
                  </td>
                  <th class="col-xs-1">右轉180度
                  </th>
                                      <td style="text-align: center">
                       <asp:ImageButton ID="ButR180" runat="server" AutoPostBack="True" ImageUrl="~/Images/Turn180.png"/>
                  </td>
                  <th class="col-xs-1">左轉90度
                  </th>
                                      <td style="text-align: center">
                      <asp:ImageButton ID="ButR270" runat="server" AutoPostBack="True" ImageUrl="~/Images/Turn-90.png"/>
                  </td>
                </tr>
                <tr>
                </tr>
              </table>
                  </td>
              </tr>
                            </table>

程式碼如下:

Private rangletotal As Integer = 0
  Protected Sub ButR090_Click(sender As Object, e As ImageClickEventArgs) Handles ButR090.Click
        RotataePhoto(90)
    End Sub
    Protected Sub ButR180_Click(sender As Object, e As ImageClickEventArgs) Handles ButR180.Click
        RotataePhoto(180)
    End Sub
    Protected Sub ButR270_Click(sender As Object, e As ImageClickEventArgs) Handles ButR270.Click
        RotataePhoto(-90)
    End Sub
    Protected Sub RotataePhoto(ByVal rangle As Integer)
        Dim bfile As String = ""
        '↓rangletotal無法累加
        rangletotal = rangletotal + rangle
        If rangletotal = 360 Then
            rangletotal = 0
        End If
        If rangletotal = -90 Then
            rangletotal = 270
        End If
        If rangletotal = 450 Then
            rangletotal = 90
        End If
        If rangletotal = -180 Then
            rangletotal = 180
        End If
        Try
            bfile = startid & "___" & rangletotal.ToString() & ".jpg"
            If Not File.Exists(Server.MapPath("Temp\" & bfile)) Then
                '先通過URI建立一個WebRequest(請參考WebRequest構造函數)
                Dim wr As WebRequest = WebRequest.Create(Server.MapPath(ButPhoto.ImageUrl))
                '然後通過其GetResponse方法得到一個WebResponse()
                Dim res As WebResponse = wr.GetResponse
                '通過WebResponse.GetResponseStream方法得到的流來創建Bitmap()
                Dim bmp As New Drawing.Bitmap(res.GetResponseStream)
                Select Case rangletotal
                    Case 0                        bmp.RotateFlip(Drawing.RotateFlipType.RotateNoneFlipNone)
                    Case 90                        bmp.RotateFlip(Drawing.RotateFlipType.Rotate90FlipNone)
                    Case 180                        bmp.RotateFlip(Drawing.RotateFlipType.Rotate180FlipNone)
                    Case 270                        bmp.RotateFlip(Drawing.RotateFlipType.Rotate270FlipNone)
                End Select
                bmp.Save(Server.MapPath("Temp\" & bfile))
            End If
            Dim url As String = "Temp/" & bfile
            ButPhoto.ImageUrl = url
            ButPhoto.Attributes.Add("onClick", "window.open('" & url & "');") '// 避免postback
            MsgBox(rangletotal.ToString() & "_" & bfile.ToString())
        Catch ex As Exception
            Dim str As String = "系統執行發生錯誤:RotataePhoto()<br>"
            str += "message:" & ex.Message & "<br>"
            str += "rangle:" & rangletotal.ToString() & "<br>"
            str += "ImageUrl:" & Server.MapPath("Temp\" & bfile) & "<br>"
            LabError.Text = str
        End Try
    End Sub

以上的sub目前累加規律如下:
點選同一imagebutton只能在第一次點選時累加,第二次點選就無法累加,例如第一次點選ButR090,可以轉90度,再點選一次ButR090就不能累加,依然是90度,但是如果再點選ButR180,它會轉90+180=270的位置,但是重複點選ButR180,就又會無法累加。

sub的方法如下:
將圖片以id+旋轉角度命名,再儲存在server的temp資料夾中,因此每張圖片可以存取四種圖片id+0,id+90,id+180,id+270。

請問各位大大為何rangletotal無法正常累加,謝謝!

阿薛 iT邦新手 5 級 ‧ 2017-11-08 11:12:39 檢舉
已解決 程式碼如下:
Protected Sub ButR090_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButR090.Click
If Session("rangle") = "" Then
Session("rangle") = "90"
rangletotal = Integer.Parse(Session("rangle"))
Else
rangletotal = Integer.Parse(Session("rangle"))
rangletotal = rangletotal + 90
End If

If rangletotal = 360 Then
rangletotal = 0
End If
If rangletotal = -90 Then
rangletotal = 270
End If
If rangletotal = 450 Then
rangletotal = 90
End If
If rangletotal = -180 Then
rangletotal = 180
End If
Session("rangle") = rangletotal.ToString()
RotataePhoto(90)
End Sub

Protected Sub ButR180_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButR180.Click
If Session("rangle") = "" Then
Session("rangle") = "180"
rangletotal = Integer.Parse(Session("rangle"))
Else
rangletotal = Integer.Parse(Session("rangle"))
rangletotal = rangletotal + 180
End If

If rangletotal = 360 Then
rangletotal = 0
End If
If rangletotal = -90 Then
rangletotal = 270
End If
If rangletotal = 450 Then
rangletotal = 90
End If
If rangletotal = -180 Then
rangletotal = 180
End If
Session("rangle") = rangletotal.ToString()
RotataePhoto(180)
End Sub

Protected Sub ButR270_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButR270.Click
If Session("rangle") = "" Then
Session("rangle") = "-90"
rangletotal = Integer.Parse(Session("rangle"))
Else
rangletotal = Integer.Parse(Session("rangle"))
rangletotal = rangletotal - 90
End If

If rangletotal = 360 Then
rangletotal = 0
End If
If rangletotal = -90 Then
rangletotal = 270
End If
If rangletotal = 450 Then
rangletotal = 90
End If
If rangletotal = -180 Then
rangletotal = 180
End If
Session("rangle") = rangletotal.ToString()
RotataePhoto(-90)
End Sub
Protected Sub RotataePhoto(ByVal rangle As Integer)
Dim bfile As String = ""
rangletotal = Integer.Parse(Session("rangle"))
Try
bfile = startid & "_" & rangletotal.ToString() & ".jpg"
If Not File.Exists(Server.MapPath("Temp\" & bfile)) Then
'先通過URI建立一個WebRequest(請參考WebRequest構造函數)
Dim wr As WebRequest = WebRequest.Create(Server.MapPath(ButPhoto.ImageUrl))

'然後通過其GetResponse方法得到一個WebResponse()
Dim res As WebResponse = wr.GetResponse

'通過WebResponse.GetResponseStream方法得到的流來創建Bitmap()
Dim bmp As New Drawing.Bitmap(res.GetResponseStream)

Select Case rangle
Case 0
bmp.RotateFlip(Drawing.RotateFlipType.RotateNoneFlipNone)
Case 90
bmp.RotateFlip(Drawing.RotateFlipType.Rotate90FlipNone)
Case 180
bmp.RotateFlip(Drawing.RotateFlipType.Rotate180FlipNone)
Case -90
bmp.RotateFlip(Drawing.RotateFlipType.Rotate270FlipNone)
End Select

bmp.Save(Server.MapPath("Temp\" & bfile))
End If
Dim url As String = "Temp/" & bfile
ButPhoto.ImageUrl = url

ButPhoto.Attributes.Add("onClick", "window.open('" & url & "');") '// 避免postback

Catch ex As Exception
Dim str As String = "系統執行發生錯誤:RotataePhoto()<br>"
str += "message:" & ex.Message & "<br>"
str += "rangle:" & rangletotal.ToString() & "<br>"
str += "ImageUrl:" & Server.MapPath("Temp\" & bfile) & "<br>"
LabError.Text = str
End Try
End Sub

尚未有邦友回答

立即登入回答