放大图片框时图像变得模糊
问题描述:
我正在开发图像处理应用程序.要缩放图像,请放大PictureBox.但是放大之后,我得到了下面的图像.
I am developing an application for image processing. To zoom the image, I enlarge PictureBox. But after enlarging I get below image as result.
但是我想要下图所示的结果
But I want result like below image
这是我的代码:
picturebox1.Size = new Size((int)(height * zoomfactor), (int)
(width* zoomfactor));
this.picturebox1.Refresh();
答
PictureBox本身将始终创建漂亮且流畅的版本.
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;
然后就不会模糊了.
示例:
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;
}
当然,您需要避免将PBox设置为Sizemode Zoom或Stretch!
Of course you need to avoid setting the PBox to Sizemode Zoom or Stretch!