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;
}
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;
}