PictureBox Zoom Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wasishincar

    PictureBox Zoom Problem

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

  • chanmm

    #2
    Re: PictureBox Zoom Problem

    Try this:


    chanmm

    "wasishinca r" <adam.iem92g@gm ail.comwrote in message
    news:1161836974 .489598.54250@b 28g2000cwb.goog legroups.com...
    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;
    }
    >

    Comment

    • Alex

      #3
      Re: PictureBox Zoom Problem

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

      Comment

      • wasishincar

        #4
        Re: PictureBox Zoom Problem

        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?

        chanmm wrote:
        Try this:

        >
        chanmm
        >
        "wasishinca r" <adam.iem92g@gm ail.comwrote in message
        news:1161836974 .489598.54250@b 28g2000cwb.goog legroups.com...
        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;
        }

        Comment

        • wasishincar

          #5
          Re: PictureBox Zoom Problem

          I did not use this cuz I need to zoom it by nearest neighbor.
          Thanks for your reply.
          Alex wrote:
          Why you don't use simple methods?
          Bitmap ZoomedBmp = new Bitmap(oldBmp, newWidth, newHeight);

          Comment

          Working...