TranslateTransform: 100,100

image_pdfimage_print


   


  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  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;
      g.SmoothingMode = SmoothingMode.AntiAlias;
  
      g.PageUnit = GraphicsUnit.Pixel;

        Point renderingOrgPt = new Point(0,0);

      renderingOrgPt.X = 100;
      renderingOrgPt.Y = 100;

      g.TranslateTransform(renderingOrgPt.X,
        renderingOrgPt.Y);

      g.DrawRectangle(new Pen(Color.Red, 1), 0, 0, 100, 100);
      
      g.Dispose();

    }
  }


           
          


Coordiate Translate Transform

image_pdfimage_print


   


  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ComponentModel;
  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;
      g.SmoothingMode = SmoothingMode.AntiAlias;
      g.PageUnit = GraphicsUnit.Inch;

        Point renderingOrgPt = new Point(0,0);
      renderingOrgPt.X = 1;
      renderingOrgPt.Y = 1;

      g.TranslateTransform(renderingOrgPt.X,renderingOrgPt.Y);
      g.DrawRectangle(new Pen(Color.Red, 1), 0, 0, 100, 100);
      g.Dispose();
    }
  }
           
          


Return a randomly-generated color

image_pdfimage_print
   
 
// crudwork
// Copyright 2004 by Steve T. Pham (http://www.crudwork.com)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with This program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace crudwork.Utilities
{
  /// <summary>
  /// Color Utility
  /// </summary>
  public static class ColorUtil
  {
    private static Random random = new Random();
    private static int r;
    private static int g;
    private static int b;

    /// <summary>
    /// Return a randomly-generated color
    /// </summary>
    /// <returns></returns>
    public static Color GetRandomColor()
    {
      r = random.Next(0, 256);
      g = random.Next(0, 256);
      b = random.Next(0, 256);
      return Color.FromArgb(r, g, b);
    }
  }
}

   
     


Color representation in h,s,v with h = [0 – 360], s,v = [0.0 – 1.0].

image_pdfimage_print

//GNU General Public License version 2 (GPLv2)
//http://dotwayutilities.codeplex.com/license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dotway.WPF.Controls.Utilities
{
///

/// Color representation in h,s,v with h = [0 – 360], s,v = [0.0 – 1.0].
///

public struct HSV
{
public HSV(double hue, double saturation, double value)
{
h = 0.0;
s = 0.0;
v = 0.0;

H = hue;
S = saturation;
V = value;
}

private double h;
public double H
{
get { return h; }
set
{
if (value >= 0 && value <= 360) { h = value; } else { throw new ArgumentOutOfRangeException("H is not in the span [0, 360]."); } } } private double s; public double S { get { return s; } set { if (value >= 0.0 && value <= 1.0) { s = value; } else { throw new ArgumentOutOfRangeException("S is not in the span [0, 1]."); } } } private double v; public double V { get { return v; } set { if (value >= 0.0 && value <= 1.0) { v = value; } else { throw new ArgumentOutOfRangeException("V is not in the span [0, 1]."); } } } } } [/csharp]

Color representation in r,g,b with values [0.0 – 1.0].

image_pdfimage_print

//GNU General Public License version 2 (GPLv2)
//http://dotwayutilities.codeplex.com/license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.ComponentModel;

namespace Dotway.WPF.Controls.Utilities
{
///

/// Color representation in r,g,b with values [0.0 – 1.0].
///

public struct RGB
{
public RGB(Color color)
{
r = 0.0;
g = 0.0;
b = 0.0;

R = color.R / 255.0;
G = color.G / 255.0;
B = color.B / 255.0;
}

///

/// Each value must be in the span 0 – 255.
///

/// /// /// public RGB(byte red, byte green, byte blue)
{
r = 0.0;
g = 0.0;
b = 0.0;

R = red / 255.0;
G = green / 255.0;
B = blue / 255.0;
}

///

/// Each value must be in the span 0.0 – 1.0.
///

/// /// /// public RGB(double red, double green, double blue)
{
r = 0.0;
g = 0.0;
b = 0.0;

R = red;
G = green;
B = blue;
}

private double r;
public double R
{
get { return r; }
set
{
if (value >= 0.0 && value <= 1.0) { r = value; } else { throw new ArgumentOutOfRangeException("R is not in the span [0, 1]"); } } } private double g; public double G { get { return g; } set { if (value >= 0.0 && value <= 1.0) { g = value; } else { throw new ArgumentOutOfRangeException("G is not in the span [0, 1]"); } } } private double b; public double B { get { return b; } set { if (value >= 0.0 && value <= 1.0) { b = value; } else { throw new ArgumentOutOfRangeException("B is not in the span [0, 1]"); } } } } } [/csharp]

Get all known color

image_pdfimage_print

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form {
ArrayList knownColorList = new ArrayList();
ArrayList knownColorNameList = new ArrayList();

public Form1() {
NonSystemColors(knownColorList, knownColorNameList);

}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
private void NonSystemColors(ArrayList knownColorList, ArrayList knownColorNameList) {
Array cA = Enum.GetValues(typeof(KnownColor));
foreach (KnownColor knwnC in cA) {
Color curC = Color.FromKnownColor(knwnC);
if (!curC.IsSystemColor) {
knownColorList.Add(curC);
knownColorNameList.Add(curC.Name.ToString());
}
}
}
protected override void OnPaint(PaintEventArgs pea) {
Graphics g = pea.Graphics;
int wi = 70, hi = 12, rectNb = 8;
int count = knownColorList.Count;

this.Width = (wi + 2) * rectNb + 9;
int y = (int)(count / rectNb);
this.Height = y * (2 + hi) + 60;

DisplayKnownColors(g, count, wi, hi, rectNb);
g.Dispose();
}
private void DisplayKnownColors(Graphics g, int count, int wi, int hi, int rectNb) {
Rectangle rec;
Pen p = new Pen(this.ForeColor);
Brush b;

StringFormat strfmt = new StringFormat();
strfmt.LineAlignment = strfmt.Alignment = StringAlignment.Near;

int x, y;
for (int i = 0; i < count; i++) { x = (int)(i % rectNb); y = (int)(i / rectNb); rec = new Rectangle(1 + x * (2 + wi), 1 + y * (2 + hi), wi, hi); g.DrawRectangle(p, rec); b = new SolidBrush((Color)knownColorList[i]); g.FillRectangle(b, rec); b = new SolidBrush(Color.Black); g.DrawString((string)knownColorNameList[i], this.Font, b, rec, strfmt); } } } [/csharp]

Use Color.FromArgb to create Color

image_pdfimage_print
   
  


using System;
using System.Drawing;
using System.Windows.Forms;
   
class RandomClear: Form 
{
     public static void Main()
     {
          Application.Run(new RandomClear());
     }
     public RandomClear()      
     {
          Text = "Random Clear";
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics graphics = pea.Graphics;
          Random   rand = new Random();
   
          graphics.Clear(Color.FromArgb(rand.Next(256),
                                    rand.Next(256),
                                    rand.Next(256)));
     }
}