C# Graphics Library
I need to integrate some graphics methods into my C# programs. I do not need any colour graphs just black and white graphs giving me lines circles and general curves. What is the best out there?
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
senthil kannan6 pointsPosted Jul 25, 2006, 2:00 AM EDT
u can go thru this code ....
just change the colors as u like
using System; using System.Drawing; using System.Collections; //using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Drawing.Drawing2D; namespace _2D { /// <summary> /// Summary description for Form1. /// </summary> public /*partial*/ class DrawShapesForm : Form { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public DrawShapesForm() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { // // DrawShapesForm // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Name = "DrawShapesForm"; this.Text = "2D Example"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawShapesForm_Paint); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new DrawShapesForm()); } private void DrawShapesForm_Paint( object sender, PaintEventArgs e ) { // references to object we will use Graphics graphicsObject = e.Graphics; // ellipse rectangle and gradient brush Rectangle drawArea1 = new Rectangle( 5, 35, 30, 100 );LinearGradientBrush linearBrush =
new LinearGradientBrush ( drawArea1, Color.Blue, Color.Yellow, LinearGradientMode.ForwardDiagonal); // draw ellipse filled with a blue-yellow gradient graphicsObject.FillEllipse( linearBrush, 5, 30, 65, 100 ); //graphicsObject.FillEllipse(linearBrush,5,150,100,150); // pen and location for red outline rectangle Pen thickRedPen = new Pen( Color.Red, 10 ); graphicsObject.DrawLine(thickRedPen,40,150,40,250); graphicsObject.DrawLine(thickRedPen,45,200,90,150); graphicsObject.DrawLine(thickRedPen,45,200,90,250); Rectangle drawArea2 = new Rectangle( 80, 30, 65, 100 ); // draw thick rectangle outline in red graphicsObject.DrawRectangle( thickRedPen, drawArea2 ); // bitmap texture Bitmap textureBitmap = new Bitmap( 10, 10 ); // get bitmap graphicsGraphics graphicsObject2 =
Graphics.FromImage( textureBitmap );
// brush and pen used throughout program
SolidBrush solidColorBrush =
new SolidBrush( Color.Red ); Pen coloredPen = new Pen( solidColorBrush ); // fill textureBitmap with yellow solidColorBrush.Color = Color.Yellow; graphicsObject2.FillRectangle( solidColorBrush, 0, 0, 10, 10 ); // draw small black rectangle in textureBitmap coloredPen.Color = Color.Black; graphicsObject2.DrawRectangle( coloredPen, 1, 1, 6, 6 ); // draw small blue rectangle in textureBitmap solidColorBrush.Color = Color.Blue; graphicsObject2.FillRectangle( solidColorBrush, 1, 1, 3, 3 ); // draw small red square in textureBitmap solidColorBrush.Color = Color.Red; graphicsObject2.FillRectangle( solidColorBrush, 4, 4, 3, 3 ); // create textured brush and // display textured rectangleTextureBrush texturedBrush =
new TextureBrush( textureBitmap ); graphicsObject.FillRectangle( texturedBrush, 155, 30, 75, 100 ); // draw pie-shaped arc in white coloredPen.Color = Color.White; coloredPen.Width = 6; graphicsObject.DrawPie( coloredPen, 240, 30, 75, 100, 0, 270 ); // draw lines in green and yellow coloredPen.Color = Color.Green; coloredPen.Width = 5; graphicsObject.DrawLine( coloredPen, 395, 30, 320, 150 ); // draw a rounded, dashed yellow line coloredPen.Color = Color.Yellow; coloredPen.DashCap = DashCap.Round; coloredPen.DashStyle = DashStyle.Dash; graphicsObject.DrawLine( coloredPen, 320, 30, 395,150 ); } } }