رسم خط و مستطیل با استفاده از کد در سی شارپ
در این مطلب آموزشی یاد میگیرید که با استفاده از کدهای سی شارپ ، اشکال گرافیکی مثل خط و مربع رسم کنید.
کد زیر برای رسم یک مستطیل توپر به کار می رود:
private void Form1_Paint(object sender, PaintEventArgs e)
{
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(0, 0, 100, 80));
myBrush.Dispose();
formGraphics.Dispose();
}
{
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Blue);
System.Drawing.Graphics formGraphics;
formGraphics = this.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle(0, 0, 100, 80));
myBrush.Dispose();
formGraphics.Dispose();
}
به جای کلمه ی Blue ، رنگ مستطیل و به جای 100 و 80 می توانید عرض و ارتفاع مستطیل را تعیین کنید.
دقت کنید که حتما در رویداد Paint فرم باشد.
و این کد هم برای رسم خط به کار می رود:
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, 0, 0, 200, 200);
myPen.Dispose();
formGraphics.Dispose();
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.DrawLine(myPen, 0, 0, 200, 200);
myPen.Dispose();
formGraphics.Dispose();