BitMap Pixel Format

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;
  using System.Drawing.Imaging;

  public class Test
  {
    static void Main() 
    {
      // Create two new bitmap images
      Bitmap bmp1 = new Bitmap(100, 100, PixelFormat.Format32bppArgb);
      Bitmap bmp2 = new Bitmap(100, 100, PixelFormat.Format24bppRgb);

      bool b1 = ((bmp1.PixelFormat & PixelFormat.Alpha) != 0);
      bool b2 = ((bmp2.PixelFormat & PixelFormat.Alpha) != 0);

      Console.WriteLine("bmp1 has alpha?: " + b1);
      Console.WriteLine("bmp2 has alpha?: " + b2);

      bmp1.Dispose();
      bmp2.Dispose();
    }
  }

           
          


Draw a pie

image_pdfimage_print


   

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

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

  public Form1(){
    InitializeComponent();
        SetStyle(ControlStyles.Opaque, true);
  }
  protected override void Dispose( bool disposing ){
    if( disposing ){
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
    protected override void OnPaint(PaintEventArgs e) {
         Graphics g = e.Graphics;
         g.FillRectangle(Brushes.White, ClientRectangle);

         g.FillPie(Brushes.Chartreuse, new Rectangle(60, 60, 50, 50), 90, 210);
    }
  private void InitializeComponent(){
    this.components = new System.ComponentModel.Container();
    this.Size = new System.Drawing.Size(300,300);
    this.Text = "Form1";
  }
  static void Main() {
    Application.Run(new Form1());
  }
}



           
          


Arc Pie Demo

image_pdfimage_print

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

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

namespace MIsc2D_c
{
///

/// Summary description for MIsc2D.
///

public class MIsc2D : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ListBox listBox1;
///

/// Required designer variable.
///

private System.ComponentModel.Container components = null;

public MIsc2D()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

///

/// Clean up any resources being used.
///

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

#region Windows Form Designer generated code
///

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(40, 280);
this.label1.Name = “label1”;
this.label1.Size = new System.Drawing.Size(160, 16);
this.label1.TabIndex = 0;
//
// label2
//
this.label2.Location = new System.Drawing.Point(40, 296);
this.label2.Name = “label2”;
this.label2.Size = new System.Drawing.Size(160, 16);
this.label2.TabIndex = 1;
//
// label3
//
this.label3.Location = new System.Drawing.Point(40, 312);
this.label3.Name = “label3”;
this.label3.Size = new System.Drawing.Size(160, 16);
this.label3.TabIndex = 2;
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(24, 200);
this.listBox1.Name = “listBox1”;
this.listBox1.Size = new System.Drawing.Size(192, 69);
this.listBox1.TabIndex = 3;
//
// MIsc2D
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 373);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.listBox1,
this.label3,
this.label2,
this.label1});
this.Name = “MIsc2D”;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = “MIsc2D”;
this.Load += new System.EventHandler(this.MIsc2D_Load);
this.ResumeLayout(false);

}
#endregion

///

/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.Run(new MIsc2D());
}

private void MIsc2D_Load(object sender, System.EventArgs e)
{

}

protected override void OnPaint(PaintEventArgs e)
{
Graphics G = e.Graphics;

GraphicsPath p = new GraphicsPath();
PointF[] pts = { new PointF(50, 50),
new PointF(150, 25),
new PointF(200, 50)};
p.AddCurve(pts);
p.AddRectangle(new Rectangle(60, 60, 50, 50));
p.AddPie(100, 100, 80, 80, 0, 35);
G.DrawPath(Pens.Black,p);

GraphicsPathIterator iter = new GraphicsPathIterator(p);
label1.Text = “Num pts in path = ” + iter.Count.ToString();
label2.Text = “Num subpaths in path = ” + iter.SubpathCount.ToString();
label3.Text = “Path has curve = ” + iter.HasCurve().ToString();

int StartIndex;
int EndIndex;
int i;
bool IsClosed;
// Rewind the Iterator.
iter.Rewind();
// List the Subpaths.
for(i=0;i

Custom Pen

image_pdfimage_print


   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

public class MainForm : Form {
    public MainForm() {
    }

    protected void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        Pen bluePen = new Pen(Color.Blue, 20);
        Pen pen2 = Pens.Firebrick;

        g.DrawEllipse(bluePen, 10, 10, 100, 100);
        g.DrawLine(pen2, 10, 130, 110, 130);
        g.DrawPie(Pens.Black, 150, 10, 120, 150, 90, 80);

        Pen pen3 = new Pen(Color.Purple, 5);
        pen3.DashStyle = DashStyle.DashDotDot;
        g.DrawPolygon(pen3, new Point[]{     new Point(30, 140),
          new Point(265, 200), new Point(100, 225),
          new Point(190, 190), new Point(50, 330),
          new Point(20, 180)});

        Rectangle r = new Rectangle(150, 10, 130, 60);
        g.DrawRectangle(Pens.Blue, r);
        g.DrawString("Hello out there...How are ya?",new Font("Arial", 12), Brushes.Black, r);

        Pen customDashPen = new Pen(Color.BlueViolet, 10);
        float[] myDashes = { 5.0f, 2.0f, 1.0f, 3.0f };
        customDashPen.DashPattern = myDashes;
        g.DrawRectangle(customDashPen, ClientRectangle);
    }
    public static void Main(){
        Application.Run(new MainForm());    
    }
}

    


Set LineJoin for Pen

image_pdfimage_print
   
 

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

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.SmoothingMode = SmoothingMode.AntiAlias;
    g.FillRectangle(Brushes.White, this.ClientRectangle);

    Pen p = new Pen(Color.Black, 10);
    p.LineJoin = LineJoin.Bevel;
    e.Graphics.DrawRectangle(p, 20, 20, 60, 60);
    p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


creates the custom dash pattern

image_pdfimage_print
   
 

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

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    Pen p = new Pen(Color.Black, 2);

    float[] f = { 15, 5, 10, 5 };

    p.DashPattern = f;
    g.DrawRectangle(p, 10, 10, 80, 100);
    p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

    


Set DashStyle

image_pdfimage_print
   
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;

using System.Text;
using System.Windows.Forms;

public class Form1 : Form {

    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        g.FillRectangle(Brushes.White, this.ClientRectangle);
        Pen p = new Pen(Color.Black, 1);

        p.DashStyle = DashStyle.Dash;
        g.DrawLine(p, 3, 3, 100, 3);
        p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}