GraphicsUnit: Display, Millimeter, Inch, Pixel : GraphicsUnit « 2D « C# / CSharp Tutorial






GraphicsUnit: Display, Millimeter, Inch, Pixel
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class GraphicUnitDisplay : System.Windows.Forms.Form
{
  private System.ComponentModel.Container components = null;

  public GraphicUnitDisplay()
  {
    InitializeComponent();
  }

  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }

  private void InitializeComponent()
  {
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(408, 273);
    this.Name = "GraphicUnitDisplay";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "GDI+ Coordinate";
    this.Resize += new System.EventHandler(this.OnResize);
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint);

  }

  [STAThread]
  static void Main() 
  {
    Application.Run(new GraphicUnitDisplay());
  }
  protected void OnPaint (object sender, System.Windows.Forms.PaintEventArgs e)
  {
      GraphicsUnit gUnit = GraphicsUnit.Pixel;

      Point renderingOrgPt = new Point(0,0);

    Graphics g = e.Graphics;
    g.SmoothingMode = SmoothingMode.AntiAlias;

    
    gUnit = GraphicsUnit.Display;
/*
    
    gUnit = GraphicsUnit.Millimeter;
    gUnit = GraphicsUnit.Inch;
    gUnit = GraphicsUnit.Pixel;
*/

    
    g.PageUnit = gUnit;
    g.TranslateTransform(renderingOrgPt.X,renderingOrgPt.Y);
    g.DrawRectangle(new Pen(Color.Red, 1), 0, 0, 100, 100);
  
    this.Text = string.Format("PageUnit: {0}, Origin: {1}",  gUnit, renderingOrgPt.ToString());
  }

  protected void OnResize (object sender, System.EventArgs e)
  {
    Invalidate();
  }
}








27.3.GraphicsUnit
27.3.1.Configure graphics unitConfigure graphics unit
27.3.2.GraphicsUnit PointGraphicsUnit Point
27.3.3.GraphicsUnit DocumentGraphicsUnit Document
27.3.4.GraphicsUnit.Display
27.3.5.GraphicsUnit.Inch
27.3.6.GraphicsUnit.Millimeter
27.3.7.GraphicsUnit: Display, Millimeter, Inch, PixelGraphicsUnit: Display, Millimeter, Inch, Pixel