picturebox放大、缩小、旋转、拖拽

来源:互联网 发布:数据库安全管理原则 编辑:程序博客网 时间:2024/06/04 23:34

1、加载图像

   Dim temp As Byte()
        Try
            If cxh <> "" Then
                temp = myYw.GetPic(cxh)
                Dim stream As New MemoryStream(temp)
                picfile.Image = Image.FromStream(stream)
                picfile.SizeMode = PictureBoxSizeMode.AutoSize
                txtkuan.Text = picfile.Width
                txtgao.Text = picfile.Height
            End If

        Catch ex As Exception
        End Try

 

   '获取缩略图
        Public Function GetPic(ByVal xh As String) As Byte()
            Dim strSQL As String
            Dim dr As SqlDataReader
            strSQL = "Select * From ec_m Where xh=" & xh
            Try
                dr = SqlHelper.ExecuteReader(o.GetConn, CommandType.Text, strSQL)
                If dr.Read Then
                    Return DirectCast(dr("cd"), Byte())
                End If
                'Return SqlHelper.ExecuteScalar(o.GetConn, CommandType.Text, strSQL)
            Catch ex As Exception
            Finally
                If Not dr Is Nothing Then dr.Close()
            End Try

        End Function

 

 

2、缩小

   picfile.SizeMode = PictureBoxSizeMode.StretchImage
        picfile.Width = CInt(picfile.Width * 0.8)
        picfile.Height = CInt(picfile.Height * 0.8)

3、旋转

      Dim bmp As New Bitmap(picfile.Image)
        picfile.SizeMode = PictureBoxSizeMode.AutoSize
        bmp.RotateFlip(RotateFlipType.Rotate90FlipNone)
        picfile.Image = bmp
        txtgao.Text = picfile.Height
        txtkuan.Text = picfile.Width
4、最适合大小

    Dim tempkuan As Double
        Dim tempgao As Double

        picfile.SizeMode = PictureBoxSizeMode.StretchImage
        If CDbl(txtkuan.Text) > 1000 Then
            If txtgao.Text > 500 Then
                '1300*2300
                tempkuan = (1000 / picfile.Width)
                tempgao = (500 / picfile.Height)
                If tempkuan > tempgao Then
                    picfile.Width = CDbl(tempgao * txtkuan.Text)
                    picfile.Height = CDbl(tempgao * txtgao.Text)
                Else
                    picfile.Width = CDbl(tempkuan * txtkuan.Text)
                    picfile.Height = CDbl(tempkuan * txtgao.Text)
                End If
            Else

                '1200*490
                picfile.Width = (1000 / picfile.Width) * txtkuan.Text
                picfile.Height = (1000 / picfile.Width) * txtgao.Text
            End If
        Else
            If txtgao.Text > 500 Then
                tempgao = (500 / picfile.Height)
                picfile.Width = CDbl(tempgao * txtkuan.Text)
                picfile.Height = CDbl(tempgao * txtgao.Text)
            Else
                picfile.Width = txtkuan.Text
                picfile.Height = txtgao.Text
            End If
        End If

 

5、拖拽

‘先声明

    Dim mousePos0 As Point
    Dim canMove0 As Boolean

’事件处理

  Private Sub picfile_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picfile.MouseUp
        canMove0 = False
    End Sub

    Private Sub picfile_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picfile.MouseMove
        If canMove0 Then
            picfile.Location = New Point(picfile.Location.X - mousePos0.X + e.X, picfile.Location.Y - mousePos0.Y + e.Y)
        End If
    End Sub

    Private Sub picfile_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picfile.MouseDown
        canMove0 = True
        mousePos0 = New Point(e.X, e.Y)
    End Sub

 

 

原创粉丝点击