Windows Forms: How to zoom an image in C#
By FoxLearn 7/2/2017 8:55:36 PM 14.13K
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 save files using SaveFileDialog in C#
- How to make an Alarm clock with sound in C#
- How to Display Images in DataGridView in C#
- How to Print DataGridView with Header & Footer with Landscape in C#
- How to Create a custom Progress Bar with Percentage in C#
- How to read an image file in C#
- How to use BackgroundWorker in C#
- How to protect .NET code from reverse engineering
Categories
Popular Posts
Flat Able Admin Dashboard Template
11/18/2024
Regal Admin Dashboard Template
11/18/2024
Plus Admin Dashboard Template
11/18/2024
Admin Tailwind CSS Admin Dashboard Template
11/18/2024