Graphics.DrawImage(im, rec, recPart, GraphicsUnit.Pixel) : Graphics « System.Drawing « C# / C Sharp by API






Graphics.DrawImage(im, rec, recPart, GraphicsUnit.Pixel)

   

/*
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 ImageZoom
{
    /// <summary>
    /// Summary description for ImageZoom.
    /// </summary>
    public class ImageZoom1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.RadioButton radioButton1;
        private System.Windows.Forms.RadioButton radioButton2;
        private System.Windows.Forms.RadioButton radioButton3;
        private System.Windows.Forms.RadioButton radioButton4;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.CheckBox checkBox1;

        Image im = null;
        Image im2 = null;

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

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

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

            //
            // 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()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.radioButton3 = new System.Windows.Forms.RadioButton();
            this.radioButton4 = new System.Windows.Forms.RadioButton();
            this.label2 = new System.Windows.Forms.Label();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(8, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(200, 240);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            // 
            // groupBox1
            // 
            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.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(72, 128);
            this.groupBox1.TabIndex = 1;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "groupBox1";
            // 
            // checkBox1
            // 
            this.checkBox1.Location = new System.Drawing.Point(8, 32);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(56, 24);
            this.checkBox1.TabIndex = 1;
            this.checkBox1.Text = "checkBox1";
            this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
            // 
            // radioButton1
            // 
            this.radioButton1.Location = new System.Drawing.Point(8, 64);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(16, 24);
            this.radioButton1.TabIndex = 0;
            this.radioButton1.Text = "radioButton1";
            // 
            // radioButton2
            // 
            this.radioButton2.Location = new System.Drawing.Point(40, 64);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(16, 24);
            this.radioButton2.TabIndex = 0;
            this.radioButton2.Text = "radioButton1";
            // 
            // radioButton3
            // 
            this.radioButton3.Location = new System.Drawing.Point(8, 96);
            this.radioButton3.Name = "radioButton3";
            this.radioButton3.Size = new System.Drawing.Size(16, 24);
            this.radioButton3.TabIndex = 0;
            this.radioButton3.Text = "radioButton1";
            // 
            // radioButton4
            // 
            this.radioButton4.Location = new System.Drawing.Point(40, 96);
            this.radioButton4.Name = "radioButton4";
            this.radioButton4.Size = new System.Drawing.Size(16, 24);
            this.radioButton4.TabIndex = 0;
            this.radioButton4.Text = "radioButton1";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(328, 16);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(200, 240);
            this.label2.TabIndex = 0;
            this.label2.Text = "label1";
            // 
            // ImageZoom
            // 
            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.Name = "ImageZoom";
            this.Text = "ImageZoom";
            this.groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new ImageZoom1());
        }
        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);
                
                // Center part:
                recPart = new Rectangle(im.Width/4, im.Height/4, im.Width/2,
                    im.Height/2);
                if(this.radioButton1.Checked)  // Left-Top part
                    recPart = new Rectangle(0, 0, im.Width/2, im.Height/2);
                if(this.radioButton2.Checked)  // Right-Top part
                    recPart = new Rectangle(im.Width/2, 0, im.Width/2, im.Height/2);
                if(this.radioButton3.Checked)  // Left-Down part
                    recPart = new Rectangle(0, im.Height/2, im.Width/2, im.Height/2);
                if(this.radioButton4.Checked)  // Right-Down part
                    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 = @"szeret3.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.Graphics.Clear
2.Graphics.CopyFromScreen
3.Graphics.DashCap
4.Graphics.Dispose()
5.Graphics.DpiX
6.Graphics.DpiY
7.Graphics.DrawArc
8.Graphics.DrawBeziers
9.Graphics.DrawEllipse
10.Graphics.DrawIcon(Icon icon, int x, int y)
11.Graphics.DrawImage(Image i, int x, int y);
12.Graphics.DrawImage(Image img, int x, int y, int width, int height)
13.Graphics.DrawImage(Image, Points[])
14.Graphics.DrawLine(Pen p, int x0,int y0, int x1, int y1)
15.Graphics.DrawLine(Pen,Point point1, Point point2)
16.Graphics.DrawLines(Pen pen, Point[] points)
17.Graphics.DrawPie
18.Graphics.DrawRectangle
19.Graphics.DrawRectangles
20.Graphics.DrawString
21.Graphics.EnumerateMetafileProc
22.Graphics.FillClosedCurve
23.Graphics.FillEllipse
24.Graphics.FillEllipse(Brush brush,int x, int y,int width,int height)
25.Graphics.FillPath(Brushes.AliceBlue, myPath);
26.Graphics.FillPolygon
27.Graphics.FillRectangle
28.Graphics.FillRectangles
29.Graphics.FillRegion
30.Graphics.FromHwnd()
31.Graphics.FromImage
32.Graphics.InterpolationMode
33.Graphics.MeasureString
34.Graphics.PageScale
35.Graphics.PageUnit
36.Graphics.PixelOffsetMode
37.Graphics.ReleaseHdc
38.Graphics.RotateTransform
39.Graphics.ScaleTransform
40.Graphics.SetClip
41.Graphics.SetClip(path, (CombineMode)miCombineMode.Index)
42.Graphics.SmoothingMode
43.Graphics.TextRenderingHint
44.Graphics.Transform
45.Graphics.TranslateClip
46.Graphics.TranslateTransform
47.Graphics.VisibleClipBounds