0

The purpose: In a picturebox I want to use wheel to zoom in or zoom out the picture. The method I get the image is to read the picturebox's image(like picture 1) I use the Cubic interpolation method to zoom the picture. As I zoom in and out many times, the picture would be very blurry(Because many times Cubic interpolation). So how can I solve the problem?

 void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
    {

        //Console.WriteLine("hhh");
        int i = e.Delta * SystemInformation.MouseWheelScrollLines ;
        System.Drawing.Point p = e.Location;
        // get picturebox image
        Mat OriginPictureMat = new Mat();
        Bitmap bit_image = new Bitmap(pictureBox1.Image);
        OriginPictureMat = ConvertFile.BitmapToMat(bit_image);
        /// 

        /// Zoom the picture
        Mat ZoomPictureMat = new Mat();
        OpenCvSharp.Size ZoomDsize = new OpenCvSharp.Size();
        int diff_origincenter_pointX = p.X  ; 
        int diff_origincenter_pointY = p.Y ;
        // zoom scale
        float scale_float = 1 + i / 3600f;
        int ZoomWidth = (int)Convert.ToInt32(OriginPictureMat.Width * scale_float);
        int ZoomHeight = (int)Convert.ToInt32(OriginPictureMat.Height * scale_float);
        if(ZoomHeight<OriginPictureMat.Height || ZoomWidth < OriginPictureMat.Width)
        {
            ZoomDsize = new OpenCvSharp.Size(OriginPictureMat.Width, OriginPictureMat.Height);
        }
        else
        {
            ZoomDsize =  new OpenCvSharp.Size(ZoomWidth, ZoomHeight);
        }

        Cv2.Resize(OriginPictureMat, ZoomPictureMat, ZoomDsize, 0, 0, InterpolationFlags.Cubic);

        //get the center point 

        Rect rect = new Rect((int)(scale_float * p.X) - diff_origincenter_pointX, (int)(scale_float * p.Y) - diff_origincenter_pointY, OriginPictureMat.Width, OriginPictureMat.Height);
        Mat matfinal = new Mat(ZoomPictureMat,rect);
        Bitmap bitmapFinal = ConvertFile.MatToBitmap(matfinal);
        pictureBox1.Image = bitmapFinal;
        matfinal = null;
        OriginPictureMat = null;
        ZoomPictureMat = null;
        GC.Collect();





    }
Lan
  • 1
  • Don't use the `PictureBox.Image` as the source of your interpolation, that's already interpolated (i.e., don't do that more than once, unless it's a something you actually want - not here). Assign that object to a Field and work on a copy of it, to show the results in your PictureBox. – Jimi Jan 26 '22 at 15:59
  • An example here (GDI+) that uses 4 interpolation methods to zoom an Image: [Zoom and translate an Image from the mouse location](https://stackoverflow.com/a/61964222/7444103). See what object (`drawingImage`) the interpolation are applied to. – Jimi Jan 26 '22 at 16:07

0 Answers0