HatchBrush 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 hatch_c
{
    /// <summary>
    /// Summary description for hatch.
    /// </summary>
    public class hatch : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

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

        #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()
        {
      // 
      // hatch
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Name = "hatch";
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
      this.Text = "hatch";
      this.Load += new System.EventHandler(this.hatch_Load);

    }
        #endregion

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

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

    protected override void OnPaint ( PaintEventArgs e )
    {
      HatchBrush h = new HatchBrush(HatchStyle.BackwardDiagonal,
                                    Color.Black, 
                                    Color.Cyan);
      Pen P = new Pen(h, 20);

      e.Graphics.Clear(Color.AliceBlue);
      e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

      e.Graphics.DrawLine(P, 80, 90, 80, 200 );
      e.Graphics.FillEllipse(h, 50, 50, 50, 30 );
    }

    }
}

           
          


Scrolling Demo

image_pdfimage_print



   

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, 
   Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

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

        public Scrolling()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            this.Text = "Scrolling";
            this.Size = new Size(210, 290);

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

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #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()
        {
            // 
            // Scrolling
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(176, 254);
            this.Name = "Scrolling";
            this.Text = "Scrolling";

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Scrolling());
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g;
            g = Graphics.FromHwnd(this.Handle);
            GraphicsUnit units = GraphicsUnit.Pixel;

            string path = "MonetZsu.jpg";
            Image im = Image.FromFile(path);
            this.AutoScrollMinSize = new Size(im.Width, im.Height);
            //this.AutoScroll = true;

            Point P = this.AutoScrollPosition;
            Rectangle dstR = this.ClientRectangle;
            RectangleF srcR = new RectangleF(-P.X, -P.Y, dstR.Width, dstR.Height);
            g.DrawImage(im, dstR, srcR, units);
            g.Dispose();
        }
    }
}


           
          


P18_Scrolling.zip( 19 k)

Draw Pyramid

image_pdfimage_print

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Drawing.Drawing2D;

namespace Pyramid
{
///

/// Summary description for Pyramid.
///

public class Pyramid1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
///

/// Required designer variable.
///

private System.ComponentModel.Container components = null;

int rot = 0;
Point center = new Point(125, 100);
public Pyramid1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

this.Text = “Pyramid by Transfomation”;
//
// 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.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 8);
this.button1.Name = “button1”;
this.button1.Size = new System.Drawing.Size(48, 24);
this.button1.TabIndex = 0;
this.button1.Text = “Rotate”;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Pyramid
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1});
this.Name = “Pyramid”;
this.Text = “Pyramid”;
this.ResumeLayout(false);

}
#endregion

///

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

[STAThread]
static void Main()
{
Application.Run(new Pyramid1());
}
protected override void OnPaint (System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(this.BackColor);

Pyramid(g);
//PyramidPathRotate(g);
g.Dispose();
}
protected void Pyramid(Graphics g)
{
Pen p = new Pen(Color.Blue);
int ten = 10;
Rectangle rc = new Rectangle(50, 90, 150, ten); // the base rectangle
g.DrawRectangle(p, rc);
for(int i = 1;i <= 7; i++) { rc.Offset(0,-ten); rc.Inflate(-ten, 0); g.DrawRectangle(p, rc); } p.Dispose(); } private void button1_Click(object sender, System.EventArgs e) { // rot++; // rot is a class member // PyramidPathRotate(CreateGraphics()); // Refresh(); } protected void PyramidPathRotate(Graphics g) { GraphicsPath gP = new GraphicsPath(); // create an empty path Pen p = new Pen(Color.Blue); int ten = 10; Rectangle rc = new Rectangle(50, 90, 150, ten); // the base rectangle gP.AddRectangle(rc); for(int i = 1;i <= 7; i++) { rc.Offset(0,-ten); rc.Inflate(-ten,0); gP.AddRectangle(rc); } Matrix m = new Matrix(); m.RotateAt(45*rot, center, MatrixOrder.Append); gP.Transform(m); g.DrawPath(p, gP); // draw the rotated path g.FillEllipse(Brushes.Red, center.X, center.Y, 3, 3); // center point p.Dispose(); } } } [/csharp]

Perform a Screen Capture

image_pdfimage_print
   
 


using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class DesktopCapture {

    [DllImport("user32.dll")]
    private extern static IntPtr GetDesktopWindow();

    [DllImport("user32.dll")]
    private extern static IntPtr GetDC(IntPtr windowHandle);

    [DllImport("gdi32.dll")]
    private extern static IntPtr GetCurrentObject(IntPtr hdc,
      ushort objectType);

    [DllImport("user32.dll")]
    private extern static void ReleaseDC( IntPtr hdc );

    const int OBJ_BITMAP = 7;

    public static Bitmap Capture() {
        IntPtr desktopWindow = GetDesktopWindow();
        IntPtr desktopDC = GetDC( desktopWindow );
        IntPtr desktopBitmap = GetCurrentObject(desktopDC, OBJ_BITMAP);
        Bitmap desktopImage = Image.FromHbitmap( desktopBitmap );
        ReleaseDC(desktopDC);
        return desktopImage;
    }
}

    


Get Screen Resolution

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 Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      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;
      Bitmap bmp = new Bitmap("winter.jpg");
      g.DrawImage(bmp, 0, 0);

      Console.WriteLine("Screen resolution: " + g.DpiX + "DPI");
      Console.WriteLine("Image resolution: " + bmp.HorizontalResolution + "DPI");
      Console.WriteLine("Image Width: " + bmp.Width);
      Console.WriteLine("Image Height: " + bmp.Height);

      SizeF s = new SizeF(bmp.Width * (g.DpiX / bmp.HorizontalResolution),
                bmp.Height * (g.DpiY / bmp.VerticalResolution));
      Console.WriteLine("Display size of image: " + s);
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


           
          


Scale subsequent operations by 3x horizontally and 2x vertically

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 Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      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.FillRectangle(Brushes.White, this.ClientRectangle);

      g.DrawRectangle(Pens.Black, 10, 10, 50, 50);
      g.DrawEllipse(Pens.Black, 10, 10, 20, 20);

      // Now apply the scaling transformation
      // This will scale subsequent operations by 3x horizontally
      // and 2x vertically
      g.ScaleTransform(3.0f, 2.0f);

      // Now draw the same rectangle and circle, 
      // but with the scaling transformation 
      g.DrawRectangle(Pens.Black, 10, 10, 50, 50);
      g.DrawEllipse(Pens.Black, 10, 10, 20, 20);
    }

    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }

           
          


Fill region

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();
  }

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

    protected override void OnPaint ( PaintEventArgs e) {
      Rectangle r1 = new Rectangle(10, 10, 50, 50);
      Rectangle r2 = new Rectangle(40, 40, 50, 50);
      Region r = new Region(r1);
      r.Union(r2);

      GraphicsPath path = new GraphicsPath(new Point[] {new Point(45, 45),
                                                        new Point(145, 55),
                                                        new Point(200, 150),
                                                        new Point(75, 150),
                                                        new Point(45, 45)
                                                       }, new byte[] {  (byte)PathPointType.Start,
                                                                        (byte)PathPointType.Bezier,
                                                                        (byte)PathPointType.Bezier,
                                                                        (byte)PathPointType.Bezier,
                                                                        (byte)PathPointType.Line
                                                                     });
      r.Union(path);
      e.Graphics.FillRegion(Brushes.Blue, r);
    }

   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());
  }
}