locked
Visual c# 2010 Express code for zoom in & zoom out of an image RRS feed

  • Question

  • i want to zoom in & zoom out the image in paint as it is in M.S paint using Visual c# 2010 Express...plz help me..
    Tuesday, August 7, 2012 5:13 PM

Answers

  • Have you added the atribute "private Size Multiplier;" in your class? You can check my example.. its working!

    Here is the full class code:

     public partial class FormZoom : Form
        {
            private Size Multiplier;
    
            public FormZoom()
            {
                InitializeComponent();
            }
    
            public void ZoomIn()
            {
                Multiplier = new Size(2,2);
    
                Image MyImage = pictureBox1.Image;
    
                Bitmap MyBitMap = new Bitmap(MyImage, Convert.ToInt32(MyImage.Width * Multiplier.Width),
                    Convert.ToInt32(MyImage.Height * Multiplier.Height));
    
                Graphics Graphic = Graphics.FromImage(MyBitMap);
    
                Graphic.InterpolationMode = InterpolationMode.High ;
    
                pictureBox1.Image = MyBitMap;
    
            }
    
            public void ZoomOut()
            {
                Multiplier = new Size(2, 2);
    
                Image MyImage = pictureBox1.Image;
    
                Bitmap MyBitMap = new Bitmap(MyImage, Convert.ToInt32(MyImage.Width / Multiplier.Width),
                    Convert.ToInt32(MyImage.Height / Multiplier.Height));
    
                Graphics Graphic = Graphics.FromImage(MyBitMap);
    
                Graphic.InterpolationMode = InterpolationMode.High ;
    
                pictureBox1.Image = MyBitMap;
    
            }
    
    private void button1_Click(object sender, EventArgs e)
            {
                ZoomIn();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                ZoomOut();
            }
    }


    Rui Machado

    • Marked as answer by Dwkr Tuesday, August 7, 2012 7:40 PM
    Tuesday, August 7, 2012 7:32 PM
  • Hi Dwrk,

    I made something this morning that might help you. Check it, code gallery link

    
    
    
    
    
    
    

    Rui Machado

    • Marked as answer by Dwkr Wednesday, August 8, 2012 3:13 PM
    Wednesday, August 8, 2012 12:47 PM

All replies

  • You need to give more details about what you are doing, which frameworks you are using, etc.

    The Building Metro style apps with C# or VB  forum is definitely the wrong one for this question (VS 2010 cannot be used for such apps), but you don't provide enough information to know which forum would be relevant. The C# forum will be closer, but WinForms or WPF may be better.

    --Rob


    Tuesday, August 7, 2012 5:34 PM
  • Actually, i am doing project on paint application having some additional features using Microsoft visual c# 2010 Express...iwant to zoom in & zoom out the image which i have selected from a image file...i am using " picture box" get the image and "openfile dialog" from where i can select the image...the zoom in & zoom out functions should be same as it is in M.S Paint.
    Tuesday, August 7, 2012 5:52 PM
  • Hello Dwkr,

    You can try to use a multiplier to your Image, the algorythm is pretty simple, i will only show you how to enlarge your image, the algorythm to zoom out is the sae however you divide by the multiplier instead of multiplying...

    Here's the sample code:

    public void ZoomIn()
            {
                Multiplier = new Size(2,2);

                Image MyImage = pictureBox1.Image;

                Bitmap MyBitMap = new Bitmap(MyImage, Convert.ToInt32(MyImage.Width * Multiplier.Width),
                    Convert.ToInt32(MyImage.Height * Multiplier.Height));

                Graphics Graphic = Graphics.FromImage(MyBitMap);

                Graphic.InterpolationMode = InterpolationMode.High ;

                pictureBox1.Image = MyBitMap;

            }

    Here is a full sample for you to try it: https://skydrive.live.com/redir?resid=4463B7A42AF156FF!2941&authkey=!APepZtq9qAJ2y-k

    Hope it will help you

    Rui Machado

    rpmachado@wordpress.com


    • Edited by ruimachado Tuesday, August 7, 2012 6:33 PM
    • Proposed as answer by ruimachado Tuesday, August 7, 2012 7:06 PM
    Tuesday, August 7, 2012 6:32 PM
  • Sir, is there any reference is needed for "Multiplier" because  its giving error that " file does not exist in current context".
    Tuesday, August 7, 2012 6:55 PM
  • Dwkr,

    Yes, you need to add the reference to using System.Drawing.Drawing2D;

    when you have that kind of probs, just "CTRL+.(Dot)" and visual studio will help you with the references! :)


    Rui Machado

    Tuesday, August 7, 2012 7:08 PM
  • Sorry sir, i have already given using System.Drawing.Drawing2D; but still giving same error and when m pressing "CTRL+.(Dot)" its not giving any information..so what can i do now ..???
    Tuesday, August 7, 2012 7:20 PM
  • Have you added the atribute "private Size Multiplier;" in your class? You can check my example.. its working!

    Here is the full class code:

     public partial class FormZoom : Form
        {
            private Size Multiplier;
    
            public FormZoom()
            {
                InitializeComponent();
            }
    
            public void ZoomIn()
            {
                Multiplier = new Size(2,2);
    
                Image MyImage = pictureBox1.Image;
    
                Bitmap MyBitMap = new Bitmap(MyImage, Convert.ToInt32(MyImage.Width * Multiplier.Width),
                    Convert.ToInt32(MyImage.Height * Multiplier.Height));
    
                Graphics Graphic = Graphics.FromImage(MyBitMap);
    
                Graphic.InterpolationMode = InterpolationMode.High ;
    
                pictureBox1.Image = MyBitMap;
    
            }
    
            public void ZoomOut()
            {
                Multiplier = new Size(2, 2);
    
                Image MyImage = pictureBox1.Image;
    
                Bitmap MyBitMap = new Bitmap(MyImage, Convert.ToInt32(MyImage.Width / Multiplier.Width),
                    Convert.ToInt32(MyImage.Height / Multiplier.Height));
    
                Graphics Graphic = Graphics.FromImage(MyBitMap);
    
                Graphic.InterpolationMode = InterpolationMode.High ;
    
                pictureBox1.Image = MyBitMap;
    
            }
    
    private void button1_Click(object sender, EventArgs e)
            {
                ZoomIn();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                ZoomOut();
            }
    }


    Rui Machado

    • Marked as answer by Dwkr Tuesday, August 7, 2012 7:40 PM
    Tuesday, August 7, 2012 7:32 PM
  • Thank u so much sir..:-) )
    Tuesday, August 7, 2012 7:41 PM
  • You welcome Dwrk! :) im glad to help

    Rui Machado

    Tuesday, August 7, 2012 8:07 PM
  • Hi Sir,

    Title: Text on image using M.S Visual C# 2010 Express.

    As I am doing project on paint application, i want to write text on any image selected from a file using M.S Visual C# 2010 Express.
    Can u give me step by step codes for writing text on any image which i selected from image file and also i want to move the text which i have written, it should be similar as it is in M.S paint..??? Plz help me.
    Tuesday, August 7, 2012 8:10 PM
  • Hi Dwrk,

    I made something this morning that might help you. Check it, code gallery link

    
    
    
    
    
    
    

    Rui Machado

    • Marked as answer by Dwkr Wednesday, August 8, 2012 3:13 PM
    Wednesday, August 8, 2012 12:47 PM