-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Hi Ryan, sorry I neglected your question. It's been a busy time for me.
I think should be efficient enough to paint the superimposed image with Graphics.DrawImage (or draw the circle with Graphics.DrawEllilpse). See the SuperimposeImage function in the demo project for an example. I don't know whether using a pixel array to do the superimposition would have any advantage. My guess is that the difference probably won't be very much, and it may go one way or the other depending on the sizes of the images.
BB
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
line 82-83 in your demo
If sfd.FileName.ToUpper.EndsWith("PNG") Then
_BaseImage.Save(sfd.FileName, Imaging.ImageFormat.Png)
gives an error when I tried to save result image from PNG file:
Object reference not set to an instance of an object.
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Quote:
Originally Posted by
Goshx
line 82-83 in your demo
If sfd.FileName.ToUpper.EndsWith("PNG") Then
_BaseImage.Save(sfd.FileName, Imaging.ImageFormat.Png)
gives an error when I tried to save result image from PNG file:
Object reference not set to an instance of an object.
The base image exists if you load one. Alternatively you can stamp the zoomable image on a blank background. Otherwise there isn't anything to save.
The image composer is just a quick demo, not a full-fledged program. If you think it's necessary to check whether there is anything to save before you try to save it, add it to your own code.
BB
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Well... Playing with ImagePosition and ZoomFactor properties I've mentioned that they won't restore the same position as it was before. As higher zooming is as larger error we get.
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Quote:
Originally Posted by
Goshx
Well... Playing with ImagePosition and ZoomFactor properties I've mentioned that they won't restore the same position as it was before. As higher zooming is as larger error we get.
Yes, the position does tend to drift slightly when you zoom in and out heavily. The formulas in the code are inevitably a bit approximate (for example because the mouse position doesn't map to an exact image pixel when the zoom factor is less than 1) but there may be something making matters worse than necessary. I hope to find some time to look into it soon.
Meanwhile, I've spotted a rather stupid error in the ZoomPictureBox_Public Properties.vb file. The Set clause of the EnableMouseWheelZooming property should have
_EnableMouseWheelZooming = Value
instead of =True. That won't affect your problem, but it could be annoying for anyone trying to disable zooming with the mouse wheel:o.
BB
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
:) I wouldn't say it is slightly. First time when that happened I suspected that I made some mistake into code because window was completely blank. After that I started to move cursor and mouse wheel and... my image (4000,3000px) became visible as a small thumbnail. Far away behind window's borders.
Also, when you manipulate with picturebox content without moving of mouse whell and without dragging, image continues to change its starting positions in equal offsets.
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Goshx,
You did put in a fix for one known issue where the zoom was being applied as a delta to the current size, rather than as a current zoom value to the original size didn't you.
That was mentioned in the other thread that you started, and I'll quote it here. There may be other things like that.
Quote:
I noticed while testing that sometimes when I was zoomed in and drawing near the bottom of the image, that the drawing was offset quite aways from the mouse (always in Y in my case with several different images, but don't know if that would always be the case).
I determined the reason for this is that the bounds (_ImageBounds.Width and Height) ratios had drifted.
The Width and Height values were initially different in my images, but if you zoomed way out, then zoomed way in, because of the calculation of the width and height always being in reference to themselves (and not the original image) and limited to an Int (CInt), each would round to an Int at different points and eventually the Width and Height would become square to be in phase with each other.
So, I just modified those two lines where the Width and Height of the bounds was calculated to fix that by referencing the original size of the image. The other places where you use zoomRatio for recentering are fine as I assume once the bounds are correct, other calculations will take care of themselves.
Code:
'In ZoomPictureBox_Main.vb
'Calculate the image bounds for a given ZoomFactor,
Private Function GetZoomedBounds() As Rectangle
'Find the zoom center relative to the image bounds.
Dim imageCenter As Point = FindZoomCenter(_ZoomMode)
'Calculate the new size of the the image bounds.
_previousZoomfactor = _ImageBounds.Width / _Image.Width
If Math.Abs(_ZoomFactor - _previousZoomfactor) > 0.001 Then
Dim zoomRatio As Double = _ZoomFactor / _previousZoomfactor
' _ImageBounds.Width = CInt(_ImageBounds.Width * zoomRatio) 'Fixed these two lines so they don't accumulate Integer rounding values
' _ImageBounds.Height = CInt(_ImageBounds.Height * zoomRatio)
_ImageBounds.Width = CInt(_Image.Width * _ZoomFactor)
_ImageBounds.Height = CInt(_Image.Height * _ZoomFactor)
'Find the resulting position of the zoom center prior to correction.
Dim newPRelative As Point
newPRelative.X = CInt(imageCenter.X * zoomRatio)
newPRelative.Y = CInt(imageCenter.Y * zoomRatio)
'Apply a correction to return the zoom center to its previous position.
_ImageBounds.X += imageCenter.X - newPRelative.X
_ImageBounds.Y += imageCenter.Y - newPRelative.Y
End If
_previousZoomfactor = _ZoomFactor
Return _ImageBounds
End Function
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Yes, I've seen your post but didn't know that it was related with my question at that moment.
When I changed code to your, things are a bit better now (especially when image is not large and when it is centered). Unfortunately, problem still persists. Large zoomed images makes huge difference between original position and new one calculated from ImagePosition and ZoomFactor.
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Thanks to you both, Goshx and Passel, for identifying some problems with the ZoomPictureBox. There seem to be two different issues.
1. There is indeed something wrong with the "save view / restore view" logic. If only the zoom, or only the position, changes after saving the view, the restore works correctly. But after both zoom and position have changed, the restore goes wrong. That means I need to debug what happens in and after the ZoomFactor and ImagePosition property Set clauses.
2. A certain amount of imprecision happens due to the large zoom range. When zoomed right out (as an extreme example, a 2000 pixel wide image displayed at 10 pixels wide i.e. ZoomFactor 0.05) it's not surprising that it's difficult to use the mouse precisely. A microbe passing wind near your mouse could cause a jump of 50 image pixels, so to speak:). When you zoom right in (magnification > * 4 for example) the "pixels" tend to jiggle about due to the way GDI+ renders highly magnified images. But I don't see why this should result in a persistent shift. The corrections in the post above, although "cleaner code", above don't seem to make any visible difference here. I wonder if it's just a matter of rounding errors? I'll try replacing rectangles and points by RectangleFs and PointFs wherever possible, to see if it helps.
BB
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
an experienced VS programmer would find "the heck" without many problems, I think :)
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Quote:
Originally Posted by
Goshx
an experienced VS programmer would find "the heck" without many problems, I think :)
Well I don't even know what "the heck" is let alone how to find it! Never mind, I think I've got a handle on both problems.
re: 1
As long as you restore first the Zoom then the ImagePosition, save/restore seems to work fine. That's no doubt because setting ZoomFactor changes the ImagePosition, but not vice versa. I'll have to think how to build that into the control in some idiot-proof way.
re: 2
It is indeed a matter of Integer/Single rounding errors. I've replaced all the appropriate Rectangles, Points and CInts by RectangleFs, PointFs and CSngs. Now everything seems to zoom smoothly, or at least drift by no more than 1 pixel per full-range zoom. I must assume that anything more that that is either microbial flatulence or user tremor.
For those interested, there's a zip of my present (interim) version of the ZPB and TestForm attached. But I'll post a proper update soon.
BB
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
It is really funny and almost unbelievable that changing order of ImagePosition and ZoomFactor solves the problem. I could bet on 1 million that it is impossible :)
That means, you have to isolate/keep ZoomFactor value before calculating of ImagePosition. In any case, this problem has been solved. Thank you.
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
I'm glad it works for you too. You can never be sure:lol:.
I'm thinking of dealing with it by adding public SaveView and RestoreView methods to the next version. An alternative I first had in mind was to expose the ImageBounds property as Read/Write instead of ReadOnly. The problem with that is what to do if the user specifies a rectangle with a different aspect ratio to the image. I don't want to throw exceptions from the control if I can avoid it. Something that occurred to me was to stretch the image in one direction only to fit the specified rectangle. But it isn't the job of the ZoomPictureBox to do things like that: it's an image viewing control, not an image editor.
BB
-
Re: ZoomPictureBox: mod to constrain image to viewable area
I took your excellent ZoomPictureBox control and made a slight change that you may want to include. I added a "Constrain" property to enable/disable dragging of the image outside of the displayable area. Your control saved me a (rude word)-ton of work. Thanks.
<Category("_ZoomPictureBox"),
Description("Constrain image to within visible area")>
Public Property Constrain As Boolean
Get
Return _Constrain
End Get
Set(value As Boolean)
_Constrain = value
End Set
End Property
Private _Constrain As Boolean
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
If _dragging Then
Me.Invalidate(_ImageBounds)
'calculate new image position
Dim newx As Integer = _ImageBounds.X + e.X - _startDrag.X
Dim newy As Integer = _ImageBounds.Y + e.Y - _startDrag.Y
'adjust if Constrain=True to keep as much ofg image as possible in view
If _Constrain Then
Select Case True
'Image will fit in view - shift left or right to avoid clipping
Case _ImageBounds.Width <= Me.Width
If newx < 0 Then newx = 0
If newx + _ImageBounds.Width > Me.Width Then newx = Me.Width - _ImageBounds.Width
'shift left to remove vertical null space
Case newx > 0
newx = 0
'shift right to remove vertical null space
Case newx + _ImageBounds.Width < Me.Width
newx = Me.Width - _ImageBounds.Width
End Select
Select Case True
'Image will fit in view - shift up or down to avoid clipping
Case _ImageBounds.Height <= Me.Height
If newy < 0 Then newy = 0
If newy + _ImageBounds.Height > Me.Height Then newy = Me.Height - _ImageBounds.Height
'shift up to remove horizontal null space
Case newy > 0
newy = 0
'shift down to remove horizontal null space
Case newy + _ImageBounds.Height < Me.Height
newy = Me.Height - _ImageBounds.Height
End Select
End If
_ImageBounds.X = newx
_ImageBounds.Y = newy
_startDrag = e.Location
Me.Invalidate(_ImageBounds)
End If
MyBase.OnMouseMove(e)
End Sub
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Hi Rev. Jim, my apologies for not noticing your suggestion before. Yikes, it's been nearly 6 months! Somehow I got unsubscribed from this thread so I didn't get notified.
Now I have made some improvements to the ZoomPictureBox. Among other things it includes a way to prevent the image getting dragged out of view. The method is much the same as Rev. Jim's, but it deals with more situations (dragging, zooming and control size changed). There is a new DragMargin property that sets the minimum width that always stays visible.
The main change deals with some performance problems that affected the previous version. With large images (say more than 8 MPixels, depending on hardware), image dragging was jerky and slow when zoomed-in, and zooming could be very slow when zoomed-out. The new version works smoothly with much larger images, for example 30 MPixels. It even works reasonably well for a 200 MPixel image although I don't intend to try it with anything bigger!
Here's how it works. When you set the Image property, it generates a List of images each reduced by one quarter of the preceding pixel size until a minimum width or height of 128 pixels is reached. The OnPaint sub selects a suitable low-resolution image from the List and paints it immediately; this is much quicker than painting the full-res image. At the same time a System.Timers.Timer is triggered. Once the timer interval elapses, the image is replaced by the full-resolution version.
The attachments below are a zip file with the revised ZoomPictureBox code (two files, ZoomPictureBox_Main.vb and ZoomPictureBox_Public Properties.vb), and another zip file with code for a test form (TestForm.vb). There's no need to add a ZoomPictureBox to the test form in the designer, it's added in code. The form code allows you to double-click the ZoomPictureBox at runtime to select the image.
BB
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
No problem. Thanks for the update. I can't wait to check it out.
-
2 Attachment(s)
Re: ZoomPictureBox: picture control with mouse-centred zooming
I'm having several problems with the new code under Visual Studio 2017.
I load up the previous project from ZoomPictureBox+TestForm+Demo.zip and let VS2017 do the conversion.
I run the previous code after the rebuild to make sure it works (it does).
I replace the previous versions of ZoomPictureBox_Main.vb, ZoomPictureBox_Public Properties.vb and TestForm.vb with their newer version.
I try a rebuild and get a pile of errors
Attachment 166067
Attachment 166069
Could you please zip the current version as a complete VS project and post it?
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
I'm having several problems with the new code under Visual Studio 2017.
I load up the previous project from ZoomPictureBox+TestForm+Demo.zip and let VS2017 do the conversion.
I run the previous code after the rebuild to make sure it works (it does).
I replace the previous versions of ZoomPictureBox_Main.vb, ZoomPictureBox_Public Properties.vb and TestForm.vb with their newer version.
I try a rebuild and get a pile of errors
Attachment 166067
Could you please zip the current version as a complete VS project and post it?
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Apologies for the long delay. I didn't include a designer file in the TestForm.vb.zip in post #75, but you can get by without it by copying the code into an existing form.
The following steps work for me in VS2017:
1. Download the zip files from post #75 to a convenient folder and unzip them.
2. In Visual Studio 2017, start a new WinForms project. Leave Form1 and everything else unchanged.
3. Click Add Existing Item in the VS Project menu and select the three unzipped files (e.g. with Shift-select). Ignore any errors for the moment.
4. Select the code from TestForm.vb (excluding Class and End Class statements) and Cut it using Ctrl-x. Paste it into the default form (Form1). The errors should disappear.
5. Run the project.
The test form code enables you to Double-click the ZoomPictureBox to load an image, and change the form size to resize the ZoomPictureBox.
It's not hard to zoom and pan very large images (e.g. 50 megapixel) in this version. It even worked on my hardware with a 250 megapixel image, although the zooming was no longer smooth. By the way, I download large images for testing purposes from Wikipedia's Picture of the Day.
BB
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
First of all, thank you for making this available to us in the development community. Your product has been a tremendous addition to my application.
Is there a way to remove the image from the picture box when we are finished viewing/zooming it? I have a dilemma where I can't delete the image file that has been "Zoom Pictured" after I finish and close the popup window that has it on it.
Regular PictureBox controls have an .Image property that can be set to Nothing as well as an .Invalidate() method. I can't seem to find those things in your user control. I need a way to be able to delete the image from within my application if the user wishes to do so. However, if I have run the Zoom Picture Box on an image, I can't do that. I get the runtime error that the object is currently in use.
Thanks in advance,
Paul
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Hi Paul, welcome to VBForums.
The ZoomPictureBox allows you to set the Image to Nothing and to call its Invalidate method in much the same way as a PictureBox. In both cases, the image source file remains locked. The safest way to avoid this is to read the image as a Stream instead of opening it with Image.FromFile(filename) or New Bitmap(filename).
Code:
Using stream As IO.Stream = IO.File.OpenRead(filename)
ZoomPictureBoxName.Image = Image.FromStream(stream)
End Using
The source file is closed at the end of the Using block, so you are free to delete it independently of the image.
BB
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
I would like to contact the author of this library. I'm using the library in an application and need some information that I hope he can provide
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Quote:
Originally Posted by
davidgarnett
I would like to contact the author of this library. I'm using the library in an application and need some information that I hope he can provide
BoopsBoops is the author of this software. This thread is the place to ask your questions
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Quote:
Originally Posted by
.paul.
BoopsBoops is the author of this software. This thread is the place to ask your questions
Paul,
Thank you for your reply ... I'm new to VBForums. I thought I replied with my question to this thread.
If I didn't, please let me know how to reply.
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Quote:
Originally Posted by
davidgarnett
Paul,
Thank you for your reply ... I'm new to VBForums. I thought I replied with my question to this thread.
If I didn't, please let me know how to reply.
Yeah you're posting in the right place, but you haven't given any details with your comment. Boops Boops will need to know what it is you need to know....
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Quote:
Originally Posted by
.paul.
Yeah you're posting in the right place, but you haven't given any details with your comment. Boops Boops will need to know what it is you need to know....
There is no license information for ZPLib that I can find. Can the developer associate a license with the library? ... perhaps MIT?
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Quote:
Originally Posted by
davidgarnett
There is no license information for ZPLib that I can find. Can the developer associate a license with the library? ... perhaps MIT?
That question applies to any code library, not just ZPLib. And it's not something I know much about. Please address your question to another forum such as Visual Basic .Net or C#.
BB
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Boops Boops, I think Davidgarnett want to know what is the license associated with your library/class to know if he can use it freely or not in his own applications.
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
As far as I know all code posted in the CodeBank is unlicensed, so you are free use it however you like. if you include the code in a licensed project, it would be polite to add a comment referring to this thread or to my authorship. BB
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Hello, thank you for providing such great code.
Unfortunately, the main post directs to post #75 to get the February 2019 version, but post #75 redirects back to the main post, which are two zip files, both from 2012, none of them with the newly added "DragMargin" property. There may have been a mix-up when editing posts. Would it be possible for OP to upload the new version? Thanks a lot.
-
1 Attachment(s)
Re: ZoomPictureBox: picture control with mouse-centred zooming
Hi John, you are right. When I posted the last link nearly 3 years ago, I intended move it to post #1 where anyone could find it. But I discovered some errors and never got around to fixing them. Life got in the way...:blush: And then a month ago - even worse - I decided to clear out what seemed to be obsolete links in the thread. I forgot that the link in post #1 wasn't updated. What a mess: it took me quite a while to sort it out but now at least I have a new version with the following changes:
1. SafeMargin works OK.
2. The two separate files for the ZoomPictureBox class (Main and Properties) are now merged into one. Separating them seemed like a good idea at first but no longer.
3. The panning and zooming for very large images works smoothly, but they can be very slow to load. There must be a better way. And very large images seem prone to throw OutOfMemory exceptions in Visual Studio 2022. That will take some work
4. There was a problem with transparent backgrounds. I don't know if anyone noticed but it's fixed now.
Here's an interim version of the ZoomPictureBox Class you may care try out:
Attachment 183564
Questions, suggestions and criticism are welcome.
BB
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Dear Boops Boops, The authour of this thread
After downloading the files as per the instructions. I was so much thrilled as this was required
Indeed Great work and remarkable achievement.
Then i tried to develop an application but below coding generates error
in ZoomPictureBox_Main1 DoubleClick Event and btnGetFrontImg_Click Event
I've marked in Coding with Red mark and also pointed out the Error messages in quotes
Will appreciate your help for the right corrections as i am using vb.net VS2019
Code:
Public Class ZoomingMovingImages
Private _BaseImage As Image
Private _AddImage As Image
Public EmptyBitmapSize As New Size(500, 500)
Private Sub ZoomingMovingImages_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call FormLayout()
End Sub
Private Sub FormLayout()
'Set the PictureBox and ZoomPictureBox properties:
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
PictureBox1.BorderStyle = BorderStyle.Fixed3D
ZoomPictureBox_Main1.Parent = PictureBox1
ZoomPictureBox_Main1.BackColor = Color.Transparent
ZoomPictureBox_Main1.BorderStyle = BorderStyle.None
ZoomPictureBox_Main1.Bounds = PictureBox1.ClientRectangle
Dim zRect As Rectangle = ZoomPictureBox_Main1.ClientRectangle 'ZoomPictureBox1.ClientRectangle
Dim bmp As New Bitmap(zRect.Width, zRect.Height)
ZoomPictureBox_Main1.DrawToBitmap(bmp, zRect) 'ZoomPictureBox1.DrawToBitmap(bmp, zRect)
End Sub
Private Sub PictureBox1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs)
ZoomPictureBox_Main1.Bounds = PictureBox1.ClientRectangle
End Sub
Private Sub ZoomPictureBox_Main1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ZoomPictureBox_Main1.DoubleClick
PictureBox1.Image = SuperimposeImage(_BaseImage, _AddImage, ZoomPictureBox_Main1.ImagePosition, ZoomPictureBox_Main1.ZoomFactor)
ZoomPictureBox_Main1.ImagePosition = New Point(ZoomPictureBox_Main1.ImagePosition.X - 2, ZoomPictureBox_Main1.ImagePosition.Y - 2)
Cursor.Position = New Point(Cursor.Position.X - 2, Cursor.Position.Y - 2)
End Sub
'Get an image from file or an empty bitmap:
Private Function GetImage(ByVal currentImage As Image, ByVal emptyBitmapSize As Size, ByVal caption As String) As Image
Dim bmp As Bitmap
If currentImage Is Nothing Then
bmp = New Bitmap(emptyBitmapSize.Width, emptyBitmapSize.Height)
Else
bmp = New Bitmap(currentImage)
End If
Using ofd As New OpenFileDialog
ofd.Title = caption
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
bmp = New Bitmap(ofd.FileName)
Catch
MessageBox.Show("Invalid image file " & ofd.FileName)
End Try
End If
End Using
Return bmp
End Function
Private Function SuperimposeImage(ByVal baseImage As Image, ByVal addImage As Image, ByVal location As Point, ByVal zoomFactor As Double) As Image
If baseImage IsNot Nothing AndAlso addImage IsNot Nothing Then
Using g As Graphics = Graphics.FromImage(baseImage)
g.DrawImage(addImage, location.X, location.Y,
CInt(addImage.Width * zoomFactor),
CInt(addImage.Height * zoomFactor))
End Using
End If
Return baseImage
End Function
Private Sub SaveImage(ByVal img As Image)
Using sfd As New SaveFileDialog
sfd.Filter = "Image Files (*.png, *.jpg)|*.png;*.jpg"
If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
If sfd.FileName.ToUpper.EndsWith("PNG") Then
_BaseImage.Save(sfd.FileName, Imaging.ImageFormat.Png)
ElseIf sfd.FileName.ToUpper.EndsWith("JPG") Then
_BaseImage.Save(sfd.FileName, Imaging.ImageFormat.Jpeg)
Else
MessageBox.Show("Please give a PNG or JPG extension. Other formats are not yet supported.")
End If
End If
End Using
End Sub
Private Sub btnGetImgFile_Click(sender As Object, e As EventArgs) Handles btnGetImgFile.Click
_BaseImage = GetImage(PictureBox1.Image, EmptyBitmapSize, "Select base image")
PictureBox1.Image = _BaseImage
ZoomPictureBox_Main1.Bounds = PictureBox1.ClientRectangle
End Sub
Private Sub btnGetFrontImg_Click(sender As Object, e As EventArgs) Handles btnGetFrontImg.Click
_AddImage = GetImage(ZoomPictureBox_Main1.Image, EmptyBitmapSize, "Select zoomable image")
ZoomPictureBox_Main1.Image = _AddImage
End Sub
End Class
I get Totally 8 errors
ie below single single
Handles ZoomPictureBox_Main1.DoubleClick : Handle Clause requires a with events variable defined in the containing type or one of its base types
and below 7 with repeated messages in respective lines of syntaxes
ImagePosition is is not a member of ZoomPictureBox_Main1
ZoomFactor is not a member of ZoomPictureBox_Main1
Image is is not a member of ZoomPictureBox_Main1
Thankx
nkvb
-
Re: ZoomPictureBox: picture control with mouse-centred zooming
Boops Boops
I've succeeded in resolving the issues as per #92.
Wherever Errors cropping up have marked in RED and Underlined
But now I am not able to access ZPBLib
Imports ZPBlib GreenLine displaying under ZPBlib Although ZPBlib Project file Exists in Solution Explorer
Now I've error coming up BC42025: Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated at following line in below code
Also GreenLine dispalying ZoomPictureBox_Main1.ZoomType
'Below Trackbar I've used to Zoom the ZoomPicturebox
Code:
Imports ZPBlib
Public Class ZoomingMovingImages
Private _BaseImage As Image
Private _AddImage As Image
Public EmptyBitmapSize As New Size(500, 500)
Private Sub ZoomingMovingImages_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call FormLayout()
End Sub
Private Sub FormLayout()
'Set the PictureBox and ZoomPictureBox properties:
PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
PictureBox1.BorderStyle = BorderStyle.Fixed3D
ZoomPictureBox_Main1.Parent = PictureBox1
ZoomPictureBox_Main1.BackColor = Color.Transparent
ZoomPictureBox_Main1.BorderStyle = BorderStyle.None
ZoomPictureBox_Main1.Bounds = PictureBox1.ClientRectangle
Dim zRect As Rectangle = ZoomPictureBox_Main1.ClientRectangle 'ZoomPictureBox1.ClientRectangle
Dim bmp As New Bitmap(zRect.Width, zRect.Height)
ZoomPictureBox_Main1.DrawToBitmap(bmp, zRect) 'ZoomPictureBox1.DrawToBitmap(bmp, zRect)
End Sub
Private Sub PictureBox1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs)
ZoomPictureBox_Main1.Bounds = PictureBox1.ClientRectangle
End Sub
Private Sub ZoomPictureBox_Main1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ZoomPictureBox_Main1.DoubleClick
PictureBox1.Image = SuperimposeImage(_BaseImage, _AddImage, ZoomPictureBox_Main1.ImagePosition, ZoomPictureBox_Main1.ZoomFactor)
ZoomPictureBox_Main1.ImagePosition = New Point(ZoomPictureBox_Main1.ImagePosition.X - 2, ZoomPictureBox_Main1.ImagePosition.Y - 2)
Cursor.Position = New Point(Cursor.Position.X - 2, Cursor.Position.Y - 2)
End Sub
'Get an image from file or an empty bitmap:
Private Function GetImage(ByVal currentImage As Image, ByVal emptyBitmapSize As Size, ByVal caption As String) As Image
Dim bmp As Bitmap
If currentImage Is Nothing Then
bmp = New Bitmap(emptyBitmapSize.Width, emptyBitmapSize.Height)
Else
bmp = New Bitmap(currentImage)
End If
Using ofd As New OpenFileDialog
ofd.Title = caption
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
bmp = New Bitmap(ofd.FileName)
Catch
MessageBox.Show("Invalid image file " & ofd.FileName)
End Try
End If
End Using
Return bmp
End Function
Private Function SuperimposeImage(ByVal baseImage As Image, ByVal addImage As Image, ByVal location As Point, ByVal zoomFactor As Double) As Image
If baseImage IsNot Nothing AndAlso addImage IsNot Nothing Then
Using g As Graphics = Graphics.FromImage(baseImage)
g.DrawImage(addImage, location.X, location.Y,
CInt(addImage.Width * zoomFactor),
CInt(addImage.Height * zoomFactor))
End Using
End If
Return baseImage
End Function
Private Sub SaveImage(ByVal img As Image)
Using sfd As New SaveFileDialog
sfd.Filter = "Image Files (*.png, *.jpg)|*.png;*.jpg"
If sfd.ShowDialog = Windows.Forms.DialogResult.OK Then
If sfd.FileName.ToUpper.EndsWith("PNG") Then
_BaseImage.Save(sfd.FileName, Imaging.ImageFormat.Png)
ElseIf sfd.FileName.ToUpper.EndsWith("JPG") Then
_BaseImage.Save(sfd.FileName, Imaging.ImageFormat.Jpeg)
Else
MessageBox.Show("Please give a PNG or JPG extension. Other formats are not yet supported.")
End If
End If
End Using
End Sub
Private Sub btnGetImgFile_Click(sender As Object, e As EventArgs) Handles btnGetImgFile.Click
_BaseImage = GetImage(PictureBox1.Image, EmptyBitmapSize, "Select base image")
PictureBox1.Image = _BaseImage
ZoomPictureBox_Main1.Bounds = PictureBox1.ClientRectangle
End Sub
Private Sub btnGetFrontImg_Click(sender As Object, e As EventArgs) Handles btnGetFrontImg.Click
_AddImage = GetImage(ZoomPictureBox_Main1.Image, EmptyBitmapSize, "Select zoomable image")
ZoomPictureBox_Main1.Image = _AddImage
End Sub
Private Sub ZoomSlider_Scroll(sender As Object, e As EventArgs) Handles ZoomSlider.Scroll
ZoomPictureBox_Main1.EnableMouseWheelZooming = False
ZoomPictureBox_Main1.EnableMouseDragging = False
ZoomPictureBox_Main1.ZoomMode = ZoomPictureBox_Main1.ZoomType.ControlCenter
If ZoomSlider.Value = 1 Then ZoomPictureBox_Main1.ZoomFactor = 1
If ZoomSlider.Value = 2 Then ZoomPictureBox_Main1.ZoomFactor = 1.25
If ZoomSlider.Value = 3 Then ZoomPictureBox_Main1.ZoomFactor = 1.5
If ZoomSlider.Value = 4 Then ZoomPictureBox_Main1.ZoomFactor = 2
If ZoomSlider.Value = 5 Then ZoomPictureBox_Main1.ZoomFactor = 2.5
End Sub
End Class
Some Observations when trackbar scrolled No Doubt ZoomPictureBox_Main1 Displays all the images with respective zoomFactor
and does not resemble as if zooming because smaller to larger all the images zoomed sized are displayed. How can all these Images with zoomed sized be cleared and resemble as Zooming
your guidance and help shall be appreciated
nkvb