开发者

How to do Free hand Image Cropping in C# window application?

How to do Free hand Image Cropping in C# window ap开发者_运维知识库plication??


Okay, you provided very small amount of information, but I'll assume that you are using winforms. There are some tasks dealing with freehand-technique such as:

  • Drawing
  • Drag-n-dropping
  • Cropping
  • Selecting

They all are very similar. Let's assume that you have a PictureBox and want to crop an image inside it.

// Current selection
private Rectangle _cropRectangle;
// Starting point
private Point _cropStart;
// Dragging flag
private bool _isDragging;

private void pBox_MouseDown(object sender, MouseEventArgs e)
{
    _cropRectangle = new Rectangle(e.X, e.Y, 0, 0);
    _isDragging = true;
}

private void pBox_MouseUp(object sender, MouseEventArgs e)
{
    _isDragging = false;
}

private void pBox_MouseMove(object sender, MouseEventArgs e)
{
    if (!_isDragging)
        return;

    _cropRectangle = new Rectangle(Math.Min(_cropStart.X, e.X),
                                   Math.Min(_cropStart.Y, e.Y),
                                   Math.Abs(e.X - _cropStart.X),
                                   Math.Abs(e.Y - _cropStart.Y));
    pBox.Invalidate();
}

private void pBox_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawRectangle(Pens.Red, _cropRectangle);
}

What happens: I make use of three mouse events (MouseDown, MouseUp, MoseMove) and the Paint event. Basically, whenever you want to do anything from the above list, you'll have handle these four events.

I tried to keep the code short and self-explanatory. There are four event handlers working with three instance fields. The fields are used to store the current state of dragging process.

Feel free to customize the code, especially the pBox_Paint handler. Mine just draws a thin red rectangle around selected area. You might want to do something more elaborate here.

Whenever you're done with your rectangle, you can call the Crop method:

private Image Crop()
{
    Bitmap croppedImage = new Bitmap(_cropRectangle.Width, _cropRectangle.Height);
    using (Graphics g = Graphics.FromImage(croppedImage))
    {
        g.DrawImage(pBox.Image, 0, 0, _cropRectangle, GraphicsUnit.Pixel);
    }
    return croppedImage;
}

It creates a new Bitmap and put the selected portion of source image into it. The returned Image object might be used in any manner you like.

EDIT: trying to simplify the code I made some mistakes earlier, fixed now.


You can use Graphics.DrawImage to draw a cropped image onto the graphics object from a bitmap.

Rectangle cropRect = new Rectangle(...);
Bitmap src = Image.FromFile(fileName) as Bitmap;
Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);

using(Graphics g = Graphics.FromImage(target))
{
   g.DrawImage(src, cropRect, 
                    new Rectangle(0, 0, target.Width, target.Height),
                    GraphicsUnit.Pixel);
}

You can also refer the full code for this.... Refer this *Link*

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜