How do I draw simple graphics in C#?

后端 未结 9 809
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 02:33

I just want to draw simple 2D objects like circle, line, square etc in C#. How do I do that? Back in the Turbo C++ days I remember initializing some graphics library for doi

相关标签:
9条回答
  • 2020-12-10 02:56

    GDI+ using System.Drawing

    0 讨论(0)
  • 2020-12-10 02:58

    You need to use GDI+.

    How you do it depends slightly on what you want to draw on. You can draw on a control or a form, or you can draw on an image object. Either way, you need a System.Drawing.Graphics object which I believe is located in System.Drawing.dll.

    You can instantiate a new Bitmap class and call Graphics.FromImage(myImage), and then draw using the methods on the Graphics object you just created. If you want to draw on a form or control just override the OnPaint method and look for the Graphics property on the EventArgs class.

    More information on System.Drawing namespace here: http://msdn.microsoft.com/en-us/library/system.drawing.aspx

    0 讨论(0)
  • 2020-12-10 02:59

    Here's a simple code sample that will get you started (assumes you have a PictureBox named pictureBox1):

    Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
    using (Graphics g = Graphics.FromImage(bmp))
    {
        g.DrawLine(new Pen(Color.Red), 0, 0, 10, 10);
    }
    pictureBox1.Image = bmp;
    

    The graphics object has a bunch of other drawing methods, and Intellisense will show you how to call them.

    0 讨论(0)
提交回复
热议问题