Thumbnail Image

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

class Thumbnail: Form
{
const int iSquare = 64;
Image imageThumbnail;

public static void Main()
{
Application.Run(new Thumbnail());
}
public Thumbnail()
{
Text = “Thumbnail”;
ResizeRedraw = true;
Image image = Image.FromFile(“Color.jpg”);

int cxThumbnail, cyThumbnail;

if (image.Width > image.Height)
{
cxThumbnail = iSquare;
cyThumbnail = iSquare * image.Height / image.Width;
}
else
{
cyThumbnail = iSquare;
cxThumbnail = iSquare * image.Width / image.Height;
}
imageThumbnail = image.GetThumbnailImage(cxThumbnail, cyThumbnail,
null, (IntPtr) 0);
}
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)
{
for (int y = 0; y < cy; y += iSquare){ for (int x = 0; x < cx; x += iSquare) grfx.DrawImage(imageThumbnail, x + (iSquare - imageThumbnail.Width) / 2, y + (iSquare - imageThumbnail.Height) / 2, imageThumbnail.Width, imageThumbnail.Height); } } } [/csharp]

Image Open

   
  
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
   
class ImageOpen: Form
{
     protected string strProgName;
     protected string strFileName;
     protected Image  image;
   
     public static void Main()
     {
          Application.Run(new ImageOpen());
     }
     public ImageOpen()
     {
          ResizeRedraw = true;
   
          Menu = new MainMenu();
          Menu.MenuItems.Add("&amp;File");
          Menu.MenuItems[0].MenuItems.Add(new MenuItem("&amp;Open...", 
                                   new EventHandler(MenuFileOpenOnClick),
                                   Shortcut.CtrlO));
     }
     void MenuFileOpenOnClick(object obj, EventArgs ea)
     {
          OpenFileDialog dlg = new OpenFileDialog();
   
          dlg.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;" +
                              "*.jfif;*.png;*.tif;*.tiff;*.wmf;*.emf|" +
                       "Windows Bitmap (*.bmp)|*.bmp|" +
                       "Windows Icon (*.ico)|*.ico|" +
                       "Graphics Interchange Format (*.gif)|*.gif|" +
                       "JPEG File Interchange Format (*.jpg)|" +
                              "*.jpg;*.jpeg;*.jfif|" +
                       "Portable Network Graphics (*.png)|*.png|" +
                       "Tag Image File Format (*.tif)|*.tif;*.tiff|" +
                       "Windows Metafile (*.wmf)|*.wmf|" +
                       "Enhanced Metafile (*.emf)|*.emf|" +
                       "All Files (*.*)|*.*";
   
          if (dlg.ShowDialog() == DialogResult.OK)
          {    
   
               try
               {
                    image = Image.FromFile(dlg.FileName);
               }
               catch (Exception exc)
               {
                    Console.WriteLine(exc.Message);
                    return;
               }
               strFileName = dlg.FileName;
               Text = Path.GetFileName(strFileName);
               Invalidate();
          }
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
   
          if (image != null)
               grfx.DrawImage(image, 0, 0);
     }
}

   
     


Image Save

   
  
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
   
class ImageOpen: Form
{
     protected string strProgName;
     protected string strFileName;
     protected Image  image;
     MenuItem miSaveAs;
     public static void Main()
     {
          Application.Run(new ImageOpen());
     }
     public ImageOpen()
     {
          ResizeRedraw = true;
   
          Menu = new MainMenu();
          Menu.MenuItems.Add("&amp;File");
          Menu.MenuItems[0].MenuItems.Add(new MenuItem("&amp;Open...", 
                                   new EventHandler(MenuFileOpenOnClick),
                                   Shortcut.CtrlO));
          Menu.MenuItems[0].Popup += new EventHandler(MenuFileOnPopup);
          miSaveAs = new MenuItem("Save &amp;As...");
          miSaveAs.Click += new EventHandler(MenuFileSaveAsOnClick);
          Menu.MenuItems[0].MenuItems.Add(miSaveAs);
     }
     void MenuFileOnPopup(object obj, EventArgs ea)
     {
          miSaveAs.Enabled = (image != null);
     }
     void MenuFileSaveAsOnClick(object obj, EventArgs ea)
     {
          SaveFileDialog savedlg = new SaveFileDialog();
   
          savedlg.InitialDirectory = Path.GetDirectoryName(strFileName);
          savedlg.FileName = Path.GetFileNameWithoutExtension(strFileName);
          savedlg.AddExtension = true;
          savedlg.Filter = "Windows Bitmap (*.bmp)|*.bmp|" +
                           "Graphics Interchange Format (*.gif)|*.gif|" +
                           "JPEG File Interchange Format (*.jpg)|" +
                              "*.jpg;*.jpeg;*.jfif|" +
                           "Portable Network Graphics (*.png)|*.png|" +
                           "Tagged Imaged File Format (*.tif)|*.tif;*.tiff";
   
          if (savedlg.ShowDialog() == DialogResult.OK)
          {
               try
               {
                    image.Save(savedlg.FileName);
               }
               catch (Exception exc)
               {
                    MessageBox.Show(exc.Message, Text);
                    return;
               }
               strFileName = savedlg.FileName;
               Text = strProgName + " - " + Path.GetFileName(strFileName);
          }
     }

     void MenuFileOpenOnClick(object obj, EventArgs ea)
     {
          OpenFileDialog dlg = new OpenFileDialog();
   
          dlg.Filter = "All Image Files|*.bmp;*.ico;*.gif;*.jpeg;*.jpg;" +
                              "*.jfif;*.png;*.tif;*.tiff;*.wmf;*.emf|" +
                       "Windows Bitmap (*.bmp)|*.bmp|" +
                       "Windows Icon (*.ico)|*.ico|" +
                       "Graphics Interchange Format (*.gif)|*.gif|" +
                       "JPEG File Interchange Format (*.jpg)|" +
                              "*.jpg;*.jpeg;*.jfif|" +
                       "Portable Network Graphics (*.png)|*.png|" +
                       "Tag Image File Format (*.tif)|*.tif;*.tiff|" +
                       "Windows Metafile (*.wmf)|*.wmf|" +
                       "Enhanced Metafile (*.emf)|*.emf|" +
                       "All Files (*.*)|*.*";
   
          if (dlg.ShowDialog() == DialogResult.OK)
          {    
   
               try
               {
                    image = Image.FromFile(dlg.FileName);
               }
               catch (Exception exc)
               {
                    Console.WriteLine(exc.Message);
                    return;
               }
               strFileName = dlg.FileName;
               Text = Path.GetFileName(strFileName);
               Invalidate();
          }
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
   
          if (image != null)
               grfx.DrawImage(image, 0, 0);
     }
     
}

   
     


Fill Ellipse with image based Texture Brush


   
 

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{
   private System.ComponentModel.Container components = null;
   private Image theImage;
   private Image smallImage;

   public Form1() {
      InitializeComponent();
      SetStyle(ControlStyles.Opaque, true);
      theImage = new Bitmap("Winter.jpg");
      smallImage = new Bitmap(theImage,new Size(theImage.Width / 2, theImage.Height / 2));
   }

    protected override void OnPaint(PaintEventArgs e){
       Graphics g = e.Graphics;
       g.FillRectangle(Brushes.White, ClientRectangle);
       Brush tBrush = new TextureBrush(smallImage, new Rectangle(0, 0,smallImage.Width, smallImage.Height));
       g.FillEllipse(tBrush, ClientRectangle);
       tBrush.Dispose();
    }

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


           
         
     


Load image and display


   
 

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{
   private System.ComponentModel.Container components = null;
   private Image theImage;

   public Form1() {
      InitializeComponent();
      SetStyle(ControlStyles.Opaque, true);
      theImage = new Bitmap("Winter.jpg");
   }

   protected override void OnPaint(PaintEventArgs e){
      Graphics g = e.Graphics;
      g.DrawImage(theImage, ClientRectangle);
   }


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


           
         
     


Set image resolution and paint it


   
 

  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.FillRectangle(Brushes.White, this.ClientRectangle);
      bmp.SetResolution(600f, 600f);
      g.DrawImage(bmp, 0, 0);
      bmp.SetResolution(1200f, 1200f);
      g.DrawImage(bmp, 180, 0);
    }

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


           
         
     


Draw image based on its size


   
 


  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");

      Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
      g.DrawImage(bmp, r, r, GraphicsUnit.Pixel);
    }

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