Draw shapes to the bitmap in memory

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 gForm = e.Graphics;
      gForm.FillRectangle(Brushes.White, this.ClientRectangle);

      // Create a Bitmap image in memory and set its CompositingMode
      Bitmap bmp = new Bitmap(260, 260, PixelFormat.Format32bppArgb);
      Graphics gBmp = Graphics.FromImage(bmp);
      gBmp.CompositingMode = CompositingMode.SourceCopy;

      // draw a red circle to the bitmap in memory
      Color red = Color.FromArgb(0x60, 0xff, 0, 0);
      Brush redBrush = new SolidBrush(red);
      gBmp.FillEllipse(redBrush, 70, 70, 160, 160);

      // draw a green rectangle to the bitmap in memory
      Color green = Color.FromArgb(0x40, 0, 0xff, 0);
      Brush greenBrush = new SolidBrush(green);
      gBmp.FillRectangle(greenBrush, 10, 10, 140, 140);
      
      // draw the bitmap on our window
      gForm.DrawImage(bmp, 20, 20, bmp.Width, bmp.Height);

      bmp.Dispose();
      gBmp.Dispose();
      redBrush.Dispose();
      greenBrush.Dispose();
    }

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

           
         
     


Create your own BitMap

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)
    {      
      Bitmap bmp = new Bitmap(100, 100);
      Graphics gImage = Graphics.FromImage(bmp);
      gImage.FillRectangle(Brushes.Red, 0, 0, bmp.Width, bmp.Height);
      gImage.DrawRectangle(Pens.Black, 10, 10, bmp.Width - 20, bmp.Height - 20);

      Graphics gScreen = e.Graphics;
      gScreen.FillRectangle(Brushes.White, this.ClientRectangle);
      gScreen.DrawImage(bmp, new Rectangle(10, 10, bmp.Width, bmp.Height));
    }

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


           
         
     


Bitmap property: Height, Physical Dimension, width, raw format and size

image_pdfimage_print


   
 

using System;
using System.IO;
using System.Drawing;
   
class MainClass
{
    static public void Main()
    {
        Bitmap img = new Bitmap("winter.jpg");
        
        Console.WriteLine(img.PhysicalDimension.ToString() );
        Console.WriteLine(img.Height.ToString() );
        Console.WriteLine(img.Width.ToString() );
        Console.WriteLine(img.RawFormat.ToString() );
        Console.WriteLine(img.Size.ToString() );
    }
}
           
         
     


Bitmap.SetResolution

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;
    Bitmap bmp = new Bitmap("rama.jpg");

    g.FillRectangle(Brushes.White, this.ClientRectangle);
    bmp.SetResolution(600f, 600f);
    g.DrawImage(bmp, 0, 0);
    bmp.SetResolution(1200f, 1200f);
    g.DrawImage(bmp, 180, 0);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

   
     


Create Graphics object From Image

image_pdfimage_print
   
  

using System;
using System.Drawing;
using System.Windows.Forms;
   
class HelloWorldBitmap: Form
{
     const  float fResolution = 300;
     Bitmap bitmap = new Bitmap(1, 1);
   
     public static void Main()
     {
          Application.Run(new HelloWorldBitmap());
     }
     public HelloWorldBitmap()
     {
          ResizeRedraw = true; 
          
          bitmap.SetResolution(fResolution, fResolution);
   
          Graphics grfx = Graphics.FromImage(bitmap);
          Font     font = new Font("Times New Roman", 72);
          Size     size = grfx.MeasureString(Text, font).ToSize();
   
          bitmap = new Bitmap(bitmap, size);
          bitmap.SetResolution(fResolution, fResolution);
               
          grfx = Graphics.FromImage(bitmap);
          grfx.Clear(Color.White);
          grfx.DrawString(Text, font, Brushes.Black, 0, 0);
          grfx.Dispose();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          grfx.DrawImage(bitmap, 0, 0);
     }
}

   
     


Resize the Bitmap using the highest quality interpolation mode

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;
    Bitmap bmp = new Bitmap("rama.jpg");
    g.FillRectangle(Brushes.White, this.ClientRectangle);

    int width = bmp.Width;
    int height = bmp.Height;

    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.DrawImage(
      bmp,
      new Rectangle(130, 10, 120, 120),   // source rectangle
      new Rectangle(0, 0, width, height), // destination rectangle
      GraphicsUnit.Pixel);
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}

   
     


Resize the Bitmap using the lowest quality interpolation mode

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;
    Bitmap bmp = new Bitmap("rama.jpg");
    g.FillRectangle(Brushes.White, this.ClientRectangle);

    int width = bmp.Width;
    int height = bmp.Height;

    g.InterpolationMode = InterpolationMode.NearestNeighbor;
    g.DrawImage(
      bmp,
      new Rectangle(10, 10, 120, 120),    // source rectangle
      new Rectangle(0, 0, width, height), // destination rectangle
      GraphicsUnit.Pixel);

    }
    public static void Main() {
        Application.Run(new Form1());
    }
}