I do have following problem when zooming into an image in a picturebox. After several times zooming in and out, the bmp image gets very unsharp. Does anyone konws how to solve this problem?
Here is the code:
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();
}
It does not matter when I change the interpolation mode! Thanks!
Without a good, minimal , complete code example that clearly shows your scenario, it's difficult if not impossible to know for sure what the best solution is.
However, in general the right way to approach this would be to configure the PictureBox
to scale the image according to its own size (ie set SizeMode
to SizeMode.Zoom
), and then resize the PictureBox
itself according to the desired zoom factor.
For example:
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.