How to crop a section of an Image at cursor position?

前端 未结 1 1079
别那么骄傲
别那么骄傲 2020-11-30 15:35

I am following the link to get the image cropped and rounded. However it does not work the way I want. I spent some time but did not understand where to fix the code to get

相关标签:
1条回答
  • 2020-11-30 16:12

    When you use a Control as the container for an Image and the Image is scaled to fit the container's bounds (e.g., setting a PictureBox.SizeMode to PictureBoxSizeMode.Zoom) so an Image can be shown in the UI with predefined measures, when you need to select a section of the Image, you need to calculate the scale factor. In other words, determine the ratio between the container's size and the Image real size.

    It may be better to use the smaller container as reference, so you can then multiply instead of divide the relative measures by the scale ratio:

    private float GetImageScaledRatio(RectangleF canvas, SizeF imageSize)
    {
        return Math.Max(canvas.Width, canvas.Height) /
               Math.Max(imageSize.Width, imageSize.Height);
    }
    

    The position of the Lens inside the container - if you want the lens to follow the Mouse pointer's position - are give by the Pointer coordinates minus half of the lens size:

    private PointF GetLensPosition(PointF centerPosition, RectangleF lens)
    {
        return new PointF(centerPosition.X - (lens.Width / 2), 
                          centerPosition.Y - (lens.Height / 2));
    }
    

    To determine the actual size of the Lens (the selection) dimension in relation to the actual size of the Bitmap, the Lens dimension must be scaled when a section of a Bitmap needs to be drawn or otherwise clipped:

    private SizeF GetScaledLensSize(RectangleF canvas, SizeF imageSize, SizeF lensSize)
    {
        float scaleRatio = GetImageScaledRatio(canvas, imageSize);
        return new SizeF(lensSize.Width * scaleRatio, lensSize.Width * scaleRatio);
    }
    

    Also, when showing the preview of the current selection represent by the Lens, the selection needs to be scaled to the size of the Container used to preview the Lens' selection:

    private RectangleF CanvasToImageRect(RectangleF canvas, SizeF imageSize, RectangleF rect)
    {
        float scaleRatio = GetImageScaledRatio(canvas, imageSize);
        return new RectangleF(new PointF(rect.X / scaleRatio, rect.Y / scaleRatio),
                              new SizeF(rect.Width / scaleRatio, rect.Height / scaleRatio));
    }
    

    These simple methods allows to calculate both the actual size of a selection in relation to the Image considered and also the size of Controls used for the preview.

    When drawing the preview using the Lens selection, it can be a good idea to use a common method to draw the Image section: a method that can also be used to draw the selection in a new Bitmap, which can then be saved to disc or otherwise stored.

    Here, pctLens is the PictureBox used for the preview, RectangleF section is the Lens measure rescaled to the pctLens size (for the preview) and of course sourceImage is the original Image:

    private void pctLens_Paint(object sender, PaintEventArgs e)
    {
        RectangleF section = CanvasToImageRect(pctOriginal.ClientRectangle, sourceImage.Size, imageLens);
        DrawImageSelection(e.Graphics, pctLens.ClientRectangle, section, sourceImage);
    }
    
    private void DrawImageSelection(Graphics g, RectangleF canvas, RectangleF imageSection, Image image)
    {
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage(image, canvas, imageSection, GraphicsUnit.Pixel);
    
        switch (lensType)
        {
            case LensType.Circular:
                using (var path = new GraphicsPath())
                {
                    path.AddEllipse(canvas);
                    g.SetClip(path, CombineMode.Exclude);
                    using (var brush = new SolidBrush(Color.FromArgb(160, Color.Black)))
                    {
                        g.FillRectangle(brush, canvas);
                        g.ResetClip();
                        using (var pen = new Pen(brush, 1f))
                            g.DrawEllipse(pen, canvas);
                    }
                }
                break;
            case LensType.Rectangular:
                // NOP
                break;
        }
    }
    

    Visual result (Image: 1200x675, PictureBox: 300x175, SizeMode: Zoom)

    Complete source code to reproduce what is shown in the animation:

    Bitmap sourceImage is the original Bitmap, it must be set to an existing object.
    RectangleF imageLens is the shape used to define the relative Lens size.
    Size lensPixelSize is the size of imageLens in Pixels, relative to the UI representation.
    pctOriginal is the PictureBox where the original Image is shown.
    pctLens is the PictureBox where the Lens section preview is drawn.

    Bitmap sourceImage = null;
    RectangleF imageLens = RectangleF.Empty;
    Size lensPixelSize = new Size(100, 100);
    LensType lensType = LensType.Circular;
    bool lensUseRelativeSize = false;
    bool drawLens = false;
    
    private enum LensType
    {
        Circular,
        Rectangular
    }
    
    private void pctOriginal_MouseMove(object sender, MouseEventArgs e)
    {
        imageLens.Location = GetLensPosition(e.Location, imageLens);
        imageLens.Size = lensUseRelativeSize 
                       ? GetScaledLensSize(pctOriginal.ClientRectangle, sourceImage.Size, lensPixelSize)
                       : lensPixelSize;
        pctOriginal.Invalidate();
        pctLens.Invalidate();
    }
    
    private PointF GetLensPosition(PointF centerPosition, RectangleF rect)
    {
        return new PointF(centerPosition.X - (rect.Width / 2), 
                          centerPosition.Y - (rect.Height / 2));
    }
    
    private SizeF GetScaledLensSize(RectangleF canvas, SizeF imageSize, SizeF lensSize)
    {
        float scaleRatio = GetImageScaledRatio(canvas, imageSize);
        return new SizeF(lensSize.Width * scaleRatio, lensSize.Width * scaleRatio);
    }
    
    private float GetImageScaledRatio(RectangleF canvas, SizeF imageSize)
    {
        return Math.Max(canvas.Width, canvas.Height) /
               Math.Max(imageSize.Width, imageSize.Height);
    }
    
    private RectangleF CanvasToImageRect(RectangleF canvas, SizeF imageSize, RectangleF rect)
    {
        float scaleRatio = GetImageScaledRatio(canvas, imageSize);
        return new RectangleF(new PointF(rect.X / scaleRatio, rect.Y / scaleRatio),
                              new SizeF(rect.Width / scaleRatio, rect.Height / scaleRatio));
    }
    
    
    private void pctOriginal_Paint(object sender, PaintEventArgs e)
    {
        using (Pen pen = new Pen(Color.Red, 2.0f))
        {
            pen.DashStyle = DashStyle.Dash;
            switch (lensType)
            {
                case LensType.Circular:
                    e.Graphics.DrawEllipse(pen, Rectangle.Round(imageLens));
                    break;
                case LensType.Rectangular:
                    e.Graphics.DrawRectangle(pen, Rectangle.Round(imageLens));
                    break;
            }
        }
    }
    
    private void pctLens_Paint(object sender, PaintEventArgs e)
    {
        if (!drawLens) return;
        RectangleF section = CanvasToImageRect(pctOriginal.ClientRectangle, sourceImage.Size, imageLens);
        DrawImageSelection(e.Graphics, pctLens.ClientRectangle, section, sourceImage);
    }
    
    private void DrawImageSelection(Graphics g, RectangleF canvas, RectangleF imageSection, Image image)
    {
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage(image, canvas, imageSection, GraphicsUnit.Pixel);
    
        switch (lensType)
        {
            case LensType.Circular:
                using (var path = new GraphicsPath())
                {
                    path.AddEllipse(canvas);
                    g.SetClip(path, CombineMode.Exclude);
                    using (var brush = new SolidBrush(Color.FromArgb(160, Color.Black)))
                    {
                        g.FillRectangle(brush, canvas);
                        g.ResetClip();
                        using (var pen = new Pen(brush, 1f))
                            g.DrawEllipse(pen, canvas);
                    }
                }
                break;
            case LensType.Rectangular:
                // NOP
                break;
        }
    }
    
    private void chkSizeRelative_CheckedChanged(object sender, EventArgs e) 
        => lensUseRelativeSize = chkSizeRelative.Checked;
    
    private void radLensType_CheckedChanged(object sender, EventArgs e) 
        => lensType = (LensType)(int.Parse((sender as Control).Tag.ToString()));
    
    private void pctOriginal_MouseEnter(object sender, EventArgs e) 
        => drawLens = true;
    
    private void pctOriginal_MouseLeave(object sender, EventArgs e)
    {
        drawLens = false;
        pctLens.Invalidate();
    }
    
    0 讨论(0)
提交回复
热议问题