Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

image getting blurred when enlarging picture box

Tags:

c#

winforms

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();
like image 737
Siddhi Dave Avatar asked Dec 24 '22 08:12

Siddhi Dave


1 Answers

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!

like image 125
TaW Avatar answered Jan 13 '23 04:01

TaW