image getting blurred when enlarging picture box

2020-04-08 13:00发布

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();

标签: c# winforms
2条回答
Luminary・发光体
2楼-- · 2020-04-08 13:34

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.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!

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-04-08 13:43

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

image Scaling of picture box

查看更多
登录 后发表回答