|
Emulate Image Zooming in a PictureBox Control at Runtime
|
|
Rating: 20 user(s) have rated this article
Posted by: Suprotim Agarwal,
on 9/1/2008,
in category "Windows Forms 2.0"
Views: this article has been read 14340 times
Abstract: Zooming images in a PictureBox control is not something that comes out of the box. However with a little bit of code, basic emulation of zoom in and zoom out functionality can be achieved quiet easily. In this article, we will explore how to emulate zoom-in and zoom-out, of an image kept in the PictureBox control.
Emulate Image Zooming in a PictureBox Control at Runtime
Zooming images in a PictureBox control is not something that comes out of the box. However with a little bit of code, basic emulation of zoom in and zoom out functionality can be achieved quiet easily. In this article, we will explore how to emulate zoom in and zoom out, on an image kept in the PictureBox control. Please follow these steps:
Step 1: Open Visual Studio > File > New > Project. In the Project Types pane, choose the language of your choice (Visual C# or Visual Basic). In the Templates pane, choose Windows Application. Choose a name and location for the project and click OK.
Step 2: Drag and drop the PictureBox(picBox) and the TrackBar(zoomSlider) controls on to the form. We will be using the Trackbar control to scroll and configure the zoom value.
Now declare a class level variable of the type Image. Doing this will help us re-apply the transformations (zoom in zoom out) on the original image rather than the already transformed image.
C#
private Image imgOriginal;
VB.NET
Private imgOriginal As Image
In the Form1_Load, set the following properties on the two controls. Here we set the image location using the Image.FromFile. You can also use the Resources class to set images in your application:
C#
private void Form1_Load(object sender, EventArgs e)
{
// set image location
imgOriginal = Image.FromFile(@"C:\New Folder\picture1.jpg");
picBox.Image = imgOriginal;
// set Picture Box Attributes
picBox.BackgroundImageLayout = ImageLayout.Stretch;
// set Slider Attributes
zoomSlider.Minimum = 1;
zoomSlider.Maximum = 5;
zoomSlider.SmallChange = 1;
zoomSlider.LargeChange = 1;
zoomSlider.UseWaitCursor = false;
// reduce flickering
this.DoubleBuffered = true;
}
VB.NET
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
' set image location
imgOriginal = Image.FromFile("C:\New Folder\picture1.jpg")
picBox.Image = imgOriginal
' set Picture Box Attributes
picBox.BackgroundImageLayout = ImageLayout.Stretch
' set Slider Attributes
zoomSlider.Minimum = 1
zoomSlider.Maximum = 5
zoomSlider.SmallChange = 1
zoomSlider.LargeChange = 1
zoomSlider.UseWaitCursor = False
' reduce flickering
Me.DoubleBuffered = True
End Sub
Step 3: Let us now create our zoom functionality.
C#
public Image PictureBoxZoom(Image img, Size size)
{
Bitmap bm = new Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height));
Graphics grap = Graphics.FromImage(bm);
grap.InterpolationMode = InterpolationMode.HighQualityBicubic;
return bm;
}
VB.NET
Public Function PictureBoxZoom(ByVal img As Image, ByVal size As Size) As Image
Dim bm As Bitmap = New Bitmap(img, Convert.ToInt32(img.Width * size.Width), Convert.ToInt32(img.Height * size.Height))
Dim grap As Graphics = Graphics.FromImage(bm)
grap.InterpolationMode = InterpolationMode.HighQualityBicubic
Return bm
End Function
In the code above, we create a new instance of the Bitmap class using the image passed to the function. This image is scaled to the specific size and is taken into a Graphics object where the image is prefiltered using InterpolationMode.HighQualityBicubic to ensure high quality transformed images. The function then returns the transformed image back to the caller.
Step 4: The final step is to register the ‘Scroll’ event of the TrackBar(zoomSlider) control and call the zoom function as shown below:
C#
private void zoomSlider_Scroll(object sender, EventArgs e)
{
if (zoomSlider.Value > 0)
{
picBox.Image = null;
picBox.Image = PictureBoxZoom(imgOriginal, new Size(zoomSlider.Value, zoomSlider.Value));
}
}
VB.NET
Private Sub zoomSlider_Scroll(ByVal sender As Object, ByVal e As EventArgs)
If zoomSlider.Value > 0 Then
picBox.Image = Nothing
picBox.Image = PictureBoxZoom(imgOriginal, New Size(zoomSlider.Value, zoomSlider.Value))
End If
End Sub
That’s it. With a few lines of code, you can emulate zoom functionality in your picturebox and use it in your applications. I hope you liked the article and I thank you for viewing it.If you liked the article,
Subscribe to my RSS Feed.