Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

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

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 what I want.

public Image CropToCircle(Image srcImage, PointF center, float radius, Color backGround)
{
    Image dstImage = new Bitmap((int)Math.Round(Math.Ceiling(radius*2)), (int)Math.Round(Math.Ceiling(radius*2)), srcImage.PixelFormat);

    using (Graphics g = Graphics.FromImage(dstImage))
    {
        RectangleF r = new RectangleF(center.X - radius, center.Y - radius, 2*radius, 2 * radius);

        using (Brush br = new SolidBrush(backGround))
        {
            g.FillRectangle(br, 0, 0, dstImage.Width, dstImage.Height);
        }

        GraphicsPath path = new GraphicsPath();
        path.AddEllipse(r);
        g.SetClip(path);
        g.DrawImage(srcImage, 0, 0);

        return dstImage;
    }
}

dstImage - should show the cropped image from main image at the given cursor position.

Above code works fine but output image location moves with X,Y coordinates. What I want is always show 100x100 square image from main image under the cursor position. (Like a lens moving on the image)

This is how I call the function

private void drawWindows(Point mousePoint)
{               
    Image RoundedImage = CropToCircle(StartImage, new PointF(mousePoint.X, mousePoint.Y), 75, Color.FromArgb(0, 101, 167));
    PB.Image  = RoundedImage;    
}

I want to show the image under given location at the center of the image as follows:

enter image description here

But currently cropped image moves inside when I change the X,Y cordinates. I want the circular image to still be at center.

enter image description here

Where am I making the mistake? I feel like g.DrawImage(srcImage, 0, 0) is likely the culprit.
Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

Image Lens preview

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...