Imagenes C#
Imagenes C#
________________________________________________________________________
Captulo 3
Objetos Grficos
Los objetos grficos son la parte medular de GDI+. Se generan mediante la Clase
Graphics que define propiedades y mtodos para dibujar y rellenar objetos grficos. Cada
vez que una aplicacin necesita pintar o dibujar algo, necesita utilizar un objeto Graphics.
Esta es la razn fundamental para que comprendamos su configuracin y componentes y
veamos como usarlos en el desarrollo de grficos en lnea. Esta clase provee mtodos
para dibujar lneas, figuras, imgenes y texto. Un objeto Graphics almacena atributos del
dispositivo de pantalla y tambin de los elementos a dibujarse.
ClipBounds
CompositingMode
CompositingQuality
DpiX
DpiY
InterpolationMode
IsClipEmpty
13
http://msdn2.microsoft.com/en-us/library/system.drawing.graphics(d=ide).aspx
Junio 3 de 2006
69
Jenaro C. Paz
________________________________________________________________________
Obtiene un valor que indica si la regin de
recorte visible de este objeto Graphics est
vaca.
Obtiene o establece la relacin de escala
entre las unidades universales y las unidades
de pgina de este objeto Graphics.
Obtiene o establece la unidad de medida
utilizada para las coordenadas de pgina en
este objeto Graphics.
Obtiene o establece un valor que especifica
cmo se calcula el desplazamiento de los
pxeles durante el procesamiento de este
objeto Graphics.
Obtiene o establece el origen de
procesamiento de este objeto Graphics para
la interpolacin y los pinceles de trama.
Obtiene o establece la calidad de
procesamiento de este objeto Graphics.
Obtiene o establece el valor de correccin
de gamma para el procesado de texto.
Obtiene o establece el modo de
procesamiento del texto asociado al objeto
Graphics.
Obtiene o establece la transformacin
universal de este objeto Graphics.
Obtiene o establece el rectngulo
delimitador que corresponde a la regin de
recorte visible de este objeto Graphics.
IsVisibleClipEmpty
PageScale
PageUnit
PixelOffsetMode
RenderingOrigin
SmoothingMode
TextContrast
TextRenderingHint
Transform
VisibleClipBounds
Clear
Dispose
EndContainer
EnumerateMetafile
ExcludeClip
Flush
FromHdc
FromHdcInternal
FromHwnd
FromHwndInternal
FromImage
GetHalftonePalette
GetHashCode (se hereda de Object)
GetHdc
GetLifetimeService (se hereda de
71
Jenaro C. Paz
________________________________________________________________________
MarshalByRefObject)
GetNearestColor
GetType (se hereda de Object)
IsVisible
MeasureCharacterRanges
MeasureString
MultiplyTransform
ReleaseHdc
ReleaseHdcInternal
ResetClip
ResetTransform
Restore
RotateTransform
72
ScaleTransform
SetClip
TransformPoints
TranslateClip
TranslateTransform
DrawBezier (2)
DrawBeziers (3)
DrawClosedCurve (4)
DrawCurve (5)
Jenaro C. Paz
________________________________________________________________________
DrawEllipse (6)
DrawIcon (7)
DrawIconUnstretched (8)
DrawImage (9)
DrawImageUnscaled (10)
DrawLine (11)
DrawLines (12)
DrawPath (13)
DrawPie (14)
DrawPolygon (15)
DrawRectangle (16)
DrawRectangles (17)
DrawString (18)
74
DrawBG.aspx.cs
using
using
using
using
using
using
using
using
using
using
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
namespace JCPGraphics
{
/// <summary>
/// Summary description for DrawBG.
14
A partir de esta seccin todos los programas, codigo fuente y aplicaciones Web son propiedad intelectual
del autor.
75
Jenaro C. Paz
________________________________________________________________________
/// </summary>
public class DrawBG : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtWidth;
protected System.Web.UI.WebControls.TextBox txtHeight;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button btnBuild;
protected System.Web.UI.WebControls.Label Label2;
private void Page_Load(object sender, System.EventArgs e)
{
//
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnBuild.Click += new System.EventHandler(this.btnBuild_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnBuild_Click(object sender, System.EventArgs e)
{
string recHeight = txtHeight.Text;
string recWidth = txtWidth.Text;
Response.Write("<img border='0' src='Server_bg.aspx?valueH=" +
recHeight + "&valueW=" + recWidth + "'>");
Response.Write("<p><font color=red>Just the Background
Rectangle!</font></p>");
}
}
}
Listado 3.1. Cdigo asociado con la forma DrawBG.aspx
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
/// <summary>
/// Summary description for rectServer.
/// </summary>
public class Server_bg : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
//===================================================================
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream = canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
//===================================================================
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
// 2) Whatever you do with the Graphics object you afect
// the image object!
Graphics g = Graphics.FromImage(pixelImage);
// Fill the interior of the bg rectangle
g.FillRectangle(new SolidBrush(bgColor), bgRect);
77
Jenaro C. Paz
________________________________________________________________________
MemoryStream aStream = new MemoryStream();
pixelImage.Save(aStream,ImageFormat.Png);
// Bmp, Jpeg, Png, Gif
return aStream;
}
//===================================================================
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
// Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
78
79
Jenaro C. Paz
________________________________________________________________________
Cuando se construye un objeto Pen, pueden especificarse ciertos atributos. Por ejemplo,
un constructor Pen permite especificar el color y el ancho. En el siguiente ejemplo se
dibuja una lnea azul de ancho 2 de (10, 10) a (80, 30):
[C#]
Pen myPen = new Pen(Color.Red, 2);
myGraphics.DrawLine(myPen, 10, 10, 80, 30);
El objeto Pen tambin expone propiedades, como DashStyle, que pueden utilizarse para
especificar caractersticas de la lnea. En el siguiente cdigo se dibuja una lnea
discontinua de (10, 40) a (30, 80):
[C#]
myPen.DashStyle = DashStyle.Dash;
myGraphics.DrawLine(myPen, 10, 40, 30, 80);
Las propiedades del objeto Pen pueden utilizarse para establecer muchos ms atributos de
la lnea. Las propiedades StartCap y EndCap especifican la apariencia de los extremos de
la lnea; los extremos pueden ser lisos, cuadrados, redondeados, triangulares o pueden
tener una forma personalizada. La propiedad LineJoin permite especificar si las lneas
conectadas estn en ngulo (unidas con esquinas definidas), biseladas, redondeadas o
recortadas. En la siguiente ilustracin se muestran lneas con varios estilos de
combinacin y extremos.
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
namespace JCPGraphics
80
ServerLines.aspx.cs
using
using
using
using
System;
System.Collections;
System.ComponentModel;
System.Data;
81
Jenaro C. Paz
________________________________________________________________________
using
using
using
using
using
using
using
using
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerLines : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
// 2) Whatever you do with the Graphics object you afect
// the image object!
Graphics g = Graphics.FromImage(pixelImage);
// Fill the interior of the bg rectangle
g.FillRectangle(new SolidBrush(bgColor), bgRect);
// +++++++++++++++Begins section related with this Server
//
/*
Pens to draw lines
*/
Pen aPen1 = new Pen(Color.MediumPurple, 1);
Pen aPen2 = new Pen(Color.Fuchsia, 2);
Pen aPen3 = new Pen(Color.LemonChiffon, 3);
Pen aPen4 = new Pen(Color.LightCoral, 4);
Pen aPen5 = new Pen(Color.MediumVioletRed, 5);
// Draw line using float coordinates
float x1 = 20.0F, y1 = 20.0F;
float x2 = 200.0F, y2 = 20.0F;
g.DrawLine(aPen1, x1, y1, x2, y2);
// Draw line using Point structure
Point pt1 = new Point(20, 20);
Point pt2 = new Point(20, 200);
g.DrawLine(aPen2, pt1, pt2);
// Draw line using PointF structure
PointF ptf1 = new PointF(20.0F, 20.0F);
82
83
Jenaro C. Paz
________________________________________________________________________
3.1.2.1.1.1 Dibujando rectngulos sobre un lienzo
Dibujar rectngulos con GDI+ es parecido a dibujar lneas. Para dibujar un rectngulo
son necesarios un objeto Graphics y un objeto Pen. El objeto Graphics proporciona un
mtodo DrawRectangle, y el objeto Pen almacena atributos tales como el color y el ancho
de lnea. El objeto Pen se pasa como uno de los argumentos de mtodo DrawRectangle.
En el siguiente cdigo se dibuja un rectngulo con la esquina superior izquierda en (90,
40), un ancho de 70 y una altura de 40:
[C#]
myGraphics.DrawRectangle(myPen, 90, 40, 70, 40);
Razn por la cual en lo sucesivo solo mostraremos el cdigo relacionado con el mtodo
btnGenerar_Click().
DrawRectangle.aspx.cs
private void btnGenerar_Click(object sender, System.EventArgs e)
{
string recHeight = txtHeight.Text;
string recWidth = txtWidth.Text;
Response.Write("<img border='0' rc='ServerRectangle.aspx?valueH="
+ recHeight + "&valueW=" + recWidth + "'>");
Response.Write("<p><font color=red>Two Rectangles!</font></p>");
}
Listado 3.6. Cdigo asociado con la forma DrawRectangle.aspx
84
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerRectangle : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
// 2) Whatever you do with the Graphics object you afect
// the image object!
Graphics g = Graphics.FromImage(pixelImage);
// Fill the interior of the bg rectangle
g.FillRectangle(new SolidBrush(bgColor), bgRect);
// +++++++++++++++Begins section related with this Server
//
/*
Pens to draw lines
*/
Pen aPen1 = new Pen(Color.MediumPurple, 1);
Pen aPen2 = new Pen(Color.Fuchsia, 2);
Pen aPen3 = new Pen(Color.LemonChiffon, 3);
Pen aPen4 = new Pen(Color.LightCoral, 4);
Pen aPen5 = new Pen(Color.MediumVioletRed, 5);
g.DrawRectangle(aPen3,20, 40, 80, 120);
g.DrawRectangle(aPen4,60, 80, 140, 50);
aPen1.Dispose();
aPen2.Dispose();
aPen3.Dispose();
aPen4.Dispose();
aPen5.Dispose();
//
// +++++++++++++++Ends section related with this Server
85
Jenaro C. Paz
________________________________________________________________________
aPen1.Dispose();
aPen2.Dispose();
aPen3.Dispose();
aPen4.Dispose();
aPen5.Dispose();
//
// +++++++++++++++Ends section related with this Server
MemoryStream aStream = new MemoryStream();
pixelImage.Save(aStream, ImageFormat.Png);
// Bmp, Jpeg, Png, Gif
return aStream;
}
| Web Form Designer Generated Code |
}
}
86
87
Jenaro C. Paz
________________________________________________________________________
//
// +++++++++++++++Ends section related with this Server
MemoryStream aStream = new MemoryStream();
pixelImage.Save(aStream, ImageFormat.Png);
// Bmp, Jpeg, Png, Gif
return aStream;
}
Listado 3.8. Codigo alterno asociado a la forma ServerRectangle.aspx
88
Para dibujar una elipse, son necesarios un objeto Graphics y un objeto Pen. El objeto
Graphics proporciona el mtodo DrawEllipse, y el objeto Pen almacena atributos como el
color y el ancho de la lnea que se utiliza para representar la elipse. El objeto Pen se pasa
como uno de los argumentos del mtodo DrawEllipse. El resto de los argumentos que se
pasan al mtodo DrawEllipse especifican el rectngulo delimitador de la elipse. En el
siguiente cdigo se dibuja una elipse; el rectngulo delimitador tiene un ancho de 60, una
altura de 30 y la esquina superior izquierda en (60, 50):
[C#]
myGraphics.DrawEllipse(myPen, 60, 50, 60, 30);
Un arco es una parte de una elipse. Para dibujar un arco, se llama al mtodo DrawArc de
la clase Graphics. Los parmetros del mtodo DrawArc son los mismos que los del
mtodo DrawEllipse, a excepcin de que DrawArc precisa un ngulo inicial y un ngulo
de barrido. En el siguiente ejemplo se dibuja un arco con un ngulo inicial de 30 grados y
un ngulo de barrido de 180 grados:
[C#]
myGraphics.DrawArc(myPen, 100, 50, 140, 70, 30, 180);
89
Jenaro C. Paz
________________________________________________________________________
Veamos ahora como aplicar lo anterior para trabajar con formas Web.
Forma DrawEllipses.aspx.cs
private void btnBuild_Click(object sender, System.EventArgs e)
{
string recHeight = txtHeight.Text;
string recWidth = txtWidth.Text;
Response.Write("<img border='0' src='ServerEllipses.aspx?valueH=" +
recHeight + "&valueW=" + recWidth + "'>");
Response.Write("<p><font color=red>Three Ellipses!</font></p>");
}
Listado 3.9. Cdigo asociado con la forma DrawEllipses.aspx
Forma ServerEllipses.aspx.cs
using
using
using
using
using
using
using
using
using
using
using
using
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerEllipses : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
90
91
Jenaro C. Paz
________________________________________________________________________
Forma DrawArcs.aspx.cs
private void btnBuild_Click(object sender, System.EventArgs e)
{
string recHeight = txtHeight.Text;
string recWidth = txtWidth.Text;
Response.Write("<img border='0' src='ServerArcs.aspx?valueH=" +
recHeight + "&valueW=" + recWidth + "'>");
Response.Write("<p><font color=red>Diferent Arcs!</font></p>");
}
Listado 3.11. Codigo asociado a la forma DrawArcs.aspx
Forma ServerArcs.aspx.cs
using
using
using
using
using
using
using
using
using
using
using
using
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerArcs : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream = buildImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream buildImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
92
93
Jenaro C. Paz
________________________________________________________________________
3.1.2.1.1.1 Dibujando Polgonos sobre un lienzo
Un polgono es una forma cerrada con tres o ms lneas rectas. Por ejemplo, un tringulo
es un polgono con tres lados, un rectngulo es un polgono con cuatro lados y un
pentgono es un polgono con cinco lados. En la siguiente ilustracin se muestran varios
polgonos.
Para dibujar un polgono, son necesarios un objeto Graphics, un objeto Pen y un arreglo
de objetos Point (o PointF). El objeto Graphics proporciona el mtodo DrawPolygon.
El objeto Pen almacena atributos, como el ancho y el color de la lnea utilizada para
representar el polgono, y el arreglo de objetos Point almacena los puntos que se van a
conectar mediante lneas rectas. El objeto Pen y el arreglo de objetos Point se pasan
como argumentos del mtodo DrawPolygon. En el siguiente ejemplo se dibuja un
polgono de tres lados. Tenga en cuenta que slo existen tres puntos en myPointArray: (0,
0), (45, 25) y (25, 50). El mtodo DrawPolygon cierra el polgono de forma automtica
ya que dibuja una lnea desde (25, 50) hacia atrs hasta el punto inicial (0, 0).
[C#]
Point[] myPointArray =
{new Point(0, 0), new Point(45, 25), new Point(25, 50)};
myGraphics.DrawPolygon(myPen, myPointArray);
94
ServerPolygon.aspx.cs
using
using
using
using
using
using
using
using
using
using
using
using
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerPolygon : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
95
Jenaro C. Paz
________________________________________________________________________
// 2) Whatever you do with the Graphics object you afect
// the image object!
Graphics g = Graphics.FromImage(pixelImage);
// Fill the interior of the bg rectangle
g.FillRectangle(new SolidBrush(bgColor), bgRect);
// +++++++++++++++Begins section related with this Server
/*
Pens to draw lines
*/
Pen aPen1 = new Pen(Color.MediumPurple, 3);
Pen aPen2 = new Pen(Color.Fuchsia, 2);
Pen aPen3 = new Pen(Color.LemonChiffon, 3);
Pen aPen4 = new Pen(Color.LightCoral, 4);
Pen aPen5 = new Pen(Color.MediumVioletRed, 5);
PointF[] starArray =
{
new PointF(125.0F, 30.0F),
new PointF(147.0F, 99.0F),
new PointF(220.0F, 99.0F),
new PointF(161.0F, 142.0F),
new PointF(183.0F, 210.0F),
new PointF(125.0F, 168.0F),
new PointF(67.0F, 210.0F),
new PointF(89.0F, 142.0F),
new PointF(30.0F, 99.0F),
new PointF(103.0F, 99.0F),
new PointF(125.0F, 30.0F),
};
PointF P1 =new PointF(125.0F, 30.0F);
PointF P2 =new PointF(220.0F, 99.0F);
PointF P3 =new PointF(183.0F, 210.0F);
PointF P4 =new PointF(67.0F, 210.0F);
PointF P5 =new PointF(30.0F, 99.0F);
PointF P6 =new PointF(125.0F, 30.0F);
PointF[] polyArray =
{
P1, P2, P3, P4, P5, P6
};
PointF p1 =new PointF(147.0F, 99.0F);
PointF p2 =new PointF(161.0F, 142.0F);
PointF p3 =new PointF(125.0F, 168.0F);
PointF p4 =new PointF(89.0F, 142.0F);
PointF p5 =new PointF(103.0F, 99.0F);
PointF p6 =new PointF(147.0F, 99.0F);
PointF[] poly2Array =
{
p1, p2, p3, p4, p5, p6
};
// Draw polygon
g.DrawPolygon(aPen3,polyArray);
g.DrawPolygon(aPen1,poly2Array);
g.DrawLines(aPen2,starArray);
// Dispose of objects
aPen1.Dispose();
aPen2.Dispose();
aPen3.Dispose();
aPen4.Dispose();
96
97
Jenaro C. Paz
________________________________________________________________________
3.1.2.1.1.1 Dibujando Curvas sobre un lienzo
Una curva spline cardinal es una secuencia de curvas individuales combinadas para
formar una curva mayor. La curva spline se especifica mediante una matriz de puntos y
un parmetro de tensin. Una curva spline cardinal pasa suavemente por cada punto de la
matriz; no hay esquinas definidas ni cambios abruptos en la tensin de la curva. En la
siguiente ilustracin se muestra un conjunto de puntos y una curva spline cardinal que
pasa por cada punto del conjunto.
Una curva spline fsica es un trozo fino de madera o de otro material flexible. Antes de la
aparicin de las curvas spline matemticas, los diseadores utilizaban curvas spline
fsicas para dibujar curvas. Un diseador colocaba la curva spline en un trozo de papel y
la delimitaba con respecto a un conjunto determinado de puntos. Despus, el diseador
poda crear una curva dibujando a lo largo de la curva spline con un lpiz. Un conjunto
determinado de puntos podra generar varias curvas, en funcin de las propiedades de la
curva spline fsica. Por ejemplo, una curva spline con una resistencia alta a curvarse
genera una curva distinta a la de una curva spline extremadamente flexible.
Las frmulas que se utilizan para calcular curvas spline matemticas se basan en las
propiedades de barras flexibles, de modo que las curvas creadas por curvas spline
matemticas son similares a las curvas que se crearon una vez con las curvas spline
fsicas. Del mismo modo que las curvas spline fsicas de distinta tensin generan curvas
distintas a travs de un conjunto determinado de puntos, las curvas spline matemticas
con valores diferentes para el parmetro de tensin generan curvas diferentes a travs de
un conjunto determinado de puntos. En la siguiente ilustracin se muestran cuatro curvas
spline cardinales que pasan por el mismo conjunto de puntos. Se muestra la tensin de
cada curva spline. Tenga en cuenta que una tensin de 0 se corresponde con una tensin
fsica infinita, que obliga a la curva a tomar el camino ms corto (lneas rectas) entre
puntos. Una tensin de 1 corresponde a la ausencia de tensin fsica, que permite a la
curva spline tomar el trazado de menor curvatura total. Con valores de tensin superiores
a 1, la curva se comporta como un muelle comprimido, al que se empuja para tomar un
trazado ms largo.
98
Tenga en cuenta que las cuatro curvas spline de la ilustracin anterior comparten la
misma lnea tangente en el punto inicial. La tangente es la lnea que va desde el punto
inicial hasta el siguiente punto de la curva. De forma similar, la tangente compartida en el
punto final es la lnea que va desde el punto final hasta el punto anterior de la curva.
Para dibujar una curva spline cardinal, es necesario un objeto Graphics, un objeto Pen y
un arreglo de objetos Point. El objeto Graphics proporciona el mtodo DrawCurve, que
dibuja la curva spline, y el objeto Pen almacena atributos de la curva spline, como el
color y el ancho de lnea. El arreglo de objetos Point almacena los puntos por los que
pasar la curva. En el siguiente ejemplo se dibuja una curva spline cardinal que pasa por
los puntos de myPointArray. La tensin es el tercer parmetro.
[C#]
myGraphics.DrawCurve(myPen, myPointArray, 1.5f);
DrawCurves.aspx.cs
private void btnBuild_Click(object sender, System.EventArgs e)
{
string recHeight = txtHeight.Text;
string recWidth = txtWidth.Text;
Response.Write("<img border='0' src='ServerCurves.aspx?valueH=" +
recHeight + "&valueW=" + recWidth + "'>");
Response.Write("<p><font color=red>Drawing Open Curves!</font></p>");
}
Listado 3.15. Codigo asociado a la forma DrawCurves.aspx
ServerCurves.aspx.cs
using
using
using
using
using
using
using
using
using
using
using
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
99
Jenaro C. Paz
________________________________________________________________________
using System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerCurves : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
// 2) Whatever you do with the Graphics object you afect
// the image object!
Graphics g = Graphics.FromImage(pixelImage);
// Fill the interior of the bg rectangle
g.FillRectangle(new SolidBrush(bgColor), bgRect);
// +++++++++++++++Begins section related with this Server
//
/*
Pens to draw lines
*/
Pen aPen1 = new Pen(Color.MediumPurple, 3);
Pen aPen2 = new Pen(Color.Fuchsia, 2);
Pen aPen3 = new Pen(Color.LemonChiffon, 3);
PointF[] starArray =
{
new PointF(195.0F, 30.0F),
new PointF(217.0F, 99.0F),
new PointF(290.0F, 99.0F),
new PointF(231.0F, 142.0F),
new PointF(253.0F, 210.0F),
new PointF(195.0F, 168.0F),
new PointF(137.0F, 210.0F),
new PointF(159.0F, 142.0F),
new PointF(100.0F, 99.0F),
new PointF(173.0F, 99.0F),
new PointF(195.0F, 30.0F),
};
PointF P1 =new PointF(195.0F, 30.0F);
PointF P2 =new PointF(290.0F, 99.0F);
PointF P3 =new PointF(253.0F, 210.0F);
100
101
Jenaro C. Paz
________________________________________________________________________
Sustituyendo el mtodo canvasAndImage() por este otro
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
// 2) Whatever you do with the Graphics object you afect
// the image object!
Graphics g = Graphics.FromImage(pixelImage);
// Fill the interior of the bg rectangle
g.FillRectangle(new SolidBrush(bgColor), bgRect);
// +++++++++++++++Begins section related with this Server
//
/*
Pens to draw lines
*/
Pen aPen1 = new Pen(Color.MediumPurple, 3);
Pen aPen2 = new Pen(Color.Fuchsia, 2);
Pen aPen3 = new Pen(Color.LightCoral, 4);
PointF[] starArray =
{
new PointF(195.0F, 30.0F),
new PointF(217.0F, 99.0F),
new PointF(290.0F, 99.0F),
new PointF(231.0F, 142.0F),
new PointF(253.0F, 210.0F),
new PointF(195.0F, 168.0F),
new PointF(137.0F, 210.0F),
new PointF(159.0F, 142.0F),
new PointF(100.0F, 99.0F),
new PointF(173.0F, 99.0F),
//new PointF(195.0F, 30.0F),
};
PointF P1 =new PointF(195.0F, 30.0F);
PointF P2 =new PointF(290.0F, 99.0F);
PointF P3 =new PointF(253.0F, 210.0F);
PointF P4 =new PointF(137.0F, 210.0F);
PointF P5 =new PointF(100.0F, 99.0F);
PointF P6 =new PointF(195.0F, 30.0F);
PointF[] polyArray =
{
P1, P2, P3, P4, P5
};
PointF p1 =new PointF(217.0F, 99.0F);
PointF p2 =new PointF(231.0F, 142.0F);
PointF p3 =new PointF(195.0F, 168.0F);
PointF p4 =new PointF(159.0F, 142.0F);
PointF p5 =new PointF(173.0F, 99.0F);
PointF p6 =new PointF(217.0F, 99.0F);
PointF[] poly2Array =
{
p1, p2, p3, p4, p5
};
102
Se obtiene una imagen con curvas cerradas sobre un lienzo como lo muestra la Figura
3.17.
103
Jenaro C. Paz
________________________________________________________________________
Tenga en cuenta que la curva comienza en p1 y avanza hacia el punto de control c1. La
lnea tangente a la curva en p1 es la lnea que va de p1 a c1. Tenga en cuenta tambin que
la lnea tangente en el extremo p2 es la lnea que va de c2 a p2.
Para dibujar una curva spline de Bzier, son necesarios un objeto Graphics y un objeto
Pen. El objeto Graphics proporciona el mtodo DrawBezier, y el objeto Pen almacena
atributos como el ancho y el color de la lnea que se utiliza para representar la curva. El
objeto Pen se pasa como uno de los argumentos del mtodo DrawBezier. El resto de los
argumentos que se pasan al mtodo DrawBezier son los extremos y los puntos de
control. En el siguiente ejemplo se dibuja una curva spline de Bzier con un punto inicial
(0, 0), dos puntos de control (40, 20) y (80, 150) y un punto final (100, 10):
[C#]
myGraphics.DrawBezier(myPen, 0, 0, 40, 20, 80, 150, 100, 10);
Pierre Bzier desarroll por primera vez las curvas spline de Bzier para el diseo en la
industria del automvil. Desde entonces, han resultado muy tiles en muchos tipos de
CAD (computer-aided design, diseo asistido por computadora) y tambin se utilizan
para definir los contornos de fuentes. Las curvas spline de Bzier pueden generar una
gran variedad de formas, algunas de las cuales se muestran en la siguiente ilustracin.
104
DrawBezier.aspx.cs
private void btnBuild_Click(object sender, System.EventArgs e)
{
string recHeight = txtHeight.Text;
string recWidth = txtWidth.Text;
Response.Write("<img border='0' src='ServerBezier.aspx?valueH=" +
recHeight + "&valueW=" + recWidth + "'>");
Response.Write("<p><font color=red>Diferent Beziers!</font></p>");
}
Listado 3.17. Codigo asociado con la forma DrawBezier.aspx
ServerBezier.aspx.cs
using
using
using
using
using
using
using
using
using
using
using
using
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerBezier : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
105
Jenaro C. Paz
________________________________________________________________________
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
// 2) Whatever you do with the Graphics object you afect
// the image object!
Graphics g = Graphics.FromImage(pixelImage);
// Fill the interior of the bg rectangle
g.FillRectangle(new SolidBrush(bgColor), bgRect);
// +++++++++++++++Begins section related with this Server
//
Pens to draw lines
Pen aPen1 = new Pen(Color.MediumPurple, 2);
Pen aPen2 = new Pen(Color.Fuchsia, 2);
Pen aPen3 = new Pen(Color.LemonChiffon, 3);
// Draw a Bezier using Points
Point pt1 = new Point(20, 260);
Point pt2 = new Point(380, 40);
Point pt3 = new Point(140,30);
Point pt4 = new Point(200,350);
g.DrawBezier(aPen2, pt1,pt3,pt4,pt2);
// Draw a Bezier using an Array of Points
PointF p1 = new PointF(20.0F, 50.0F);
PointF p2 = new PointF(60.0F, 80.0F);
PointF p3 = new PointF(80.0F, 36.0F);
PointF p4 = new PointF(120.0F, 180.0F);
PointF p5 = new PointF(220.0F, 150.0F);
PointF p6 = new PointF(350.0F, 260.0F);
PointF p7 = new PointF(210.0F, 210.0F);
PointF[] ptsBezier =
{
p1, p2, p3, p4, p5, p6, p7
};
g.DrawBeziers(aPen3, ptsBezier);
// Draw Bziers
g.DrawBezier(aPen1, 30, 30,
80, 70, 120, 190, 140, 70);
aPen1.Dispose();
aPen2.Dispose();
aPen3.Dispose();
//
// +++++++++++++++Ends section related with this Server
MemoryStream aStream = new MemoryStream();
pixelImage.Save(aStream,ImageFormat.Png);
// Bmp, Jpeg, Png, Gif
return aStream;
}
| Web Form Designer Generated Code |
}
}
Listado 3.18. Codigo asociado a la forma ServerBezier.aspx
106
107
Jenaro C. Paz
________________________________________________________________________
108
ServerPath.aspx.cs
using
using
using
using
using
using
using
using
using
using
using
using
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerPath : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
109
Jenaro C. Paz
________________________________________________________________________
// 1) Will be used to build the new Graphics object
// 2) Whatever you do with the Graphics object you afect
// the image object!
Graphics g = Graphics.FromImage(pixelImage);
// Fill the interior of the bg rectangle
g.FillRectangle(new SolidBrush(bgColor), bgRect);
// +++++++++++++++Begins section related with this Server
//
//
Pens to draw lines
Pen aPen1 = new Pen(Color.MediumPurple, 2);
Pen aPen2 = new Pen(Color.Fuchsia, 2);
Pen aPen3 = new Pen(Color.LemonChiffon, 3);
Pen aPen4 = new Pen(Color.LightCoral, 4);
Pen aPen5 = new Pen(Color.MediumVioletRed, 5);
// Draw line using PointF structure
PointF ptf1 = new PointF(20.0F, 20.0F);
PointF ptf2 = new PointF(200.0F, 200.0F);
GraphicsPath path = new GraphicsPath();
// Add a line to the path
path.AddLine(ptf1,ptf2);
// Add an ellipse to the path
path.AddEllipse(100, 50, 100, 100);
// Add three more lines
path.AddLine(X1, Y1, X2, Y2);
path.AddLine(Xa, Ya, Xb, Yb);
//path.AddLine(195, 120, 300, 120);
// Create a rectangle and call
// AddRectangle
Rectangle rect = new Rectangle(50, 110, 180, 60);
path.AddRectangle(rect);
// Draw path
g.DrawPath(aPen1, path);
aPen1.Dispose();
aPen2.Dispose();
aPen3.Dispose();
aPen4.Dispose();
aPen5.Dispose();
//
// +++++++++++++++Ends section related with this Server
MemoryStream aStream = new MemoryStream();
pixelImage.Save(aStream,ImageFormat.Png);
// Bmp, Jpeg, Png, Gif
return aStream;
}
| Web Form Designer Generated Code |
}
}
Listado 3.20. Cdigo asociado a la forma ServerPath.aspx
110
ServerIcon.aspx.cs
using
using
using
using
using
using
using
using
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
111
Jenaro C. Paz
________________________________________________________________________
namespace JCPGraphics
{
public class ServerIcon : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
// 2) Whatever you do with the Graphics object you afect
// the image object!
Graphics g = Graphics.FromImage(pixelImage);
// Fill the interior of the bg rectangle
g.FillRectangle(new SolidBrush(bgColor), bgRect);
// +++++++++++++++Begins section related with this Server
//
//
Read the Icons from Folder (root)
Icon icon1 = new Icon(Server.MapPath("ar.ico"));
Icon icon2 = new Icon(Server.MapPath("be.ico"));
Icon icon3 = new Icon(Server.MapPath("br.ico"));
Icon icon4 = new Icon(Server.MapPath("ca.ico"));
Icon icon5 = new Icon(Server.MapPath("mx.ico"));
Icon icon6 = new Icon(Server.MapPath("uk.ico"));
int x1 = 20;
int y1 = 50;
int x2 = 60;
int y2 = 90;
int x3 = 100;
int y3 = 130;
int x4 = 140;
int y4 = 170;
int x5 = 180;
int y5 = 210;
int x6 = 220;
int y6 = 250;
g.DrawIcon(icon1, x1, y1);
g.DrawIcon(icon2, x2, y2);
g.DrawIcon(icon3, x3, y3);
g.DrawIcon(icon4, x4, y4);
112
113
Jenaro C. Paz
________________________________________________________________________
3.1.2.1.1.1 Dibujando Imgenes sobre un lienzo
DrawImage.aspx.cs
private void btnBuild_Click(object sender, System.EventArgs e)
{
string recHeight = txtHeight.Text;
string recWidth = txtWidth.Text;
Response.Write("<img border='0' src='ServerImage.aspx?valueH=" +
recHeight +"&valueW=" + recWidth + "'>");
Response.Write("<p><font color=red>Draw an Image!</font></p>");
}
Listado 3.23. Cdigo asociado con la forma DrawImage.aspx
ServerImage.aspx.cs
using
using
using
using
using
using
using
using
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerImage : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
// 2) Whatever you do with the Graphics object you afect
// the image object!
114
115
Jenaro C. Paz
________________________________________________________________________
3.1.2.1.1.1 Dibujando Pasteles sobre un lienzo
DrawPies.aspx.cs
private void btnGenerar_Click(object sender, System.EventArgs e)
{
string recHeight = txtHeight.Text;
string recWidth = txtWidth.Text;
Response.Write("<img border='0' src='ServerPies.aspx?valueH=" +
recHeight + "&valueW=" + recWidth + "'>");
Response.Write("<p><font color=red>Three Pies!</font></p>");
}
Listado 3.25. Cdigo asociado con la forma DrawPies.aspx
ServerPies.aspx.cs
using
using
using
using
using
using
using
using
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerPies : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
// 2) Whatever you do with the Graphics object you afect
// the image object!
Graphics g = Graphics.FromImage(pixelImage);
116
117
Jenaro C. Paz
________________________________________________________________________
3.1.2.1.1.1 Dibujando Cadenas sobre un lienzo
DrawLetters.aspx.cs
private void btnGenerar_Click(object sender, System.EventArgs e)
{
string recHeight = txtHeight.Text;
string recWidth = txtWidth.Text;
Response.Write("<img border='0' src='ServerStrings.aspx?valueH="
+ recHeight +"&valueW=" + recWidth + "'>");
Response.Write("<p><font color=red>Just Read This!</font></p>");
}
Listado 3.27. Codigo asociado con la forma DrawLetters.aspx
ServerStrings.aspx.cs
using
using
using
using
using
using
using
using
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerStrings : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
// 2) Whatever you do with the Graphics object you afect
// the image object!
Graphics g = Graphics.FromImage(pixelImage);
// Fill the interior of the bg rectangle
118
119
Jenaro C. Paz
________________________________________________________________________
", B:"+bgColor.B.ToString() +
", G:"+bgColor.G.ToString(),new Font("Verdana", 14),
new SolidBrush(Color.Blue),
new Point(42, 20) );
float h = bgColor.GetHue();
float s = bgColor.GetSaturation();
float v = bgColor.GetBrightness();
g.DrawString("Hue: "+ h.ToString() + "\n" +
"Saturation: "+ s.ToString() + "\n" +
"Brightness: "+ v.ToString(),new Font("Verdana", 14),
new SolidBrush(Color.Blue),
new Point(42, 70) );
// +++++++++++++++Ends section related with this Server
Listado 3.29. Cdigo asociado a la forma ServerStrings.aspx modificado
FillEllipse (2)
120
FillPath (3)
FillPie (4)
FillPolygon (5)
FillRectangle (6)
FillRectangles (7)
ServerFillCCurves.aspx.cs
using System;
121
Jenaro C. Paz
________________________________________________________________________
using
using
using
using
using
using
using
using
using
using
using
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerFillCCurves : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
// 2) Whatever you do with the Graphics object you afect
// the image object!
Graphics g = Graphics.FromImage(pixelImage);
// Fill the interior of the bg rectangle
g.FillRectangle(new SolidBrush(bgColor), bgRect);
// +++++++++++++++Begins section related with this Server
SolidBrush lemonBrush = new SolidBrush(Color.LemonChiffon);
SolidBrush fuchsiaBrush = new SolidBrush(Color.Fuchsia);
SolidBrush coralBrush = new SolidBrush(Color.LightCoral);
PointF[] starArray =
{
new PointF(195.0F,
new PointF(217.0F,
new PointF(290.0F,
new PointF(231.0F,
new PointF(253.0F,
new PointF(195.0F,
new PointF(137.0F,
new PointF(159.0F,
122
30.0F),
99.0F),
99.0F),
142.0F),
210.0F),
168.0F),
210.0F),
142.0F),
P1
P2
P3
P4
P5
P6
=new
=new
=new
=new
=new
=new
PointF(195.0F,
PointF(290.0F,
PointF(253.0F,
PointF(137.0F,
PointF(100.0F,
PointF(195.0F,
30.0F);
99.0F);
210.0F);
210.0F);
99.0F);
30.0F);
PointF[] polyArray =
{
P1, P2, P3, P4, P5
};
g.FillClosedCurve(lemonBrush,polyArray,
System.Drawing.Drawing2D.FillMode.Winding, 2.0F);
g.FillClosedCurve(coralBrush, polyArray,
System.Drawing.Drawing2D.FillMode.Winding, 1.0F);
g.FillClosedCurve(fuchsiaBrush, starArray,
System.Drawing.Drawing2D.FillMode.Winding, 0.8F);
// +++++++++++++++Ends section related with this Server
MemoryStream aStream = new MemoryStream();
pixelImage.Save(aStream,ImageFormat.Png);
// Bmp, Jpeg, Png, Gif
return aStream;
}
| Web Form Designer Generated Code |
}
}
Listado 3.31. Cdigo asociado con la forma ServerFillCCurves.aspx
123
Jenaro C. Paz
________________________________________________________________________
3.1.2.2.1.2 Rellenando elipses
FillEllipses.aspx.cs
private void btnGenerar_Click(object sender, System.EventArgs e)
{
string recHeight = txtHeight.Text;
string recWidth = txtWidth.Text;
Response.Write("<img border='0'
src='ServerFillEllipses.aspx?valueH=" + recHeight +
"&valueW=" + recWidth + "'>");
Response.Write("<p><font color=red>Fill Closed
Curves!</font></p>");
}
Listado 3.32. Cdigo asociado a la forma FillEllipses.aspx
ServerFillEllipses.aspx.cs
using
using
using
using
using
using
using
using
using
using
using
using
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerFillEllipses : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
124
.
Figura 3.31. Rellenado de tres elipses
125
Jenaro C. Paz
________________________________________________________________________
3.1.2.2.1.3 Rellenando trazos
FillPath.aspx.cs
private void btnGenerar_Click(object sender, System.EventArgs e)
{
string recHeight = txtHeight.Text;
string recWidth = txtWidth.Text;
Response.Write("<img border='0'
src='ServerFillPath.aspx?valueH=" + recHeight +
"&valueW=" + recWidth + "'>");
Response.Write("<p><font color=red>Fill Closed
Curves!</font></p>");
}
Listado 3.34. Cdigo asociado a la forma FillPath.aspx
ServerFillPath.aspx.cs
using
using
using
using
using
using
using
using
using
using
using
using
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerFillPath : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
126
127
Jenaro C. Paz
________________________________________________________________________
El resultado de la interaccin entre estas dos formas genera la imagen mostrada en la
Figura 3.32
.
Figura 3.32. Rellenado de un trazo
ServerFillPies.aspx.cs
using
using
using
using
using
using
using
using
using
128
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
129
Jenaro C. Paz
________________________________________________________________________
El resultado de la interaccin entre estas dos formas genera la imagen mostrada en la
Figura 3.33
ServerFillPolygon.aspx.cs
using
using
using
using
using
using
using
130
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerFillPolygon : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
// 2) Whatever you do with the Graphics object you afect
// the image object!
Graphics g = Graphics.FromImage(pixelImage);
// Fill the interior of the bg rectangle
g.FillRectangle(new SolidBrush(bgColor), bgRect);
// +++++++++++++++Begins section related with this Server
PointF P1 =new PointF(125.0F, 30.0F);
PointF P2 =new PointF(220.0F, 99.0F);
PointF P3 =new PointF(183.0F, 210.0F);
PointF P4 =new PointF(67.0F, 210.0F);
PointF P5 =new PointF(30.0F, 99.0F);
PointF P6 =new PointF(125.0F, 30.0F);
PointF[] polyArray = {
P1, P2, P3, P4, P5, P6
};
PointF p1 =new PointF(147.0F, 99.0F);
PointF p2 =new PointF(161.0F, 142.0F);
PointF p3 =new PointF(125.0F, 168.0F);
PointF p4 =new PointF(89.0F, 142.0F);
PointF p5 =new PointF(103.0F, 99.0F);
PointF p6 =new PointF(147.0F, 99.0F);
PointF[] poly2Array = {
p1, p2, p3, p4, p5, p6
};
131
Jenaro C. Paz
________________________________________________________________________
SolidBrush lemonBrush = new SolidBrush(Color.LemonChiffon);
SolidBrush purpleBrush = new SolidBrush(Color.MediumPurple);
g.FillPolygon(lemonBrush,polyArray);
g.FillPolygon(purpleBrush,poly2Array);
// +++++++++++++++Ends section related with this Server
MemoryStream aStream = new MemoryStream();
pixelImage.Save(aStream,ImageFormat.Png);
// Bmp, Jpeg, Png, Gif
return aStream;
}
| Web Form Designer Generated Code |
}
}
Listado 3.39. Cdigo asociado con la forma ServerFillPolygon.aspx
132
ServerFillRectangle.aspx.cs
using
using
using
using
using
using
using
using
using
using
using
using
System;
System.Collections;
System.ComponentModel;
System.Data;
System.Drawing;
System.Web;
System.Web.SessionState;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.HtmlControls;
System.IO;
System.Drawing.Imaging;
namespace JCPGraphics
{
public class ServerFillRectangle : System.Web.UI.Page
{
int w1,h1;
Color bgColor=Color.LightGray;
private void Page_Load(object sender, System.EventArgs e)
{
w1 = UInt16.Parse(Request.QueryString["valueW"]);
h1 = UInt16.Parse(Request.QueryString["valueH"]);
// Create a Stream in memory to save the image
MemoryStream memStream
= canvasAndImage();
Response.Clear();
// Send the memory image to Browser in binary mode
memStream.WriteTo(Response.OutputStream);
}
public MemoryStream canvasAndImage()
{
// Define a Rectangle to be the BG rectangle
Rectangle bgRect = new Rectangle(0,0,w1,h1);
// The bitmap Object used to work with images defined by
133
Jenaro C. Paz
________________________________________________________________________
// pixel data
Bitmap pixelImage = new Bitmap(w1,h1);
// 1) Will be used to build the new Graphics object
// 2) Whatever you do with the Graphics object you afect
// the image object!
Graphics g = Graphics.FromImage(pixelImage);
// Fill the interior of the bg rectangle
g.FillRectangle(new SolidBrush(bgColor), bgRect);
// +++++++++++++++Begins section related with this Server
Rectangle myRect2 = new Rectangle(20,20,w1-40,h1-40);
g.FillRectangle(new SolidBrush(Color.Chocolate), myRect2);
// +++++++++++++++Ends section related with this Server
MemoryStream aStream = new MemoryStream();
pixelImage.Save(aStream,ImageFormat.Png);
// Bmp, Jpeg, Png, Gif
return aStream;
}
| Web Form Designer Generated Code |
}
}
Listado 3.41. Cdigo asociado con la forma ServerFillRectangle.aspx
134