如何在不更改图像清晰度的情况下放大图片框图像



在放大图片框中的图像时,我确实遇到了以下问题。放大和缩小几次后,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();
    }

当我更改插值模式时,这并不重要!谢谢!

如果没有一个好的、最小的完整的代码示例来清楚地展示你的方案,就很难(如果不是不可能的话)确定最佳解决方案是什么。

但是,一般来说,解决此问题的正确方法是配置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);
   }
}  

最新更新