Click here to Skip to main content
15,887,302 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to draw a rectangle in any direction? This code works only if you draw from top left corner and drag to the lover right corner.

in mouseup event:
r = new Bitmap(f1.pictureBox4.Image); 
                gra = Graphics.FromImage(r); 
                Pen p = new Pen(f1.toolStripButton8.BackColor, int.Parse(f1.toolStripComboBox1.Text));
                f1.pictureBox4.Image = r;
ControlPaint.DrawReversibleFrame(f1.pictureBox4.RectangleToScreen(nakaPra), Color.Black, FrameStyle.Dashed);
                gra.DrawRectangle(p, nakPra);

in mousemove event:
 ControlPaint.DrawReversibleFrame(f1.pictureBox4.RectangleToScreen(nakPra), Color.Black, FrameStyle.Dashed);
                    nakPra.Width = e.X - nakPra.X;
                    nakPra.Height = e.Y - nakPra.Y;
ControlPaint.DrawReversibleFrame(f1.pictureBox4.RectangleToScreen(nakPra), Color.Black, FrameStyle.Dashed);

I want to drag from any corner. How to fix it?
Posted
Updated 29-Nov-12 11:08am
v2
Comments
Sergey Alexandrovich Kryukov 29-Nov-12 12:57pm    
System.Windows.Forms? Tag: "Forms"...
--SA

1 solution

For orientation, you need to use System.Drawing.Graphics.Transform:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.transform.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.matrix.aspx[^].

Besides, it looks you are trying to use the class PictureBox with something interactive. In principle, you could, but it makes no sense at all. You get nothing from this control, except a lot of extra useless development work and eating up extra resources. The only purpose of this control is to provide simplified way of presenting a simple image, usually static. You need to draw immediately on your control, typically derived from System.Windows.Forms.Control. I explain it in my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].

Instead of helping developers, this PictureBox control became a real curse of the beginners. Too much time lost for wrong development and explanations of what should be done. Don't go in wrong direction.

For some more detail on rendering, please also see these answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^].

Good luck,
—SA
 
Share this answer
 
Comments
CPallini 29-Nov-12 14:55pm    
5.
Sergey Alexandrovich Kryukov 29-Nov-12 16:01pm    
Thank you, Carlo.
--SA
fjdiewornncalwe 29-Nov-12 15:03pm    
+5. Comprehensive answer.
Sergey Alexandrovich Kryukov 29-Nov-12 16:02pm    
Thank you, Marcus.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900