简体   繁体   English

调整图片框大小并保持宽高比(1:1)vb.net

[英]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. 我有一个要调整大小的图片框,我需要它保持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() 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 这为我解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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