I have a project on image processing. I have a picturebox with an image loaded in it via a openfiledialog box.

I was trying to zoom an image in the picturebox by certain pecentage i.e 50%, 100% 150% and 200% . All this is done when a user clicks on a menutool strip option.

I have the following code but it is not working:

private void toolStripMenuItem2_Click(object sender, EventArgs e) //coding for 50% zoom
    {           
        if (img != null)
        {
            Graphics g = Graphics.FromImage(img);
            int zoom=50/100;
            int newwidth = Convert.ToInt32(pictureBox1.Width * zoom);
            int newheight = Convert.ToInt32(pictureBox1.Height * zoom);
            Rectangle r = new Rectangle(this.AutoScrollPosition.X,this.AutoScrollPosition.Y,newwidth, newheight);
           // pictureBox1.Invalidate();
            g.DrawImage(img, r);
            pictureBox1.Image = img;
        }
    }

Please help......

Recommended Answers

All 4 Replies

Not sure if this is your only problem, but I noticed this line straight away:

int zoom=50/100;

So zoom = 0 which means that newWidth and newHeight both equal 0 too, so you are trying to create a Rectangle 0x0 and draw the image in that space. You see what I'm getting at?

double zoom = 0.5;
// or double zoom = 50.0/100.0;

is a better way to do it, but I would suggest allowing this value to be input by the user?

Quoted Text Here

Not sure if this is your only problem, but I noticed this line straight away:

int zoom=50/100;
So zoom = 0 which means that newWidth and newHeight both equal 0 too, so you are trying to create a Rectangle 0x0 and draw the image in that space. You see what I'm getting at?

double zoom = 0.5;
// or double zoom = 50.0/100.0;
is a better way to do it, but I would suggest allowing this value to be input by the user?

I see your point but changing to double still does not help. I cannot accept it from user I have a menustrip with zoom>50%

100%
150%
And so on. So zooming takes place on click

hey guys I have solved the problem
Here is the code:

 //This is the code for 150% zoom the same goes for 50% and 200% except change in zoom
 private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            if (img != null)
            {
                double zoom = 150.0/100.0;
                Bitmap bmp = new Bitmap(img, Convert.ToInt32(pictureBox1.Width * zoom), Convert.ToInt32(pictureBox1.Height * zoom));
                Graphics g = Graphics.FromImage(bmp);
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                pictureBox1.Image = bmp;
                img = pictureBox1.Image;
            }
        }

All thanks to Mitja Bonca

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.