C#-如何绘制圆角矩形? [英] C# - How to draw a rounded Rectangles ?

查看:428
本文介绍了C#-如何绘制圆角矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改此代码以绘制圆角矩形?

How can i change this code in order to draw a rounded rectangle ?

public partial class MikePainter : Form
{
   private Bitmap PicInMemory;
   private Graphics PaintOnMyPic;
 
   private int SecondX;   
   private int SecondY;

   public MikePainter()
   {
       InitializeComponent();

       PicInMemory = new Bitmap(panel1.Width, panel1.Height);
       Graphics PaintOnMyPic = Graphics.FromImage(PicInMemory);

///////////How to change this code to draw rounded rectangle////////////////

private void OnMouseMove(object sender, MouseEventArgs e)
{
  if (Selectedtool == Rect ) --> Check if the selectTool is rect 
  {
    Pen myPen = new Pen(Color.Black);
    Graphics PaintOnPanel = panel1.CreateGraphics();
    PaintOnPanel.DrawImage(PicInMemory, 0, 0);
    PaintOnPanel.DrawRectangle(myPen, SecondX,SecondY,e.X - SecondX,e.Y - SecondY);
  }
}

private void OnMouseUp(object sender, MouseEventArgs e)
{
  if (Selectedtool == Rect )
  {
    Graphics PaintOnPic = Graphics.FromImage(PicInMemory); 
    Pen myPen = new Pen(Color.Black);
    PaintOnPic.DrawRectangle(myPen, SecondX, SecondY, e.X - SecondX, e.Y - SecondY);
    Graphics PaintOnPanel = panel1.CreateGraphics();
    PaintOnPanel.DrawImage(PicInMemory, 0, 0);
  }
}

private void OnMouseDown(object sender, MouseEventArgs e)
{
   SecondX = e.X;
   SecondY = e.Y;
}

推荐答案

首先,停止创建Graphics对象:它们是一种稀缺资源,通常会在Garbage Collector投入处置之前就用完了.如果创建Graphics对象,则在对其进行完处理后应将其处置.笔,刷子等也是如此.

第二:不要在面板1的Paint事件中进行绘画,而是在EventArgs参数中准备好要使用的适当Graphics对象,这意味着您可以在需要时绘制图片.您可以使用面板的Invalidate方法强制执行此操作,这将在准备就绪时导致重新绘制.

第三,请参阅本文:扩展的图形-圆角矩形,C#3.0的字体规格等等 [ ^ ]
Firstly, stop creating Graphics objects: they are a scarce resource and will generally run out well before the Garbage Collector chips in to Dispose them. If you create a Graphics object, you should Dispose it when you hgave finished with it. The same goes for Pens, Brushes, and so forth.

Second: Instead of faffing like you are, do your painting in the panel1 Paint event - you are handed the appropriate Graphics object ready to go in the EventArgs parameter, and it means that you picture is drawn when ever it needs to be. You can force it by using the Invalidate method of the panel, which will cause a re-draw when ready.

Thirdly, see this article: Extended Graphics - Rounded rectangles, Font metrics and more for C# 3.0[^]


例如,请参见此处: http://www .gutgames.com/post/Drawing-a-Box-With-Rounded-Corners-in-C.aspx [扩展图形 [ http://msdn. microsoft.com/en-us/library/system.drawing.rectangle.round.aspx [
See here for instance: http://www.gutgames.com/post/Drawing-a-Box-With-Rounded-Corners-in-C.aspx[^]. Here you''ll find a CP homegrown solution: Extended Graphics[^].

And on MSDN we find out how to do this natively: http://msdn.microsoft.com/en-us/library/system.drawing.rectangle.round.aspx[^] in .NET.

Enjoy!

Best Regards,

—MRB


这篇关于C#-如何绘制圆角矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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