Problem with cropping image within pictureBox.

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
Hi<br/>
i have a problem with cropping in my pictureBox, it cropped my image incorrectly!<br/>
here is my code to crop my image :
<div style="background-color:white; color:black
<pre><span style="color:blue private <span style="color:blue void btnCrop_ItemClick(<span style="color:blue object sender, ItemClickEventArgs e)

{

Bitmap bmp = <span style="color:blue new Bitmap(<span style="color:blue this.picTargetImage.Image);

Rectangle r = <span style="color:blue this.rect;

<span style="color:blue this.ClearRectangle();



<span style="color:blue this._croppedImage = Utility.Imaging.Crop(bmp, r) <span style="color:blue as Bitmap;

<span style="color:blue this.picTargetImage.Image = <span style="color:blue this._croppedImage;

}

[/code]

<span style="font-family:Courier New The this.rect variable holds a user selection region on the pictureBox via this code :
<span style="font-family:Courier New
<div style="background-color:white; color:black
<pre><span style="color:blue private <span style="color:blue void picTargetImage_Paint(<span style="color:blue object sender, PaintEventArgs e)

{

<span style="color:blue if (<span style="color:blue this.rect != Rectangle.Empty)

<span style="color:blue this.DrawRect(e.Graphics, <span style="color:blue this.rect);

}



<span style="color:blue private <span style="color:blue void picTargetImage_MouseDown(<span style="color:blue object sender, MouseEventArgs e)

{

<span style="color:blue if (!<span style="color:blue this.mousePressed)

{

<span style="color:blue this.mousePressed = <span style="color:blue true;

<span style="color:blue this.startPoint = e.Location;

}

}



<span style="color:blue private <span style="color:blue void picTargetImage_MouseMove(<span style="color:blue object sender, MouseEventArgs e)

{

<span style="color:blue if (<span style="color:blue this.mousePressed)

{

<span style="color:blue int width = e.Location.X - <span style="color:blue this.startPoint.X;

<span style="color:blue int height = e.Location.Y - <span style="color:blue this.startPoint.Y;

<span style="color:blue this.rect = <span style="color:blue new Rectangle(<span style="color:blue this.startPoint, <span style="color:blue new Size(width, height));

<span style="color:blue this.DrawRect(<span style="color:blue this.picTargetImage.CreateGraphics(), <span style="color:blue this.rect);

<span style="color:blue this.picTargetImage.Invalidate();

}

}



<span style="color:blue private <span style="color:blue void picTargetImage_MouseUp(<span style="color:blue object sender, MouseEventArgs e)

{

<span style="color:blue if (<span style="color:blue this.mousePressed)

<span style="color:blue this.mousePressed = <span style="color:blue false;

}



<span style="color:blue private <span style="color:blue void DrawRect(Graphics g, Rectangle rect)

{

g.DrawRectangle(<span style="color:blue this.pen, rect);

}



<span style="color:blue private <span style="color:blue void ClearRectangle()

{

<span style="color:blue this.rect = Rectangle.Empty;

<span style="color:blue this.picTargetImage.Invalidate();

}

[/code]

and here is my crop method :
<div style="background-color:white; color:black
<pre><span style="color:blue public <span style="color:blue static Image Crop(Image image, Rectangle selection)

{

Bitmap bmp = image <span style="color:blue as Bitmap;



<span style="color:green // Check if it is a bitmap:

<span style="color:blue if (bmp == <span style="color:blue null)

<span style="color:blue throw <span style="color:blue new ArgumentException(<span style="color:#a31515 "No valid bitmap");



<span style="color:green // Crop the image:

Bitmap cropBmp = bmp.Clone(selection, bmp.PixelFormat);



<span style="color:green // Release the resources:

image.Dispose();



<span style="color:blue return cropBmp;

}

[/code]

Note : Please set pictureBox.SizeMode to Stretch.
Also, here is my needed fields :
<pre lang="x-c# private Rectangle rect;
private Pen pen;
private Point startPoint;
private bool mousePressed = false;

public YourFormConstructor()
{
this.pen = new Pen(Brushes.Black, 2);
}[/code]
<br/>
Can anybody check my code and give me solution ?<br/>
Thanks in advance

<

http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx


View the full article
 
Back
Top