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!
|
| Author: surender 07 Sep 2008 | Member Level: Silver | Rating: Points: 0 |
Do u hav idea of gui's?
|
| Author: Jan 07 Sep 2008 | Member Level: Bronze | Rating: 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"); }
 |