Home » Category » Microsoft Visual Basic

Microsoft Visual Basic: Zoom in & out of an Image within a PictureBox

202| Sun, 10 Feb 2008 04:36:00 GMT| slowcoder| Comments (22)
I have an image in a PictureBox and I want to be able to have buttons on my form that allow a user to zoom in and out as they wish. However, it doesn't seem like the .Zoom property works with the PictureBox. Is there another way around in this? Maybe a way to scale the PictureBox according to the same way .Zoom would work? Here's my code, but it did not zoom in or out:

Public Const ZOOM_MAX As Double = 25600
Public Const ZOOM_MIN As Double = 0.1

Private Sub mnuViewZoomIn_Click()
On Error GoTo ErrHandle
pic.Picture.Zoom = 2 * pic.Picture.Zoom
tbToolBar.Buttons("ZoomOut").Enabled = True
If pic.Picture.Zoom >= ZOOM_MAX Then
tbToolBar.Buttons("ZoomIn").Enabled = False
End If
Exit Sub

ErrHandle:
tbToolBar.Buttons("ZoomIn").Enabled = False
End Sub

Private Sub mnuViewZoomout_Click()
On Error GoTo ErrHandle
pic.Picture.Zoom = 0.5 * pic.Picture.Zoom
tbToolBar.Buttons("ZoomIn").Enabled = True
If pic.Picture.Zoom <= ZOOM_MIN Then
tbToolBar.Buttons("ZoomOut").Enabled = False
End If
Exit Sub

ErrHandle:
tbToolBar.Buttons("ZoomOut").Enabled = False
End Sub

Keywords & Tags: zoom, out, image, within, picturebox, microsoft, visual basic, vb

URL: http://www.programmerbase.com/visual-basic/560730/
 
«« Prev - Next »» 22 helpful answers below.
I'd recommend using the BitBlt and StretchBlt API's.

Have a look at the attachment. It basically covers your "Zoom In" part...

hannesthegreat | Fri, 09 Nov 2007 19:47:00 GMT |

That little thing is really ingenious. :)

wof | Fri, 09 Nov 2007 19:48:00 GMT |

HanneSThEGreaT, that is a very clever program. Thank you! I'm trying to adapt something like that in my program; however, I'm having trouble. I have one PictureBox containing an image like your Picture1, but I need that full image & PictureBox to zoom in or out rather than having a second PictureBox that shows the zoom (Picture3 in your program). I can move Mouse events into my Zoom Button click functions, but my problem is adapting to using only one PictureBox. Any guidance in the right direction is much appreciated.

slowcoder | Fri, 09 Nov 2007 19:49:00 GMT |

Hmmm the picturebox control does not even have a zoom property..
Zoom applies only to the printers control's Applies To
Printer Object, Printers Collection

Returns or sets the percentage by which printed output is to be scaled up or down. Not available at design time.

Syntax
object.Zoom [= number]

Hannes, that is smart code.

Slowcoder, to finally solve the problem we need to look at posibly passing the image in a hidden picturebox to StretchBlt... Hmm where's Dan when you need him ?

gremlinsa | Fri, 09 Nov 2007 19:50:00 GMT |

Thanx guys, you made my day! :)
I'll second the idea of making use of g hidden picturebox.
I'm busy with another option, slowcoder, I'll post it tomorrow.Hmm where's Dan when you need him ?
Yeah, Dan, where are you, :D

hannesthegreat | Fri, 09 Nov 2007 19:51:00 GMT |

OK, I've worked on something last night (this morning)..

It makes use of one picturebox, and Image control, and allows for shrinking and enlarging. It is a bit flickery though...

hannesthegreat | Fri, 09 Nov 2007 19:52:00 GMT |

Very good work Hannes.. I dont want to know how you worked out the ratio's to use but it was good..

Well now i took it and expanded of the matematics a little so now you can adjust the immage anywhere between 25% and 1000$ (1/4 size up to 10 times)

I also added a timer on the enlarge and shrink to slow it down a little. Have a go..

Gremmy..

gremlinsa | Fri, 09 Nov 2007 19:53:00 GMT |

WOW! NICE! :thumb: Very, Very Nicely done!.

I like the idea of having the Image_Pos Type, it makes the whole "managing" of the picture very easy.

PS: Thanx for still keeping my ac/dc pic :lol: :D

hannesthegreat | Fri, 09 Nov 2007 19:54:00 GMT |

WOW! HanneSThEGreaT & GremlinSA...your projects are great! And Gremlin, that scrollbar is a perfect addition to the zoom feature :) I'm going to try and adapt these concepts into my project today; however, I noticed that there's an Image control used...and I think I goofed in my earlier post by not saying that my image is actually a picture bound to the picturebox: pictureBox1.Picture = LoadPicture(TempFileName)

I definitely need to be more careful with my terminology. So I'm going to try and change my project to use an image control instead, but we'll see if I break any other code in the process...

slowcoder | Fri, 09 Nov 2007 19:55:00 GMT |

my 2 cents, replaceimgZoom.Visible = False
imgZoom.Top = Tmp_Pos.Top
imgZoom.Left = Tmp_Pos.Left
imgZoom.Height = Tmp_Pos.Height
imgZoom.Width = Tmp_Pos.Width
imgZoom.Visible = TruewithimgZoom.Move Tmp_Pos.Left, Tmp_Pos.Top, Tmp_Pos.Width, Tmp_Pos.Heightand most of the flickering will disappear.

bushmobile | Fri, 09 Nov 2007 19:56:00 GMT |

Cannot help but applauding, Hannes and Gremlin. :thumb:

Shame that I still have to "spread reputation around" before I can come back to you.

wof | Fri, 09 Nov 2007 19:57:00 GMT |

and most of the flickering will disappear.

That's actually a bit more than 2cents worth :p. Thanx, I was concerned about the flickering! :thumb:

Cannot help but applauding, Hannes and Gremlin. :thumb:

Shame that I still have to "spread reputation around" before I can come back to you.
Thanx man, it's always amazing how a simple idea can spark so many other ideas, and how more than one head always seem to work :)

Credit definitely goes to Gremmy on this one... :D

hannesthegreat | Fri, 09 Nov 2007 19:58:00 GMT |

Hey this is a clasic case of a Group of Guru's each adding in there own special methods to a unified cause.

credit goes out too all on this one..

Gremmy..

gremlinsa | Fri, 09 Nov 2007 19:59:00 GMT |

WOW! HanneSThEGreaT & GremlinSA...your projects are great! And Gremlin, that scrollbar is a perfect addition to the zoom feature :) I'm going to try and adapt these concepts into my project today; however, I noticed that there's an Image control used...and I think I goofed in my earlier post by not saying that my image is actually a picture bound to the picturebox: pictureBox1.Picture = LoadPicture(TempFileName)

I definitely need to be more careful with my terminology. So I'm going to try and change my project to use an image control instead, but we'll see if I break any other code in the process...
Are you trying to add this to the crop image app we worked on before..

I had an idea about it...

using a Rouge hidden Imagebox... leave the picturbox in place.. when doing a zoom, put the pic in the Imagebox. Zoom it to the right size. Place it over the Picbox in the right possition. Make it visible. BitBlt it into the Picture box, then hide it again..

Look it's going to be alot of code, and right now the wife want a bit of time online, so i wont be able to look at it right now.. but i will look into doing something a bit later..

Gremmy..

gremlinsa | Fri, 09 Nov 2007 20:00:00 GMT |

Okay i've been playing a little with this code and i found something very interesting..

the picturebox control's paintpicture method has just about what your looking for...Syntax
object.PaintPicture picture, x1, y1, width1, height1, x2, y2, width2, height2, opcode

The PaintPicture method syntax has these parts:
Part Description

object: Optional. An object expression that evaluates to an object in the Applies To list. If object is omitted, the Form object with the focus is assumed to be object.

Picture: Required. The source of the graphic to be drawn onto object. Must be the Picture property of a Form or PictureBox.

x1, y1: Required. Single-precision values indicating the destination coordinates (x-axis and y-axis) on object for picture to be drawn. The ScaleMode property of object determines the unit of measure used.

Width1: Optional. Single-precision value indicating the destination width of picture. The ScaleMode property of object determines the unit of measure used. If the destination width is larger or smaller than the source width (width2), picture is stretched or compressed to fit. If omitted, the source width is used.

Height1: Optional. Single-precision value indicating the destination height of picture. The ScaleMode property of object determines the unit of measure used. If the destination height is larger or smaller than the source height (height2), picture is stretched or compressed to fit. If omitted, the source height is used.

After all this it might not take that much to add this funtionability to a picturebox...

More when i'm done...

Gremmy..

gremlinsa | Fri, 09 Nov 2007 20:01:00 GMT |

Okay here it is..

I've used the Paintpicture method to do the Zoom on a Picturebox Control..

The zoom worked better than i expected.. (but going higher than about 5* is not recomended, It's a bit ikky up there..)

I have not completed adding in the code to make the Zoom work with the Crops.

So if you crop then zoom, the picture snaps to the original uncropped pic..

Will add this tomorrow, It's getting a bit late and i got a long work day ahead, so i need to get some sleep..

Gremmy...

-- Edit --

Worked on it this morning and sorted it all out .. the zoom works with only picturebox's.. and is fully integrated with the crop application..

i used a lot of variables and UDT's to enable this.. basically the original pic stays in the background, untouched.. a dummy pic shows the zoom, gets the selection for crop and apply's it to the original pic in the correct proportion..

okay - enough of that...

IT WORKS... try it...

-- REEDIT --

Newer file on later post...

gremlinsa | Fri, 09 Nov 2007 20:02:00 GMT |

Okay i worked a bit more on this (can you see i'm a little bored) ...

Some issue's sorted:
- Preview of crop is 1/2 Original and Not 1/2 Zoom anymore.
- Image sliders reacts with zoom corectly.
- Added a Selection box flasher routine (CPU stays at 0% while flashing :D)

Slowcoder. All this app is missing now is the acctual editing code ;)

Gremmy//...

gremlinsa | Fri, 09 Nov 2007 20:03:00 GMT |

Nice Work dude, nice! :thumb:

hannesthegreat | Fri, 09 Nov 2007 20:04:00 GMT |

Yeah, agreed. Big man in graphics programming, are ya? :)

wof | Fri, 09 Nov 2007 20:05:00 GMT |

man ... this is really GREAT work ... :thumb: :wave:

btw , i suggest converting it into VB.Net ! :rolleyes: :thumb:
maybe ...if you have some time :blush:

that would be a more than great ...
do u think it would be easier ! :rolleyes:

momoaal | Fri, 09 Nov 2007 20:06:00 GMT |

man ... this is really GREAT work ... :thumb: :wave:

btw , i suggest converting it into VB.Net ! :rolleyes: :thumb:
maybe ...if you have some time :blush:

that would be a more than great ...
do u think it would be easier ! :rolleyes:

You could have a look at these articles I wrote ( in VB.NET ) for all types of graphics manipulation. The last part ( part IV ) deals with zooming, rotating and cropping. I hope you enjoy them :)

Creating Your Own Drawing Application with Visual Basic .NET, Part 1
http://www.dev-archive.com/csharp/.net/net_general/graphics/article.php/c13205/

Creating Your Own Drawing Application with Visual Basic.NET, Part 2
http://www.dev-archive.com/csharp/.net/net_general/graphics/article.php/c13207/

Creating Your Own Drawing Application in Visual Basic.NET, Part 3
http://www.dev-archive.com/vb/gen/vb_graphics/gdi/article.php/c13611/

Creating Your Own Drawing Application in Visual Basic.NET, Part 4
http://www.dev-archive.com/vb/gen/vb_graphics/gdi/article.php/c14007

hannesthegreat | Fri, 09 Nov 2007 20:07:00 GMT |

thanks ... i'll check it ;)

momoaal | Fri, 09 Nov 2007 20:08:00 GMT |

Microsoft Visual Basic Hot Answers

Microsoft Visual Basic New questions

Microsoft Visual Basic Related Categories