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_Cl ick(object sender, EventArgs e)
{
if (DialogResult.O K ==
this.openFileDi alogBmpLocation .ShowDialog())
{
myBitmap = new
Bitmap(this.ope nFileDialogBmpL ocation.FileNam e);
this.pictureBox Bitmap.Image = myBitmap;
this.pictureBox Bitmap.Size = new Size(myBitmap.W idth,
myBitmap.Height );
}
}
// Click Zoom In to resize the bitmap
private void buttonZoomIn_Cl ick(object sender, EventArgs e)
{
if (this.pictureBo xBitmap.Image == null) return;
Size nSize = new Size( pictureBoxBitma p.Image.Width * 2,
pictureBoxBitma p.Image.Height * 2);
Image gdi = new Bitmap(nSize.Wi dth, nSize.Height);
Graphics ZoomInGraphics = Graphics.FromIm age(gdi);
ZoomInGraphics. InterpolationMo de =
InterpolationMo de.NearestNeigh bor;
ZoomInGraphics. DrawImage(pictu reBoxBitmap.Ima ge, new Rectangle(new
Point(0, 0), nSize), new Rectangle(new Point(0, 0),
pictureBoxBitma p.Image.Size), GraphicsUnit.Pi xel);
ZoomInGraphics. Dispose();
pictureBoxBitma p.Image = gdi;
pictureBoxBitma p.Size = gdi.Size;
}
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_Cl ick(object sender, EventArgs e)
{
if (DialogResult.O K ==
this.openFileDi alogBmpLocation .ShowDialog())
{
myBitmap = new
Bitmap(this.ope nFileDialogBmpL ocation.FileNam e);
this.pictureBox Bitmap.Image = myBitmap;
this.pictureBox Bitmap.Size = new Size(myBitmap.W idth,
myBitmap.Height );
}
}
// Click Zoom In to resize the bitmap
private void buttonZoomIn_Cl ick(object sender, EventArgs e)
{
if (this.pictureBo xBitmap.Image == null) return;
Size nSize = new Size( pictureBoxBitma p.Image.Width * 2,
pictureBoxBitma p.Image.Height * 2);
Image gdi = new Bitmap(nSize.Wi dth, nSize.Height);
Graphics ZoomInGraphics = Graphics.FromIm age(gdi);
ZoomInGraphics. InterpolationMo de =
InterpolationMo de.NearestNeigh bor;
ZoomInGraphics. DrawImage(pictu reBoxBitmap.Ima ge, new Rectangle(new
Point(0, 0), nSize), new Rectangle(new Point(0, 0),
pictureBoxBitma p.Image.Size), GraphicsUnit.Pi xel);
ZoomInGraphics. Dispose();
pictureBoxBitma p.Image = gdi;
pictureBoxBitma p.Size = gdi.Size;
}
Comment