Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

c# - image getting blurred when enlarging picture box

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...