Windows Forms: How to zoom an image in C#
By FoxLearn 7/2/2017 8:55:36 PM 13.9K
How to zoom an image using PictureBox in C#
Step 1: Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "ImageZoom" and then click OK
Step 2: Design your form as below
Step 3: Add code to button click event handler as below
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ImageZoom { public partial class Form1 : Form { Image imgOriginal; public Form1() { InitializeComponent(); } private void btnOpen_Click(object sender, EventArgs e) { //Open image using (OpenFileDialog ofd = new OpenFileDialog() { Multiselect = false, ValidateNames = true, Filter = "JPEG|*.jpg" }) { if (ofd.ShowDialog() == DialogResult.OK) { pictureBox1.Image = Image.FromFile(ofd.FileName); imgOriginal = pictureBox1.Image; } } } //Zoom image file Image Zoom(Image img,Size size) { Bitmap bmp = new Bitmap(img, img.Width + (img.Width * size.Width / 100), img.Height + (img.Height * size.Height / 100)); Graphics g = Graphics.FromImage(bmp); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; return bmp; } private void trackBar1_Scroll(object sender, EventArgs e) { if (trackBar1.Value > 0) { pictureBox1.Image = Zoom(imgOriginal, new Size(trackBar1.Value, trackBar1.Value)); } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (pictureBox1.Image != null) pictureBox1.Dispose(); } } }
VIDEO TUTORIALS
- How to Send and Receive email in Microsoft Outlook using C#
- How to Print Windows Form in C#
- How to Use Form Load and Button click Event in C#
- How to use Advanced Filter DataGridView in C#
- How to use TagListControl in C#
- How to use Error Provider in C#
- How to Drag and Drop controls in C#
- How to Create a Random Password Generator in C#