6

I am developing an application for image processing. To zoom the image, I enlarge PictureBox. But after enlarging I get below image as result.

Application Output Image

But I want result like below image

enter image description here

Here is my Code :

      picturebox1.Size = new Size((int)(height * zoomfactor), (int) 
      (width* zoomfactor));
      this.picturebox1.Refresh();
TaW
  • 53,122
  • 8
  • 69
  • 111
Siddhi Dave
  • 67
  • 1
  • 8
  • 2
    PictureBox uses InterpolationMode.Bilinear, you want NearestNeighbor. So you'll have to do this yourself. Google "c# resize bitmap" for basic hits. – Hans Passant Mar 29 '18 at 12:14
  • It could be the image type that is the issue. If you expand the image then you are reducing the compression quality. Maybe this link might help https://stackoverflow.com/questions/12646287/image-scaling-of-picture-box – Jack Henry Mar 29 '18 at 11:34

1 Answers1

11

The PictureBox by itself will always create a nice and smooth version.

To create the effect you want you need to draw zoomed versions yourself. In doing this you need to set the

 Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;

Then no blurring will happen..

Example:

enter image description here

private void trackBar1_Scroll(object sender, EventArgs e)
{
    Bitmap bmp = (Bitmap)pictureBox1.Image;
    Size sz = bmp.Size;
    Bitmap zoomed = (Bitmap)pictureBox2.Image;
    if (zoomed != null) zoomed.Dispose();

    float zoom = (float)(trackBar1.Value / 4f + 1);
    zoomed = new Bitmap((int)(sz.Width * zoom), (int)(sz.Height * zoom));

    using (Graphics g = Graphics.FromImage(zoomed))
    {
      if (cbx_interpol.Checked) g.InterpolationMode = InterpolationMode.NearestNeighbor;
      g.PixelOffsetMode = PixelOffsetMode.Half;

      g.DrawImage(bmp, new Rectangle( Point.Empty, zoomed.Size) );
    }
    pictureBox2.Image = zoomed;
}

Of course you need to avoid setting the PBox to Sizemode Zoom or Stretch!

Danvil
  • 22,240
  • 19
  • 65
  • 88
TaW
  • 53,122
  • 8
  • 69
  • 111