Páginas

domingo, 27 de noviembre de 2011

Transformaciones GDI




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
// Create a Graphics object
Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);
// Create a blue pen with width of 2
Pen bluePen = new Pen(Color.Blue, 2);
Point pt1 = new Point(10, 10);
Point pt2 = new Point(20, 20);
Color[] lnColors = { Color.Black, Color.Red };
Rectangle rect1 = new Rectangle(10, 10, 15, 15);
// Create two linear gradient brushes
LinearGradientBrush lgBrush1 = new LinearGradientBrush
(rect1, Color.Blue, Color.Green, LinearGradientMode.BackwardDiagonal);
LinearGradientBrush lgBrush = new LinearGradientBrush
(pt1, pt2, Color.Red, Color.Green);
// Set linear colors
lgBrush.LinearColors = lnColors;
// Set gamma correction
lgBrush.GammaCorrection = true;
// Fill and draw rectangle and ellipses
g.FillRectangle(lgBrush, 150, 0, 50, 100);
g.DrawEllipse(bluePen, 0, 0, 100, 50);
g.FillEllipse(lgBrush1, 300, 0, 100, 100);
// Apply scale transformation
g.ScaleTransform(1, 0.5f);
// Apply translate transformation
g.TranslateTransform(50, 0, MatrixOrder.Append);
// Apply rotate transformation
g.RotateTransform(30.0f, MatrixOrder.Append);
// Fill ellipse
g.FillEllipse(lgBrush1, 300, 0, 100, 100);
// Rotate again
g.RotateTransform(15.0f, MatrixOrder.Append);
// Fill rectangle
g.FillRectangle(lgBrush, 150, 0, 50, 100);
// Rotate again
g.RotateTransform(15.0f, MatrixOrder.Append);
// Draw ellipse
g.DrawEllipse(bluePen, 0, 0, 100, 50);
// Dispose of objects
lgBrush1.Dispose();
lgBrush.Dispose();
bluePen.Dispose();
g.Dispose();
}

private void button2_Click(object sender, EventArgs e)
{
// Create a Graphics object
Graphics g = this.CreateGraphics();
g.Clear(this.BackColor);
// Create a GraphicsPath object
GraphicsPath path = new GraphicsPath();
// Add an ellipse and a line to the
// graphics path
path.AddEllipse(50, 50, 100, 150);
path.AddLine(20, 20, 200, 20);
// Create a blue pen with a width of 2
Pen bluePen = new Pen(Color.Blue, 2);
// Create a Matrix object
Matrix X = new Matrix();
// Rotate 30 degrees
X.Rotate(30);
// Translate with 50 offset in x direction
X.Translate(50.0f, 0);
// Apply transformation on the path
path.Transform(X);
// Draw a rectangle, a line, and the path
g.DrawRectangle(Pens.Green, 200, 50, 100, 100);
g.DrawLine(Pens.Green, 30, 20, 200, 20);
g.DrawPath(bluePen, path);
// Dispose of objects
bluePen.Dispose();
path.Dispose();
g.Dispose();
}
}
}

No hay comentarios:

Publicar un comentario