C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to capture portion of image that is within a selection rectangle?


Posted Date: 05 Sep 2008      Total Responses: 2

Posted By: Jan       Member Level: Bronze     Points: 1



Hello,

I'm creating a C# application in which customers create figures on a canvas. The figure is

usually rotated. I need to extract the portion of the image that is within the rotated

rectangle.

Here's my code so far:

void Test()
{
Image imgpic = (Image)picSource.Image.Clone();

Graphics gr = Graphics.FromImage(imgpic);

RectangleF r = new RectangleF(100F, 200F, 80F, 60F);

Matrix m = new Matrix();

//Let's say the select rectangle is at Point(30,80):
m.RotateAt(-30F, new PointF(130F, 280F), MatrixOrder.Append);
gr.Transform = m;
gr.SetClip(r, CombineMode.Exclude);

gr.Clear(Color.White);

imgpic.Save("D:\\Test1.png");
}

"picSource" is a picturebox holding an image. The RectangleF is the user's selection box,

which is rotated -30 degrees. The SetClip, as I noticed, erases all of the picture, apart

from the area within the selection rectangle. Now I'm very anxious to get the enclosed area

to a new image. The rotation of the area needs to be undone.

I spent all day on this. Any help would be greatly appreciated!





Responses

Author: surender    07 Sep 2008Member Level: SilverRating: Revenue Score: 0     Points: 0

Do u hav idea of gui's?


Author: Jan    07 Sep 2008Member Level: BronzeRating:     Points: 15

My (test) UI is pretty straightforward: a win form; a picturebox named pictureBox1. A button named button1.
The picturebox contains a picture.
The button serves to test the logic.
I cleaned up my code a bit (see below) and attached a screenshot. (The rectangle doesn't become visible until the screen is repainted)

private void button1_Click(object sender, EventArgs e)
{
Bitmap img = new Bitmap(pictureBox1.Image);
Graphics gr = Graphics.FromImage(pictureBox1.Image);

Rectangle r = new Rectangle(200, 300, 250, 160);

//Create a matrix to enable rotation:
PointF ptRot = new PointF(200F, 460F);
Matrix m = new Matrix();
m.RotateAt(30F, ptRot, MatrixOrder.Append);
gr.Transform = m;

//Draw a rectangle that will appear rotated:
gr.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2), r);

//Create a new bitmap for output; it needs to contain the image portion within the rectangle.
Bitmap bmp = new Bitmap(400, 400);
Graphics gr2 = Graphics.FromImage(bmp);

gr2.DrawImage(pictureBox1.Image, new Rectangle(0, 0, 250, 160), r, GraphicsUnit.Pixel);

//Save the file:
bmp.Save("D:\\Test1.png");
}



Post Reply
You must Sign In to post a response.
Next : Change text color programatically in C#
Previous : select date in calender
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use