[c#]沿任何方向绘制矩形 [英] [c#] draw rectangle in any direction

查看:112
本文介绍了[c#]沿任何方向绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在任何方向绘制矩形?仅当您从左上角绘制并拖动到情人的右上角时,此代码才有效.

在mouseup事件中:

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);


在mousemove事件中:


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?

推荐答案

要确定方向,您需要使用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 [ ^ ].

此外,您似乎正在尝试将PictureBox类与交互式内容一起使用.原则上可以,但是完全没有意义.除了大量额外的无用的开发工作和消耗额外的资源之外,您无法从此控件中获得任何收益.此控件的唯一目的是提供一种呈现简单图像(通常为静态图像)的简化方式.您需要立即绘制控件,通常从System.Windows.Forms.Control派生.我在过去的答案中对此进行了解释:
如何从旧图纸中清除面板 [ ^ ],
在C#中绘制矩形 [在图片框内添加图片 [在mdi子表单之间画线 [在面板上捕获图形 [ Paint是一种哪种好玩的方法? (DataGridViewImageCell.Paint(...)) [
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


这篇关于[c#]沿任何方向绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆