Draw Shapes to Bitmap in Memory

도형그리기를 비트맵이미지로 그리기1273706404.zip
private void Form1_Paint(object sender, PaintEventArgs e)
{
            Graphics g = e.Graphics;


            // Create a Bitmap image in memory and set its CompositingMode
            Bitmap bmp = new Bitmap(this.Size.Width, this.Size.Height,
                                                              PixelFormat.Format32bppArgb);
            Graphics gBmp = Graphics.FromImage(bmp);
            gBmp.CompositingMode = CompositingMode.SourceCopy;


            // Pen으로 대각선과 사각형 그리기 (to bitmap in memory)
            Pen p = new Pen(Color.Black, 3);
            Rectangle r = new Rectangle(10, 10, this.Size.Width/2, this.Size.Height/2);
            gBmp.DrawLine(p, 10, 10, this.Size.Width / 2 + 10, this.Size.Height / 2 + 10);
            gBmp.DrawLine(p, this.Size.Width / 2 + 10, 10, 10, this.Size.Height / 2 + 10);
            gBmp.DrawRectangle(p, r);


            // Brush로 파란사각형 그리기 (to bitmap in memory)
            Brush b = new SolidBrush(Color.Blue);
            Rectangle r2 = new Rectangle(this.Size.Width / 2 + 10, this.Size.Height / 2 + 10,
                                                                    this.Size.Width / 2 – 10, this.Size.Height / 2 – 10);
            gBmp.FillRectangle(b, r2);


            // Brush로 빨간삼각형 그리기 (to bitmap in memory)
            Brush b2 = new SolidBrush(Color.Red);
            Point[] pt = new Point[3];
            pt[0].X = 400; pt[0].Y = 10;
            pt[1].X = 300; pt[1].Y = 210;
            pt[2].X = 500; pt[2].Y = 210;
            gBmp.FillPolygon(b2, pt);


            // HatchBrush로 타원 그리기 (to bitmap in memory)
            HatchBrush hb = new HatchBrush(HatchStyle.DiagonalCross,
                                                                           Color.Yellow, Color.Brown);
            gBmp.FillEllipse(hb, 10, 220, 200, 100);


            // DrawArc 메소드 사용하여 호 그리기 (to bitmap in memory)
            Pen p2 = new Pen(Color.Green, 3);
            Rectangle r3 = new Rectangle(220, 220, 200, 100);
            gBmp.DrawArc(p2, r3, 0, 180);


            // draw the bitmap on our window
            g.DrawImage(bmp, 0, 0, bmp.Width, bmp.Height);
            bmp.Dispose();
            gBmp.Dispose();
    }

Leave a Reply

Your email address will not be published. Required fields are marked *