How to enlarge image using scrollwheel in vb.net

I use a set of graphic overlays to draw an image inside a control using a graphic object. I placed the Picturebox inside the panel and set Panel to automatically scroll. Now I need to know how to do this - use the mouse scroll wheel to increase the image size in small increments, while maintaining image quality. Does anyone know how to do this?

When I update the Abdias software code below, the image starts to decrease if the Sizemode property of the image file is set to StretchImage. I have a mouse pan function that may interfere with the proper functioning of this code. Any ideas? What could make this work properly?

solvable

This code worked much better for me than either of the two below:

Private Sub PictureBox_MouseWheel(sender As System.Object, e As MouseEventArgs) Handles PictureBox1.MouseWheel If e.Delta <> 0 Then If e.Delta <= 0 Then If PictureBox1.Width < 500 Then Exit Sub 'minimum 500? Else If PictureBox1.Width > 2000 Then Exit Sub 'maximum 2000? End If PictureBox1.Width += CInt(PictureBox1.Width * e.Delta / 1000) PictureBox1.Height += CInt(PictureBox1.Height * e.Delta / 1000) End If End Sub 
+7
source share
3 answers

You can try this code. It is assumed that in the form ( PictureBox1 inside Panel1.AutoScroll = True with Panel1.AutoScroll = True ) there is Panel1 and PictureBox1 ) with the image installed on the PictureBox .

The code does not calculate the central point of scaling, but you can use e.Location (or eX / eY) for this.

Update - here is the new code, which (should be) more reliable than the previous one (see below):

 Public Class Form1 Private _originalSize As Size = Nothing Private _scale As Single = 1 Private _scaleDelta As Single = 0.0005 Private Sub Form_MouseWheel(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel 'if very sensitive mouse, change 0.00005 to something even smaller _scaleDelta = Math.Sqrt(PictureBox1.Width * PictureBox1.Height) * 0.00005 If e.Delta < 0 Then _scale -= _scaleDelta ElseIf e.Delta > 0 Then _scale += _scaleDelta End If If e.Delta <> 0 Then _ PictureBox1.Size = New Size(CInt(Math.Round(_originalSize.Width * _scale)), _ CInt(Math.Round(_originalSize.Height * _scale))) End Sub Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage 'init this from here or a method depending on your needs If PictureBox1.Image IsNot Nothing Then PictureBox1.Size = Panel1.Size _originalSize = Panel1.Size End If End Sub End Class 

Old code - it works, but is unstable with large changes, probably due to rounding errors in Scale ():

 Public Class Form1 Private _scale As New SizeF(1, 1) Private _scaleDelta As New SizeF(0.01, 0.01) '1% for each wheel tick Private Sub Form_MouseWheel(sender As System.Object, e As MouseEventArgs) Handles Me.MouseWheel 'count incrementally _scale.Height = 1 _scale.Width = 1 If e.Delta < 0 Then _scale += _scaleDelta ElseIf e.Delta > 0 Then _scale -= _scaleDelta End If If e.Delta <> 0 Then _ PictureBox1.Scale(_scale) End Sub Private Sub Form1_Load(sender As System.Object, e As EventArgs) Handles MyBase.Load PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage 'init picturebox size = image size If PictureBox1.Image IsNot Nothing Then PictureBox1.Scale(New SizeF(1, 1)) PictureBox1.Size = PictureBox1.Image.Size End If End Sub End Class 
+4
source

Basically, you need an image viewer. I used this before: http://cyotek.com/blog/creating-a-scrollable-and-zoomable-image-viewer-in-csharp-part-4

It works great. however, this is a user control.

for an image, you need to create graphics from the image, and then interpolate. here is an example: http://www.dotnetcurry.com/ShowArticle.aspx?ID=196

I have not tested this, but it looks like it will work.

0
source

I noticed that there is an undesirable effect with StretchImage SizeMode , which ignores the ratio of the image. I just added a variable for the ratio of width and height to include in the algorithm of "scaling". See _ratWidth and _ratHeight in the code below.

 Public Class Form1 Private _originalSize As Size = Nothing Private _scale As Single = 1 Private _scaleDelta As Single = 0.0005 Private _ratWidth, _ratHeight As Double Private Sub Form_MouseWheel(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel 'if very sensitive mouse, change 0.00005 to something even smaller _scaleDelta = Math.Sqrt(PictureBox1.Width * PictureBox1.Height) * 0.00005 If e.Delta < 0 Then _scale -= _scaleDelta ElseIf e.Delta > 0 Then _scale += _scaleDelta End If If e.Delta <> 0 Then _ PictureBox1.Size = New Size(CInt(Math.Round((_originalSize.Width * _ratWidth) * _scale)), _ CInt(Math.Round((_originalSize.Height * _ratHeight) * _scale))) End Sub Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage 'init this from here or a method depending on your needs If PictureBox1.Image IsNot Nothing Then _ratWidth = PictureBox1.Image.Width / PictureBox1.Image.Height _ratHeight = PirctureBox1.Image.Height / PictureBox1.Image.Width PictureBox1.Size = Panel1.Size _originalSize = Panel1.Size End If End Sub End Class 
0
source

All Articles