Csharp/C Sharp by API/System.Drawing/GraphicsUnit

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

GraphicsUnit.Display

<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;

class WhatSize: Form {

    public static void Main()
    {
         Application.Run(new WhatSize());
    }
    public WhatSize()
    {
         Text = "What Size?";
         ResizeRedraw = true; 
    }
    protected override void OnPaint(PaintEventArgs pea)
    {
         DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
    }     
    protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
    {
         Brush brush = new SolidBrush(clr);
         int   y     = 0;
  
         DoIt(grfx, brush, ref y, GraphicsUnit.Display);
    }
    void DoIt(Graphics grfx, Brush brush, ref int y, GraphicsUnit gu)
    {
         GraphicsState gs = grfx.Save();
  
         grfx.PageUnit  = gu;
         grfx.PageScale = 1;
  
         SizeF sizef = grfx.VisibleClipBounds.Size;
  
         grfx.Restore(gs);
  
         grfx.DrawString(gu+ ": " + sizef, Font, brush, 0, y);
         y += (int) Math.Ceiling(Font.GetHeight(grfx));
    }

}

 </source>


GraphicsUnit.Document

<source lang="csharp">

 using System;
 using System.Drawing;
 using System.Drawing.Drawing2D;
 using System.Collections;
 using System.ruponentModel;
 using System.Windows.Forms;
 using System.Data;
 public class Form1 : System.Windows.Forms.Form
 {
   public Form1()
   {
     InitializeComponent();
     SetStyle(ControlStyles.ResizeRedraw, true);
   }
   private void InitializeComponent()
   {
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(400, 400);
     this.Text = "";
     this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
   }
   static void Main() 
   {
     Application.Run(new Form1());
   }
   private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
   {
     Graphics g = e.Graphics;
     // Set quality of rendering.
     g.SmoothingMode = SmoothingMode.AntiAlias;
 
     // Configure graphics unit.
     g.PageUnit = GraphicsUnit.Document;
       Point renderingOrgPt = new Point(0,0);
     renderingOrgPt.X = 100;
     renderingOrgPt.Y = 100;
     // Configure origin.
     g.TranslateTransform(renderingOrgPt.X,
       renderingOrgPt.Y);
     g.DrawRectangle(new Pen(Color.Red, 1), 0, 0, 100, 100);
     
     g.Dispose();
   }
 }
  
   
 </source>


GraphicsUnit.Inch

<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;

class WhatSize: Form {

    public static void Main()
    {
         Application.Run(new WhatSize());
    }
    public WhatSize()
    {
         Text = "What Size?";
         ResizeRedraw = true; 
    }
    protected override void OnPaint(PaintEventArgs pea)
    {
         DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
    }     
    protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
    {
         Brush brush = new SolidBrush(clr);
         int   y     = 0;
  
         DoIt(grfx, brush, ref y, GraphicsUnit.Inch);
    }
    void DoIt(Graphics grfx, Brush brush, ref int y, GraphicsUnit gu)
    {
         GraphicsState gs = grfx.Save();
  
         grfx.PageUnit  = gu;
         grfx.PageScale = 1;
  
         SizeF sizef = grfx.VisibleClipBounds.Size;
  
         grfx.Restore(gs);
  
         grfx.DrawString(gu+ ": " + sizef, Font, brush, 0, y);
         y += (int) Math.Ceiling(Font.GetHeight(grfx));
    }

}

 </source>


GraphicsUnit.Millimeter

<source lang="csharp">

using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms;

class WhatSize: Form {

    public static void Main()
    {
         Application.Run(new WhatSize());
    }
    public WhatSize()
    {
         Text = "What Size?";
         ResizeRedraw = true; 
    }
    protected override void OnPaint(PaintEventArgs pea)
    {
         DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
    }     
    protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
    {
         Brush brush = new SolidBrush(clr);
         int   y     = 0;
  
         DoIt(grfx, brush, ref y, GraphicsUnit.Millimeter);
    }
    void DoIt(Graphics grfx, Brush brush, ref int y, GraphicsUnit gu)
    {
         GraphicsState gs = grfx.Save();
  
         grfx.PageUnit  = gu;
         grfx.PageScale = 1;
  
         SizeF sizef = grfx.VisibleClipBounds.Size;
  
         grfx.Restore(gs);
  
         grfx.DrawString(gu+ ": " + sizef, Font, brush, 0, y);
         y += (int) Math.Ceiling(Font.GetHeight(grfx));
    }

}

 </source>


GraphicsUnit.Pixel

<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class GraphicUnitSetting : System.Windows.Forms.Form {

 private System.ruponentModel.Container components = null;
 public GraphicUnitSetting()
 {
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(408, 273);
   this.Name = "GraphicUnitSetting";
   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 GraphicUnitSetting());
 }
 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;
   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();
 }

}

 </source>


GraphicsUnit.Point

<source lang="csharp"> using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; public class GraphicUnitPoint : System.Windows.Forms.Form {

 private System.ruponentModel.Container components = null;
 public GraphicUnitPoint()
 {
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(408, 273);
   this.Name = "GraphicUnitPoint";
   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 GraphicUnitPoint());
 }
 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.Point;
   
   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();
 }

}

 </source>