简体   繁体   中英

Resize picturebox and maintain aspect ratio (1:1) vb.net

I have a picturebox that I am resizing and I need it to maintain an aspect ratio of 1:1. Basically have both the width and height be the same as the user is resizing. The resizing works fine, but the aspect ratio does not maintain. How could I change this to include maintaining aspect ratio?

This is what is called when the control is resized

    Private Sub pbsMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    If mouseOnHandle Then
        ReleaseCapture()
        SendMessage(activeControl.Handle, WM_NCLBUTTONDOWN, CInt(DirectCast(sender, PictureBox).Tag), 0)
        If GetCapture = 0 Then mouseOnHandle = False
        Application.DoEvents()
    End If
End Sub

ReleaseCapture()

    <DllImport("user32.dll")> _
Public Shared Function ReleaseCapture() As Boolean
End Function

Contstants and function

    Public Declare Function GetCapture Lib "user32" Alias "GetCapture" () As Integer

Private Const WM_NCLBUTTONDOWN As Integer = &HA1

I ended up adding this function to the resize event of the control

    Private Sub maintainAspectRatio()
    Dim width As Integer = activeControl.Width
    Dim height As Integer = activeControl.Height

    If width > height Then
        activeControl.Height = activeControl.Width
    ElseIf height > width Then
        activeControl.Width = activeControl.Height
    End If
End Sub

This solved the issue for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM