Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi friends,
Now I am working in C #.net .i am looking for Picture Zoom In and Zoom Out in C#.Net. My Properties are Form,Picture Box and Tool strip .In that Tool strip ,3 buttons are in that Tool strip one is Zoom In ,another One is Zoom Out and Close.i insert a JPEG File in picture box.if i click Zoom In button.Image will Zoom In.and click Zoom Out button .Image will Zoom Out how to do this?Please I want a Code for these issue.please help me.
Regards,
Lakshmi Narayanan.S
Posted
Updated 7-Nov-21 6:08am
v2
Comments
Sergey Alexandrovich Kryukov 1-Jun-11 16:26pm    
There is not such thing as C#.NET.
--SA

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Image imgOriginal;

        private void Form1_Load(object sender, EventArgs e)
             {            
                             // set Slider Attributes
                               zoomSlider.Minimum = 1;
                                zoomSlider.Maximum = 5;
                                  zoomSlider.SmallChange = 1;
                                   zoomSlider.LargeChange = 1;
                                    zoomSlider.UseWaitCursor = false;
 
                   // reduce flickering
                 this.DoubleBuffered = true;
              }
        
        public Image PictureBoxZoom(Image img, Size size)
        {
            imgOriginal = Image.FromFile(openFileDialog1.FileName);
            Bitmap bm = new Bitmap(imgOriginal, Convert.ToInt32(imgOriginal.Width * size.Width), Convert.ToInt32(imgOriginal.Height * size.Height));
            Graphics grap = Graphics.FromImage(bm);
            //grap.InterpolationMode = InterpolationMode.HighQualityBicubic;
            return bm;
        }

        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));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            DialogResult res = MessageBox.Show("Message", "Try", MessageBoxButtons.YesNo);
            if (res == DialogResult.Yes)
            {
                string imagepath = openFileDialog1.FileName;
                picBox.ImageLocation = imagepath;
            }
            else
            {
                MessageBox.Show("picture cancelled", "message");
            }
        }

        }
    }
 
Share this answer
 
v2
For Zooming In :

pictureBox1.Top= (int)(pictureBox1.Top - (pictureBox1.Height * 0.025));
pictureBox1.Left = (int)(pictureBox1.Left - (pictureBox1.Width * 0.025));
pictureBox1.Height = (int)(pictureBox1.Height + (pictureBox1.Height* 0.05));
pictureBox1.Width = (int)(pictureBox1.Width + (pictureBox1.Width * 0.05));

For Zooming Out :

pictureBox1.Top = (int)(pictureBox1.Top + (pictureBox1.Height * 0.025));
pictureBox1.Left = (int)(pictureBox1.Left + (pictureBox1.Width * 0.025));
pictureBox1.Height = (int)(pictureBox1.Height - (pictureBox1.Height* 0.05));
pictureBox1.Width = (int)(pictureBox1.Width - (pictureBox1.Width * 0.05));
 
Share this answer
 
Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

There is an article here: PictureBox Zoom[^] - which you would have found, if you had tried even a basic google...
 
Share this answer
 
Comments
naraayanan 2-Jun-11 1:43am    
Thanks friend.In this example,using 2 picture box But i want to do this in one Picture box.Please help me.
regards,
Lakshmi narayanan.S
OriginalGriff 2-Jun-11 3:12am    
Read the code: the article shows the technique, not a specific solution to your problem: you are expected to do something for yourself! :laugh:
naraayanan 2-Jun-11 3:24am    
Hi,
I tried .But i didn't.So Please help me.
OriginalGriff 2-Jun-11 3:38am    
What part of it did you not understand?
naraayanan 2-Jun-11 7:39am    
Hi,
In that Program,there are 2 methods are used in the program.But i didn't understand 2 one(UpdateZoomedImage)
Regards,
Lakshmi Narayanan.S
Please help me
Zooming an image is far easier to control if you don't use a picture box. Again, google is your friend.
 
Share this answer
 
in VB.Net :
VB
Private Sub Zoom(ByVal factor As Double)
    Dim img_ As Image
    img_ = PictureBox1.Image
    Dim srcbtm As New Bitmap(img_)
    Dim destbtm As New Bitmap(CInt(srcbtm.Width * factor), _
        CInt(srcbtm.Height * factor))
    Dim destGraphic As Graphics = Graphics.FromImage(destbtm)

    destGraphic.DrawImage(srcbtm, 0, 0, destbtm.Width + 1, _
        destbtm.Height + 1)
    PictureBox1.Image = destbtm
End Sub
 
Share this answer
 
Comments
luisvaldez 5-Nov-12 7:32am    
This isn't a Zoom... this is just a resize...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900