How to resize a picture and its picturebox?
Hi everybody,
I have a picturebox in VB6 on a form that I load normally from a bmp file on the hard disk by calling the LoadPicture function like this:
Picture1.Picture = LoadPicture(mypath + "\" + myfilename)
I need to have buttons on the screen so the user can enlarge or reduce the picture.
I cannot use an image box, and it has to be a picturebox.
How can I enlarge or reduce the picture (and its picture box) easily and without losing the picture quality (or with minimum possible quality loss)?
Please help.
thanks,
Re: How to resize a picture and its picturebox?
You can use PaintPicture (there are tons of sample posted) method but image at some point will get distorted.
Re: How to resize a picture and its picturebox?
But, when I have some pictures on the hard drive and I just double-click on one of them, Windows shows it with good quality, and I can use the + and - signs to enlarge and reduce the size many times and the picture never gets distorted.
So ther should be a way to do it programmically too.
After all the operating system does it (behind the scenes) programmically, so if I want to do it programmically, it should be possible.
Any comment on that?
Any help will be appreciated.
thanks,
Re: How to resize a picture and its picturebox?
When scaling an image, there are several algorithms that have been created over time. VB by default, uses what is called the Nearest Neighbor algorithm. It is extremely fast but can produce questionable quality.
Other common algorithms are BiLinear and BiCubic. GDI+ has 5 algos and various other quality settings that can produce very good quality scaled images. You don't have to use GDI+, the bilinear and bicubic algorithms can be found on the web and would require you to extract the pixel data, send it through your own scaling algorithm, and then paint the result to the picturebox. GDI+ is very fast when compared to doing this manually with VB.
But as RhinoBull mentioned, distortion is always in play, but higher quality scaling algorithms can minimize the bad effects. Your O/S most likely using GDI+ when resizing the image.
Re: How to resize a picture and its picturebox?
I use this code:
Picture2.Height = Picture1.Height * 2
Picture2.Width = Picture1.Width * 2
' Resize the picture.
Picture2.AutoRedraw = True
Picture2.PaintPicture Picture1.Image, _
Picture2.ScaleLeft, Picture2.ScaleTop, _
Picture2.ScaleWidth, Picture2.ScaleHeight, _
Picture1.ScaleLeft, Picture1.ScaleTop, _
Picture1.ScaleWidth, Picture1.ScaleHeight
And it works.
But my problem is that I don't want to have a separate picturebox named "Picture2".
I want to resize the picture within the same picturebox "Picture1", and however I try to experiment with the above code, it doesn't do the job.
Can you please help me?
On a side note: Please note that in the line below:
Picture2.PaintPicture Picture1.Image
I cannot use Picture1.Picture, because the picturebox is loaded from an array in memory, and not directly from hard disk.
Re: How to resize a picture and its picturebox?
Simple search (at least through my posts) would you get this very simple procedure...
And here is how to use it:
Code:
Option Explicit
Public Sub ZoomPicture(pct As PictureBox, zoom As Double)
With pct
.AutoRedraw = True
.Width = .Width * zoom
.Height = .Height * zoom
.PaintPicture .Picture, 0, 0, .ScaleWidth, .ScaleHeight
.Refresh
End With
End Sub
Private Sub Command1_Click()
ZoomPicture Picture1, 0.9 'shrink it
End Sub
Private Sub Command2_Click()
ZoomPicture Picture1, 1.1 'stretch it
End Sub