Graphics Properties : Line « 2D Graphics « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » 2D Graphics » LineScreenshots 
Graphics Properties
Graphics Properties

/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds

Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace GraphicsProps_c
{
    /// <summary>
    /// Summary description for GraphicsProps.
    /// </summary>
    public class GraphicsProps : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button B0;
    private System.Windows.Forms.Button B1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public GraphicsProps()
        {
            //
            // 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 Disposebool disposing )
        {
            ifdisposing )
            {
                if (components != null
                {
                    components.Dispose();
                }
            }
            base.Disposedisposing );
        }

        #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()
        {
      this.B0 = new System.Windows.Forms.Button();
      this.B1 = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // B0
      // 
      this.B0.Location = new System.Drawing.Point(16240);
      this.B0.Name = "B0";
      this.B0.Size = new System.Drawing.Size(4024);
      this.B0.TabIndex = 0;
      this.B0.Text = "B0";
      this.B0.Click += new System.EventHandler(this.B0_Click);
      // 
      // B1
      // 
      this.B1.Location = new System.Drawing.Point(72240);
      this.B1.Name = "B1";
      this.B1.Size = new System.Drawing.Size(4024);
      this.B1.TabIndex = 1;
      this.B1.Text = "B1";
      this.B1.Click += new System.EventHandler(this.B1_Click);
      // 
      // GraphicsProps
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(292273);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                  this.B1,
                                                                  this.B0});
      this.Name = "GraphicsProps";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "GraphicsProps";
      this.Load += new System.EventHandler(this.GraphicsProps_Load);
      this.ResumeLayout(false);

    }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new GraphicsProps());
        }

    private void GraphicsProps_Load(object sender, System.EventArgs e)
    {
    }
    protected override void OnPaint PaintEventArgs e )
    {
      Graphics G = e.Graphics;

      G.Clear(Color.Bisque);
      //Change one of the Graphics attributes and save state
      G.SmoothingMode=SmoothingMode.AntiAlias;
      GraphicsState OldG = G.Save();

      //Restore the attribute and draw a line
      G.SmoothingMode=SmoothingMode.Default;
      G.DrawLine(new Pen(Color.DarkMagenta, 20)1050
                                (int)(this.Width - 10)140);

      //Restore the old Graphics state and draw another line
      G.Restore(OldG);
      G.DrawLine(new Pen(Color.DarkMagenta, 20)10100
                                (int)(this.Width - 10)190);

      G.Dispose();
    }

    private void B0_Click(object sender, System.EventArgs e)
    {
      BeginContainerNoArg(this.CreateGraphics());
    }

    private void B1_Click(object sender, System.EventArgs e)
    {
     // BeginContainerIntRectArg(this.CreateGraphics());
     // World2PageXform(this.CreateGraphics());
      RenderText(this.CreateGraphics());
    }

    public void BeginContainerNoArg(Graphics G)
    {
      G.Clear(Color.Bisque);

      //Change one of the attributes of the Graphics object
      //then save the state.
      G.SmoothingMode = SmoothingMode.AntiAlias;
      GraphicsContainer OldG  = G.BeginContainer();

      //Restore the Smoothing mode state and draw a line
      G.SmoothingMode = SmoothingMode.Default;
      G.DrawLine(new Pen(Color.Chocolate, 20)1050
                                  (int)(this.Width - 10)150);

      //Restore the old Graphics state and draw another line
      G.EndContainer(OldG);
      G.DrawLine(new Pen(Color.Chocolate, 20)10100
                                  (int)(this.Width - 10)200);

      G.Dispose();
    }

    public void BeginContainerIntRectArg(Graphics G)
    {
      G.Clear(Color.Bisque);

      // Define transformation for container.
      Rectangle srcRect = new Rectangle(00200200);
      Rectangle destRect = new Rectangle(00100100);
      // Begin graphics container.
      GraphicsContainer containerState  = G.BeginContainer(destRect, 
                                                srcRect, GraphicsUnit.Pixel);

      G.DrawLine(new Pen(Color.DarkOrchid, 20)10100200100);
      G.EndContainer(containerState);

      G.DrawLine(new Pen(Color.DarkOrchid, 20)10100200100);

      G.Dispose();
    }

    public void World2PageXform(Graphics G)
    {
      int EndX = 1;
      int EndY = 1;
      G.Clear(Color.Azure);
      G.PageUnit=GraphicsUnit.Inch;
      G.TranslateTransform(11);
      G.DrawLine(Pens.Blue, 00, EndX, EndY);

      int Xpix = EndX * (int)G.DpiX;
      int Ypix = EndY * (int)G.DpiY;

    }

    public void RenderText(Graphics G)
    {
      Font F = new Font("Arial"16);
      SolidBrush B = new SolidBrush(Color.Black);

      G.Clear(Color.Azure);
      G.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
      G.DrawString("SingleBitPerPixel", F, B, new PointF(1010));

      G.TextRenderingHint = TextRenderingHint.AntiAlias;
      G.DrawString("AntiAlias default Contrast", F, B, new PointF(1060));

      G.TextContrast = 12;
      G.DrawString("AntiAlias Low Contrast", F, B, new PointF(1090));

      G.TextContrast = 1;
      G.DrawString("AntiAlias High Contrast", F, B, new PointF(10120));

      
    }
    }
}



           
       
Related examples in the same category
1.Simplest code to draw a lineSimplest code to draw a line
2.Draw radiation linesDraw radiation lines
3.illustrates the use of Pens and Linesillustrates the use of Pens and Lines
4.Draw line 1Draw line 1
5.Draw a lineDraw a line
6.Draw line 2Draw line 2
7.Line styleLine style
8.Draw line 3Draw line 3
9.Simplest Coordinate Simplest Coordinate
10.Draw a line 2Draw a line 2
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.