Friday, May 30, 2014

Graphics Transform


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

//create metrix class
using System.Drawing.Drawing2D;



using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GraphicsTransform
{
    public partial class Form1 : Form
    {
        Bitmap bmap1, bmap2, bmap3;
        Graphics g1, g2, g3;

        public Form1()
        {
            InitializeComponent();

            bmap1 = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            bmap2 = new Bitmap(pictureBox2.Width, pictureBox2.Height);
            bmap3 = new Bitmap(pictureBox3.Width, pictureBox3.Height);

            //create graphic objects
            g1 = Graphics.FromImage(bmap1);
            g2 = Graphics.FromImage(bmap2);
            g3 = Graphics.FromImage(bmap3);
        }

        private void btnDefaultXnYaxis_Click(object sender, EventArgs e)
        {
            //int xmax = 95 * bmap1.Width / 100; //95% of the bitmap
            //int ymax = 95 * bmap1.Height / 100;
            int xmax = 9 * bmap1.Width / 10; //95% of the bitmap
            int ymax = 9 * bmap1.Height / 10;

            Pen pen = new Pen(Color.Yellow, 4);
            Point p1 = new Point(0, 0);
            Point px2 = new Point(xmax, 0);
            Point py2 = new Point(0, ymax);

            //draw x - y axis
            g1.DrawLine(pen, p1, px2);
            g1.DrawLine(pen, p1, py2);

            pictureBox1.Image = bmap1;
        }

        private void btnXnYSet2LowerLeft_Click(object sender, EventArgs e)
        {
            int xmax = 95 * bmap1.Width / 100; //95% of the bitmap
            int ymax = 95 * bmap1.Height / 100;

            Pen pen = new Pen(Color.Yellow, 3);
            Point p1 = new Point(0, 0);
            Point px2 = new Point(xmax, 0);
            Point py2 = new Point(0, ymax);

            //create matrix to transform the graphics objects
            int dx = bmap2.Width / 10;
            //lower 90 % in y axis
            int dy = 9 * bmap2.Height / 10;

           //Matrix matrix = new Matrix(1, 0, 0, 1, dx, dy);
            Matrix matrix = new Matrix(1, 0, 0, -1, dx, dy);

            //apply to this graphics transform
            g2.Transform = matrix;
            //g2.TranslateTransform(dx, dy);

            //draw x - y axis
            g2.DrawLine(pen, p1, px2);
            g2.DrawLine(pen, p1, py2);

            pictureBox2.Image = bmap2;
        }

        private void btnXnYSet2Center_Click(object sender, EventArgs e)
        {
            int xmax = 95 * bmap1.Width / 100; //95% of the bitmap
            int ymax = 95 * bmap1.Height / 100;

            Pen pen = new Pen(Color.Yellow, 3);
            Point p1 = new Point(0, 0);
            Point px2 = new Point(xmax, 0);
            Point py2 = new Point(0, ymax);

            //create matrix to transform the graphics objects
            int dx = 5 * bmap2.Width / 10;
            //lower 90 % in y axis
            int dy = 5 * bmap2.Height / 10;

            Matrix matrix1 = new Matrix(1, 0, 0, 1, dx, dy);
            g3.Transform = matrix1;
            g3.DrawLine(pen, p1, px2);
            g3.DrawLine(pen, p1, py2);

            Matrix matrix2 = new Matrix(-1, 0, 0, -1, dx, dy);
            //apply to this graphics transform
            g3.Transform = matrix2;
            //draw x - y axis
            g3.DrawLine(pen, p1, px2);
            g3.DrawLine(pen, p1, py2);

            pictureBox3.Image = bmap3;
        }
    }
}
//more to read==================
/*
 * http://msdn.microsoft.com/en-us/library/system.drawing.graphics.transform(v=vs.110).aspx
 * http://msdn.microsoft.com/en-us/library/d0616edf(v=vs.110).aspx
 */

No comments:

Post a Comment