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......

Dani AI

Generated

If this is WinForms (PictureBox), avoid repeatedly resampling the bitmap or drawing into the same image. Keep the original image once, store a scale factor, and render at the requested zoom in the PictureBox.Paint event. This preserves quality, avoids GDI+ leaks, and keeps the code simple. Also, base scaling on the image dimensions (not the control size), otherwise zoom depends on how large the PictureBox happens to be.

Example pattern:

private Image _original;
private float _scale = 1f; // 1.0 = 100%

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    if (_original == null) return;
    e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
    int w = (int)Math.Round(_original.Width * _scale);
    int h = (int)Math.Round(_original.Height * _scale);
    e.Graphics.DrawImage(_original, new Rectangle(0, 0, w, h));
}

private void SetZoom(float percent)
{
    _scale = percent / 100f; // e.g., 50, 100, 150, 200
    pictureBox1.Invalidate();
}

On image load, keep the source: _original = Image.FromFile(path); pictureBox1.SizeMode = PictureBoxSizeMode.Normal;. On menu clicks, call SetZoom(150f); etc. This renders with high-quality scaling each time without mutating the bitmap. If you prefer no custom painting, an alternative is pictureBox1.SizeMode = PictureBoxSizeMode.Zoom and resizing the PictureBox to _original.Size * _scale (often inside an AutoScroll panel).

See Microsoft docs for details: Graphics.DrawImage, InterpolationMode, and PictureBox.SizeMode. Note: PictureBox is not available in ASP.NET; the above applies to WinForms.

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.