ดาวน์โหลดไฟล์ PDF ที่นี่
วันเสาร์ที่ 10 สิงหาคม พ.ศ. 2556
วันศุกร์ที่ 9 สิงหาคม พ.ศ. 2556
lab20 การเขียนโปรแกรมกราฟฟิกส์ด้วย GDI+4
การโหลดภาพเพื่อแสดงบน Form
private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp = new Bitmap("C:\\Users\\chaninthorn\\Desktop\\ws_ALTools-_Valentine's_Quotes_1366x768.jpg");
this.SetClientSizeCore(bmp.Width + 20, bmp.Height + 20);
e.Graphics.DrawImage(bmp, 10, 10);
}
----------------------------------------------
การ Zoom out คือการกำหนดให้ Rectangle ปลายทาง เล็กกว่าขนาดจริงของภาพ
private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp = new Bitmap("C:\\Users\\chaninthorn\\Desktop\\ws_ALTools-_Valentine's_Quotes_1366x768.jpg");
Rectangle destrect = new Rectangle(10, 10, bmp.Width / 2, bmp.Height / 2);
Rectangle srcrect = new Rectangle(0, 0, bmp.Width, bmp.Height);
this.SetClientSizeCore(destrect.Width + 20, destrect.Height + 20);
e.Graphics.DrawImage(bmp,destrect, srcrect,GraphicsUnit.Pixel);
}
-------------------------------------------------
การ Zoom in คือการกำหนดให้ Rectangle ปลายทาง โตกว่า Rectangle ของภาพ ในที่จะเลือกภาพมา
private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp = new Bitmap("C:\\Users\\chaninthorn\\Desktop\\ws_ALTools-_Valentine's_Quotes_1366x768.jpg");
Rectangle destrect = new Rectangle(10, 10, bmp.Width, bmp.Height);
Rectangle srcrect = new Rectangle(0, 0, bmp.Width / 2, bmp.Height / 2);
this.SetClientSizeCore(destrect.Width + 20, destrect.Height + 20);
e.Graphics.DrawImage(bmp, destrect, srcrect, GraphicsUnit.Pixel);
}
------------------------------------------------
การพลิกและหมุนภาพ
private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp = new Bitmap("C:\\Users\\chaninthorn\\Desktop\\ws_ALTools-_Valentine's_Quotes_1366x768.jpg");
this.SetClientSizeCore(bmp.Width, bmp.Height);
Rectangle topleft = new Rectangle(0, 0, bmp.Width / 2, bmp.Height / 2);
Rectangle topright = new Rectangle(bmp.Width / 2, 0, bmp.Width / 2, bmp.Height / 2);
Rectangle bottomleft = new Rectangle(0, bmp.Height / 2, bmp.Width / 2, bmp.Height / 2);
Rectangle bottomRight = new Rectangle(bmp.Width / 2, bmp.Height / 2, bmp.Width / 2, bmp.Height / 2);
bmp.RotateFlip(RotateFlipType.RotateNoneFlipNone);
e.Graphics.DrawImage(bmp, topleft);
bmp.RotateFlip(RotateFlipType.RotateNoneFlipX);
e.Graphics.DrawImage(bmp, topright);
bmp.RotateFlip(RotateFlipType.Rotate180FlipNone);
e.Graphics.DrawImage(bmp, bottomleft);
bmp.RotateFlip(RotateFlipType.Rotate180FlipY);
e.Graphics.DrawImage(bmp, bottomRight);
}
-------------------------------------------------
การเขียนข้อความลงในภาพ
private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap bmp = new Bitmap("C:\\Users\\chaninthorn\\Desktop\\ws_ALTools-_Valentine's_Quotes_1366x768.jpg");
this.SetClientSizeCore(bmp.Width, bmp.Height);
Rectangle destrect = new Rectangle(0, 0, bmp.Width, bmp.Height);
Brush myBrush = new SolidBrush(Color.Coral);
e.Graphics.DrawImage(bmp, destrect);
e.Graphics.DrawString("I LOVE YOU จุฟๆ ^_______^",
new Font("Verdana", 25, FontStyle.Bold), myBrush, 0, 0);
}
lab19 การเขียนโปรแกรมกราฟฟิกส์ด้วย GDI+3
การระบายสีด้วย SolidBrush
private void Form1_Paint(object sender, PaintEventArgs e)
{
Point[] pt = { new Point(10,22),
new Point(188,246),
new Point(280,192),
new Point(250,48),
};
e.Graphics.FillClosedCurve(Brushes.Green, pt);
e.Graphics.DrawClosedCurve(Pens.Red, pt);
}
---------------------------------
การระบายสีด้วย HatchBrush
private void Form1_Paint(object sender, PaintEventArgs e)
{
this.SetClientSizeCore(500, 600);
HatchBrush brush;
int x = 20;
int y = 20;
foreach (HatchStyle brushStyle in Enum.GetValues(typeof(HatchStyle)))
{
brush = new HatchBrush(brushStyle, Color.Navy, Color.Yellow);
e.Graphics.FillRectangle(brush, x, y, 40, 20);
y += 30;
if ((y + 30) > this.ClientSize.Height)
{
y = 20;
x = 180;
}
}
}
------------------------------------------
การระบายสีด้วย TextureBrush
private void Form1_Paint(object sender, PaintEventArgs e)
{
Image image = Image.FromFile("C:\\Users\\chaninthorn\\Desktop\\ws_ALTools-_Valentine's_Quotes_1366x768.jpg");
TextureBrush brush = new TextureBrush(image);
Rectangle rect = new Rectangle(10, 10, 180, 150);
e.Graphics.FillEllipse(brush, rect);
}
-----------------------------------------------------------------------------------
การระบายสีด้วย TextureBrush
private void Form1_Paint(object sender, PaintEventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
LinearGradientBrush pnlGdt =
new LinearGradientBrush(panel1.ClientRectangle,
Color.Yellow, Color.Navy, 0f, true);
e.Graphics.FillRectangle(pnlGdt, panel1.ClientRectangle);
pnlGdt.Dispose();
}
private void panel2_Paint_1(object sender, PaintEventArgs e)
{
LinearGradientBrush pnlGdt =
new LinearGradientBrush(panel2.ClientRectangle,
Color.Yellow, Color.Navy, 90f, true);
e.Graphics.FillRectangle(pnlGdt, panel2.ClientRectangle);
pnlGdt.Dispose();
}
-----------------------------------------------------------------------------
การระบายสีด้วย Path Gradient Brush
namespace lab19
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
LinearGradientBrush pnlGdt =
new LinearGradientBrush(panel1.ClientRectangle,
Color.Yellow, Color.Navy, 0f, true);
e.Graphics.FillRectangle(pnlGdt, panel1.ClientRectangle);
pnlGdt.Dispose();
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
LinearGradientBrush pnlGdt =
new LinearGradientBrush(panel2.ClientRectangle,
Color.Yellow, Color.Navy, 90f, true);
e.Graphics.FillRectangle(pnlGdt, panel2.ClientRectangle);
pnlGdt.Dispose();
}
private void panel3_Paint(object sender, PaintEventArgs e)
{
GraphicsPath path = new GraphicsPath();
path.AddEllipse(panel3.ClientRectangle);
PathGradientBrush br = new PathGradientBrush(path);
br.CenterPoint = new PointF(panel3.ClientRectangle.Width / 2,
panel3.ClientRectangle.Height / 2);
br.CenterColor = Color.Navy;
br.SurroundColors = new Color[] { Color.Yellow };
e.Graphics.FillPath(br, path);
}
}
lab18 การเขียนโปรแกรมกราฟฟิกส์ด้วย GDI+2
**เพิ่มusing System.Drawing.Drawing2D; ด้วย
การวาดเส้นตรง เป็นการเชื อมต่อระหว่างจุด จำนวน 2 จุด โดยใช้ออบเจกต์ Pen
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen blackPen = new Pen(Color.Black, 3);
Point point1 = new Point(100, 100);
Point point2 = new Point(200, 100);
e.Graphics.DrawLine(blackPen, point1, point2);
blackPen.Dispose();
}
-----------------------------------------------------------------
การวาดเส้นตรงด้วย pen style และ brush
-----------------------------------------------
การวาดเส้นตรงด้วย pen style และ brush
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Red);
e.Graphics.DrawLine(pen, 10, 50, 220, 50);
pen = new Pen(Color.Green, 2);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
e.Graphics.DrawLine(pen, 10, 80, 220, 80);
pen = new Pen(Brushes.DeepSkyBlue, 4);
e.Graphics.DrawLine(pen, 10, 120, 220, 120);
pen.Dispose();
}
-----------------------------------------------
การกำหนดจุดปลายของเส้นตรงด้วย style แบบต่างๆ
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen[] objPen = new Pen[11];
for (int i = 0; i != 11; i++)
{
objPen[i] = new Pen(Color.Blue, 9);
}
objPen[0].EndCap = LineCap.AnchorMask;
objPen[2].EndCap = .LineCap.ArrowAnchor;
objPen[3].EndCap = .LineCap.DiamondAnchor;
objPen[4].EndCap = .LineCap.Flat;
objPen[5].EndCap = .LineCap.NoAnchor;
objPen[6].EndCap = .LineCap.Round;
objPen[7].EndCap = .RoundAnchor;
objPen[8].EndCap = .LineCap.Square;
objPen[9].EndCap = .LineCap.SquareAnchor;
objPen[10].EndCap = .LineCap.Triangle;
for (int i = 0; i != 11; i++)
{
e.Graphics.DrawLine(objPen[i], 10, 10 + 20 * i, 200, 10 + 20 * i);
objPen[i].Dispose();
}
}
------------------------------------------------
การวาดเส้นโค้ง
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Blue);
Point[] pt = {new Point(20,200), new Point(50,20),
new Point(100,100), new Point(150,20),
new Point(200,200)
};
e.Graphics.DrawCurve(pen, pt);
pen.Dispose();
}
--------------------------------------------
การวาดเส้นโค้งด้วย Graphics path
private void Form1_Paint(object sender, PaintEventArgs e)
{
GraphicsPath gp = new GraphicsPath ();
gp.AddCurve(new Point[]{
new Point(100,50),
new Point(105,40),
new Point(120,40),
new Point(130,65),
new Point(100,100),
}, 0.5f);
gp.AddCurve(new Point[]{
new Point(100,100),
new Point(70,65),
new Point(80,40),
new Point(95,40),
new Point(100,50),
}, 0.5f);
e.Graphics.DrawPath(Pens.Red , gp);
}
---------------------------------------------------
การวาดสีเ หลี่ย มครั้ง ละรูปเดียว
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen mypen = new Pen(Color.Red);
e.Graphics.DrawRectangle(mypen, 10, 120, 100, 100);
Rectangle rect = new Rectangle(10, 10, 100, 100);
e.Graphics.DrawRectangle(mypen, rect);
}
การวาดสีเ หลีย มพร้อมกันครัง ละหลายๆ รูป
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen curpen = new Pen(Color.Red,4);
Rectangle[] rect = { new Rectangle (20,20,120,20),
new Rectangle (20,50,120,30),
new Rectangle (20,90,120,40),
new Rectangle (20,140,120,60),
};
e.Graphics.DrawRectangles(curpen, rect);
}
-----------------------------------------
การวาดวงกลมและวงรี
private void Form1_Paint(object sender, PaintEventArgs e)
{
Rectangle rect = new Rectangle(10, 10, 120, 100);
e.Graphics.DrawEllipse(Pens.Cyan, rect);
Rectangle rect1 = new Rectangle(10, 120, 100, 100);
e.Graphics.FillEllipse(Brushes.DeepPink, rect1);
Rectangle rect2 = new Rectangle(150, 10, 120, 100);
e.Graphics.DrawEllipse(Pens.DarkSlateBlue, rect2);
Rectangle rect3 = new Rectangle(120, 120, 100, 100);
e.Graphics.FillEllipse(Brushes.Firebrick, rect3);
}
----------------------------------------------
การวาดส่วนโค้ง (Arc)
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen penEllipse = new Pen(Color.Green);
penEllipse.DashStyle = DashStyle.Dash;
e.Graphics.DrawEllipse(penEllipse, 20, 20, 200, 150);
Pen penArc = new Pen(Color.Magenta, 2);
e.Graphics.DrawArc(penArc, 20, 20, 200, 150, 45, 180);
}
---------------------------------------
การวาดรูป Pie
private void Form1_Paint(object sender, PaintEventArgs e)
{
Pen penEllipse = new Pen(Color.Green);
penEllipse.DashStyle = DashStyle.Dash;
e.Graphics.DrawEllipse(penEllipse, 20, 20, 200, 150);
Pen penPie = new Pen(Color.Magenta, 2);
e.Graphics.DrawPie(penPie, 20, 20, 200, 150, 45, 90);
Pen penPie1 = new Pen(Color.BlueViolet, 2);
e.Graphics.DrawPie(penPie1, 20, 20, 200, 150, 135, 45);
}
-------------------------------------------------
การสร้าง graphics path จากรูปต่างๆ
private void Form1_Paint(object sender, PaintEventArgs e)
{
GraphicsPath gpath = new GraphicsPath();
gpath.AddEllipse(46, 4, 28, 28);
gpath.AddLine(36, 32, 84, 32);
gpath.AddLine(100, 80, 88, 84);
gpath.AddLine(76, 50, 74, 84);
gpath.AddLine(90, 150, 74, 150);
gpath.AddLine(60, 100, 46, 150);
gpath.AddLine(32, 150, 46, 84);
gpath.AddLine(44, 50, 32, 84);
gpath.AddLine(20, 80, 36, 32);
e.Graphics.FillPath(Brushes.Blue, gpath);
}
สมัครสมาชิก:
บทความ (Atom)



