PictureBox Zoom Problem

W

wasishincar

I'm trying to read a bitmap and zoom in/out it on screen.
I wrote some code but it did not perform as I expect. The bitmap shift
1 pixel to the upper-left coner when zooming in/out button was clicked.
Please kindly help me to check if anything wrong with my code. Many
many thanks.

using System.Drawing;
using System.Drawing.Drawing2D;

// Click Browse to open bitmap file
private void buttonBrowse_Click(object sender, EventArgs e)
{
if (DialogResult.OK ==
this.openFileDialogBmpLocation.ShowDialog())
{
myBitmap = new
Bitmap(this.openFileDialogBmpLocation.FileName);

this.pictureBoxBitmap.Image = myBitmap;
this.pictureBoxBitmap.Size = new Size(myBitmap.Width,
myBitmap.Height);
}
}

// Click Zoom In to resize the bitmap
private void buttonZoomIn_Click(object sender, EventArgs e)
{
if (this.pictureBoxBitmap.Image == null) return;
Size nSize = new Size( pictureBoxBitmap.Image.Width * 2,
pictureBoxBitmap.Image.Height * 2);
Image gdi = new Bitmap(nSize.Width, nSize.Height);

Graphics ZoomInGraphics = Graphics.FromImage(gdi);

ZoomInGraphics.InterpolationMode =
InterpolationMode.NearestNeighbor;
ZoomInGraphics.DrawImage(pictureBoxBitmap.Image, new Rectangle(new
Point(0, 0), nSize), new Rectangle(new Point(0, 0),
pictureBoxBitmap.Image.Size), GraphicsUnit.Pixel);
ZoomInGraphics.Dispose();

pictureBoxBitmap.Image = gdi;
pictureBoxBitmap.Size = gdi.Size;
}
 
A

Alex

Why you don't use simple methods?
Bitmap ZoomedBmp = new Bitmap(oldBmp, newWidth, newHeight);
 
W

wasishincar

Thank for your reply, but I think there is not much different between
my code and the Microsoft Example. We use the same methed to zoom the
image. I think the problem is from pictureBox. I try to set the padding
and margin to 0, but ths image still shift. Just dont know what's wrong
with the pictureBox. Is anyone know why?
 
W

wasishincar

I did not use this cuz I need to zoom it by nearest neighbor.
Thanks for your reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top