Image Zoom : Image « 2D Graphics « C# / C Sharp






Image Zoom

    
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.Windows.Forms.Label label1= new System.Windows.Forms.Label();
    private System.Windows.Forms.GroupBox groupBox1= new System.Windows.Forms.GroupBox();
    private System.Windows.Forms.RadioButton radioButton1= new System.Windows.Forms.RadioButton();
    private System.Windows.Forms.RadioButton radioButton2= new System.Windows.Forms.RadioButton();
    private System.Windows.Forms.RadioButton radioButton3= new System.Windows.Forms.RadioButton();
    private System.Windows.Forms.RadioButton radioButton4= new System.Windows.Forms.RadioButton();
    private System.Windows.Forms.Label label2= new System.Windows.Forms.Label();
    private System.Windows.Forms.CheckBox checkBox1= new System.Windows.Forms.CheckBox();

    Image im = null;
    Image im2 = null;
    public Form1() {
        this.groupBox1.SuspendLayout();
        this.SuspendLayout();
        this.label1.Location = new System.Drawing.Point(8, 16);
        this.label1.Size = new System.Drawing.Size(200, 240);
        this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                          this.checkBox1,
                                          this.radioButton1,
                                          this.radioButton2,
                                          this.radioButton3,
                                          this.radioButton4});
        this.groupBox1.Location = new System.Drawing.Point(232, 48);
        this.groupBox1.Size = new System.Drawing.Size(72, 128);
        this.checkBox1.Location = new System.Drawing.Point(8, 32);
        this.checkBox1.Size = new System.Drawing.Size(56, 24);
        this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
        this.radioButton1.Location = new System.Drawing.Point(8, 64);
        this.radioButton1.Size = new System.Drawing.Size(16, 24);
        this.radioButton2.Location = new System.Drawing.Point(40, 64);
        this.radioButton2.Size = new System.Drawing.Size(16, 24);
        this.radioButton3.Location = new System.Drawing.Point(8, 96);
        this.radioButton3.Size = new System.Drawing.Size(16, 24);
        this.radioButton4.Location = new System.Drawing.Point(40, 96);
        this.radioButton4.Size = new System.Drawing.Size(16, 24);

        this.label2.Location = new System.Drawing.Point(328, 16);
        this.label2.Size = new System.Drawing.Size(200, 240);

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(536, 266);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.groupBox1,
                                      this.label1,
                                      this.label2});
        this.groupBox1.ResumeLayout(false);
        this.ResumeLayout(false);
        this.Text = "Zooming";
        this.label1.Text = "";
        this.groupBox1.Text = "Zoom";
        this.checkBox1.Text = "Paint";
        this.radioButton1.Checked = false;
        this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
        this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
        this.radioButton3.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
        this.radioButton4.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
    }

    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(PaintEventArgs e) { ImageZoom(); }
    private void checkBox1_CheckedChanged(object sender, System.EventArgs e) { ImageZoom(); }
    private void radioButtons_CheckedChanged(object sender, System.EventArgs e) { ImageZoom(); }
    protected void ImageZoom() {
        Graphics g1 = Graphics.FromHwnd(this.label1.Handle);
        Graphics g2 = Graphics.FromHwnd(this.label2.Handle);
        Rectangle rec;
        Rectangle recPart;

        if (this.checkBox1.Checked) {
            if (im == null) ReadImage();

            rec = new Rectangle(0, 0, label1.Width, label1.Height);
            g1.DrawImage(im, rec);

            recPart = new Rectangle(im.Width / 4, im.Height / 4, im.Width / 2, im.Height / 2);
            if (this.radioButton1.Checked)
                recPart = new Rectangle(0, 0, im.Width / 2, im.Height / 2);
            if (this.radioButton2.Checked)
                recPart = new Rectangle(im.Width / 2, 0, im.Width / 2, im.Height / 2);
            if (this.radioButton3.Checked)
                recPart = new Rectangle(0, im.Height / 2, im.Width / 2, im.Height / 2);
            if (this.radioButton4.Checked)
                recPart = new Rectangle(im.Width / 2, im.Height / 2, im.Width / 2, im.Height / 2);
            g2.DrawImage(im, rec, recPart, GraphicsUnit.Pixel);
        } else {
            Clear(g1);
            Clear(g2);
        }

        g1.Dispose(); g2.Dispose();
    }
    protected void ReadImage() {
        string path = "3.BMP";
        im = Image.FromFile(path);
        this.radioButton1.Enabled = true;
        this.radioButton2.Enabled = true;
        this.radioButton3.Enabled = true;
        this.radioButton4.Enabled = true;
    }
    protected void Clear(Graphics g) {
        g.Clear(this.BackColor);
        g.Dispose();
        im = null;
        im2 = null;
        this.radioButton1.Checked = false;
        this.radioButton2.Checked = false;
        this.radioButton3.Checked = false;
        this.radioButton4.Checked = false;

        this.radioButton1.Enabled = false;
        this.radioButton2.Enabled = false;
        this.radioButton3.Enabled = false;
        this.radioButton4.Enabled = false;
    }
}

   
    
    
  








Related examples in the same category

1.Load Image from an image file with Exception handler
2.Image.FromStream: load image from stream
3.Shrink ImageShrink Image
4.Shear ImageShear Image
5.Clone ImageClone Image
6.Get Image Resolution and Image size and Display sizeGet Image Resolution and Image size and Display size
7.Draw image based on its sizeDraw image based on its size
8.Set image resolution and paint itSet image resolution and paint it
9.Load image and displayLoad image and display
10.Fill Ellipse with image based Texture BrushFill Ellipse with image based Texture Brush
11.Image Save
12.Image Open
13.Thumbnail Image
14.Draw on Pixel-Size Image
15.Draw text on an Image
16.Partial Image Rotate
17.Partial Image Stretch
18.Draw Partial Image
19.Image At Points (Draw part of the image)
20.Image Reflection
21.Image Scale Isotropic
22.Image Scale To Rectangle
23.Center Pixel-Size Image by using Image.Width and Image.Height
24.Center an Image (VerticalResolution, HorizontalResolution)
25.Load image from a URL(Web) and draw it
26.Scribble with Bitmap
27.Image Flipping and Rotating
28.Image.GetThumbnailImage
29.Create Thumbnail
30.Image Resize
31.Get Bitmap Source
32.Get Bytes From BitmapSource
33.Load Image
34.Create Icon from Image
35.Resizes an image
36.Get Gravatar Image
37.Changing image opacity, Resize image, crop image and gray scale image
38.Resize Image
39.Resize image to fit
40.Image Filter
41.Save Window/Canvas to Image
42.Image Cache
43.Saves the image to a bytes array. Used to store an image into DB.
44.Get the "energy" of a pixel