Hi,

Have resize the image to fit the picture box (for other purpose). now i tried to zoom in the image and i succeed on it. but my problem is when i zoom the image the image get zoomed and image moves toward right.

How can zoom the image without changing the position of the image.

Thanks in Advance....

//sz1 = (800, 600)
private Image resizeimage(Image my, Size sz1)
{
double ratio = 0d;
double myThumbWidth = 0d;
double myThumbHeight = 0d;
Bitmap bp;
int c;
int d;

if ((my.Width / Convert.ToDouble(sz1.Width)) > (my.Height /
Convert.ToDouble(sz1.Height)))
ratio = Convert.ToDouble(my.Width) / Convert.ToDouble(sz1.Width);
else ratio = Convert.ToDouble(my.Height) / Convert.ToDouble(sz1.Height);


myThumbHeight = Math.Ceiling(my.Height / ratio);
myThumbWidth = Math.Ceiling(my.Width / ratio);
Size thumbSize = new Size((int)myThumbWidth, (int)myThumbHeight);
bp = new Bitmap(sz1.Width, sz1.Height);
c = (sz1.Width - thumbSize.Width) / 2;
d = (sz1.Height - thumbSize.Height);
System.Drawing.Graphics g = Graphics.FromImage(bp);
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
rect = new Rectangle(c, d, thumbSize.Width, thumbSize.Height);
g.DrawImage(my, rect, 0, 0, my.Width, my.Height, GraphicsUnit.Pixel);
if (f == false)
{
sz = bp.Size;
pictureBox1.Size = sz;
}
rect = new Rectangle(0, 0, 0, 0);
return (bp);
// this will trigged when clicked on the picture box
public void ZoomIn()
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
if (sz.Width >=800* 4)
MessageBox.Show("Max ZoomIn");
else {
sz.Width += 100;
sz.Height += 100;

pictureBox1.Size = sz;
pictureBox1.Invalidate();
}

Dani AI

Generated

Short answer: the image “moves” because the PictureBox grows from its top‑left corner. Preserve the image position by keeping the same center (or the same point under the mouse) when you change the control size.

A simple, reliable approach is: capture the PictureBox center, change Size, then reposition so the center stays where it was.

void ZoomBy(int delta)
{
    var center = new Point(pictureBox1.Left + pictureBox1.Width / 2,
                           pictureBox1.Top  + pictureBox1.Height / 2);

    var newWidth  = Math.Max(10, pictureBox1.Width + delta);
    var newHeight = Math.Max(10, pictureBox1.Height + delta);

    pictureBox1.Size = new Size(newWidth, newHeight);
    pictureBox1.Left = center.X - newWidth / 2;
    pictureBox1.Top  = center.Y - newHeight / 2;
}

If you want zoom to follow the mouse pointer instead of the control center, compute the mouse-relative fraction before resizing and restore that point after resize:

void ZoomAt(Point mouseClient, float factor)
{
    float rx = (mouseClient.X - pictureBox1.Left) / (float)pictureBox1.Width;
    float ry = (mouseClient.Y - pictureBox1.Top)  / (float)pictureBox1.Height;

    int nw = (int)(pictureBox1.Width * factor);
    int nh = (int)(pictureBox1.Height * factor);
    pictureBox1.Size = new Size(nw, nh);

    pictureBox1.Left = (int)(mouseClient.X - rx * nw);
    pictureBox1.Top  = (int)(mouseClient.Y - ry * nh);
}

Longer‑term alternative: keep the control fixed and draw the scaled image yourself (OnPaint) using Translate/Scale or DrawImage. That gives smoother quality control, easier cursor‑centered zooming, and avoids layout surprises. Also: clamp sizes to sensible limits, respect the parent container bounds or use a scrollable Panel, and set InterpolationMode for good rendering. ’s Inflate trick is a neat shortcut for keeping the rectangle centered; the center-preserve math above is the explicit version that makes mouse-centred zoom straightforward for .

Recommended Answers

All 4 Replies

Your ZoomIn method only changes the size of the PictureBox, it does not changes its Location. Therefore the top left corner of the PictureBox remains where it is and the right and bottom move by the change in size.
You need to adjust the Location property of the PictureBox (by negative half the change in size) to re-center the image.

I tried by adjusting the picture box location as below but it's not works. how to adjust it.

pictureBox1.Location = new Point(0,pictureBox1.Top - 5);

Minus 5 is a small amount to test with. Try a bigger number.
Also, I think using Point.Offset like this will help;

Point pt = pictureBox1.Location;
pt.Offset(0, -50);
pictureBox1.Location = pt;

I discovered that if you use Inflate on a rectangle then it maintains its relative center position automatically so;

Rectangle rec = pictureBox1.Bounds;
rec.Inflate(50, 50);
pictureBox1.Bounds = rec;

This does the resize and automatically re-centers the pircturebox for you.

commented: nice...hadn't tried inflate before :) +1
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.