简体   繁体   English

Vb.net图片框缩放

[英]Vb.net picturebox zooming

I am attempting to replicate windows photo viewer, to an extent. 我在一定程度上尝试复制Windows照片查看器。

Right now I have a form with a picturebox, and the ability to move it around with the mouse and zoom in/out with the scroll wheel. 现在,我有了一个带有图片框的窗体,并且能够用鼠标在窗体中移动并使用滚轮放大/缩小。

However, I wish to zoom towards the mouse pointer. 但是,我希望放大到鼠标指针。 You can see what I am trying to explain by opening a decently big image in windows photo viewer, and zooming with your mouse somewhere away from the center of the image. 您可以通过在Windows照片查看器中打开一个相当大的图像,然后将鼠标缩放到远离图像中心的位置来查看我要解释的内容。 I want to replicate that, but so far I can only zoom in and out. 我想复制它,但是到目前为止,我只能放大和缩小。

I know that I have to move the image in the opposite direction of the mouse pointer to the center of the form, and vary how much it moves per scroll wheel tick depending on how far your mouse is from the center of the form, but this is where I'm stuck. 我知道我必须将图像沿与鼠标指针相反的方向移动到表单的中心,并根据鼠标距表单中心的距离来改变每个滚动轮刻度的移动量。是我被困的地方。

Here's my laughable, confused piece of code that's half commented out and partway between not working and completely not working: 这是我可笑的,混乱的代码,一半被注释掉,介于无法正常工作和完全无法正常工作之间:

    Dim Me_Center As Point = New Point(Me.Width / 2, Me.Height / 2)
    Dim PB_Center_R As Point = New Point(PictureBox1.Width / 2, PictureBox1.Height / 2)
    Dim PB_Center As Point = New Point(PictureBox1.Location.X + PB_Center_R.X, PictureBox1.Location.Y + PB_Center_R.Y)
    Dim PB_Diff As Point = (PB_Center - MousePos)


    PictureBox1.Location = New Point((Me_Center - PB_Center_R) - PB_Diff)

    'PictureBox1.Location = New Point((Me.Width / 2) - (PictureBox1.Width / 2), (Me.Height / 2) - (PictureBox1.Height / 2))

    '(Me.Width / 2) - (PictureBox1.Width + Pos.X / 2), (Me.Height / 2) - (PictureBox1.Height - Pos.Y / 2)
    'PictureBox1.Location = New Point((Me.Width / 2 - (PictureBox1.Width / 2)) + XP, (Me.Height / 2 - (PictureBox1.Height / 2)) - YP)

this does pretty much exactly what I want (if you hit the "Open Zoomable Image" when running this form, however I can't understand exactly how it works: http://www.vbforums.com/showthread.php?654846-ZoomPictureBox-picture-control-with-mouse-centred-zooming 这几乎完全符合我的要求(如果在运行此表单时单击“打开可缩放图像”,但是我无法确切了解其工作原理: http ://www.vbforums.com/showthread.php?654846- ZoomPictureBox图象-控制-与-小鼠为中心,变焦

You can resize the PictureBox that contains the image the way you want. 您可以根据需要调整包含图像的PictureBox的大小。 Set the SizeMode to Zoom so the image readjust its size automatically: 将SizeMode设置为Zoom以便图像自动重新调整其大小:

PictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom

And then resize the PictureBox as you want. 然后根据需要调整PictureBox的大小。

You can detect the mouse position in the form by adding the event MouseMove on the form and storing the position in a variable. 您可以通过在表单上添加事件MouseMove并将该位置存储在变量中来检测表单中的鼠标位置。 Or you can get the mouse position in the screen at any moment with: 或者,您可以随时通过以下方式获取鼠标在屏幕中的位置:

Dim p As Point = Me.PointToClient(Cursor.Position)

You can read a similar question here: How to zoom in a Picturebox with scrollwheel in vb.net 您可以在此处阅读类似的问题: 如何在vb.net中使用滚轮放大Picturebox

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

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