当放大图片框中的图像时,我确实遇到以下问题。放大和缩小几次后,bmp图像变得非常不清晰。有没有人知道如何解决这个问题?
以下是代码:
public Form1()
{
InitializeComponent();
pictureBox1.MouseEnter += new EventHandler(pictureBox1_MouseEnter);
pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
//Set the SizeMode to center the image.
this.pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
}
private double zoom = 1.0;
void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
if (pictureBox1.Image != null)
{
if (e.Delta < 0)
{
zoom = zoom * 1.05;
}
else
{
if (zoom != 1.0)
{
zoom = zoom / 1.05;
}
}
txttextBox1.Text = zoom.ToString();
Bitmap bmp = new Bitmap(pictureBox1.Image, Convert.ToInt32(pictureBox1.Width * zoom), Convert.ToInt32(pictureBox1.Height * zoom));
Graphics g = Graphics.FromImage(bmp);
g.InterpolationMode = InterpolationMode.Default;
pictureBox1.Image = bmp;
}
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
pictureBox1.Focus();
}
更改插补模式时无关紧要! 谢谢!
答案 0 :(得分:0)
如果没有明确显示您的方案的a good, minimal, complete code example,那么很难确定最佳解决方案是什么,如果不是不可能的话。
然而,一般来说,正确的方法是配置PictureBox
以根据自己的大小(即将SizeMode
设置为SizeMode.Zoom
)来缩放图像,然后根据所需的缩放系数调整PictureBox
本身的大小。
例如:
void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
{
if (pictureBox1.Image != null)
{
if (e.Delta < 0)
{
zoom = zoom * 1.05;
}
else
{
if (zoom != 1.0)
{
zoom = zoom / 1.05;
}
}
txttextBox1.Text = zoom.ToString();
pictureBox1.Width = (int)Math.Round(pictureBox1.Image.Width * zoom);
pictureBox1.Height = (int)Math.Round(pictureBox1.Image.Height * zoom);
}
}