ดาวน์โหลดไฟล์ 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);
}
วันศุกร์ที่ 26 กรกฎาคม พ.ศ. 2556
lab17 การเขียนโปรแกรมกราฟฟิกส์ด้วย GDI+1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace lab17
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen bluepen = new Pen(Color.Blue, 2);
g.DrawRectangle(bluepen, 10, 10, 100, 100);
bluepen.Dispose();
}
}
}
----------------------------------------------------
โดยการผสมค่าสี
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen bluepen = new Pen(Color.Blue, 2);
g.DrawRectangle(bluepen, 10, 10, 100, 100);
bluepen.Dispose();
Pen somePen = new Pen(Color.FromArgb(255, 120, 200));
g.DrawEllipse(somePen, 20, 20, 200, 200);
somePen.Dispose();
---------------------------------------------
โดยการใช้ methode FromName
Graphics g = e.Graphics;
Pen bluepen = new Pen(Color.Blue, 2);
g.DrawRectangle(bluepen, 10, 10, 100, 100);
bluepen.Dispose();
Pen somePen = new Pen(Color.FromArgb(255, 120, 200));
g.DrawEllipse(somePen, 20, 20, 200, 200);
somePen.Dispose();
Color col = Color.FromName("LightGreen");
this.BackColor = col;
--------------------------------------------
เปลี่ย นขนาดและสีของปากกา
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen mypen = new Pen(Color.Black, 1);
g.DrawRectangle(mypen, 10, 10, 200, 200);
mypen.Width = 4;
mypen.Color = Color.Pink;
g.DrawEllipse(mypen, 10, 10, 200, 200);
}
---------------------------------------------
เปลี่ย นชนิดของปากกาเป็นเส้นประ
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen mypen = new Pen(Color.Black, 1);
mypen.DashStyle = DashStyle.Dash;
g.DrawRectangle(mypen, 10, 10, 200, 200);
mypen.Width = 4;
mypen.Color = Color.Pink;
g.DrawEllipse(mypen, 10, 10, 200, 200);
}
------------------------------------------------
ใช้ Pen ร่วมกับ Brush
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush myBrush = new SolidBrush(Color.DarkGreen);
Pen mypen = new Pen(myBrush, 5);
g.DrawEllipse(mypen,10,10,200,200);
mypen.Dispose();
myBrush.Dispose();
}
------------------------------------------------
การใช้ Pen ร่วมกับ HatchBrush
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush myBrush = new HatchBrush(HatchStyle.DarkVertical, Color.White, Color.Violet);
Pen mypen = new Pen(myBrush, 5);
g.DrawEllipse(mypen,10,10,200,200);
mypen.Dispose();
myBrush.Dispose();
}
---------------------------------
ทดลองเปลีย น Color และ HatchStyle เป็นแบบต่างๆ
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Brush myBrush = new HatchBrush(HatchStyle.DarkVertical, Color.Red, Color.Black);
Pen mypen = new Pen(myBrush, 5);
g.DrawEllipse(mypen,10,10,200,200);
mypen.Dispose();
myBrush.Dispose();
}
วันศุกร์ที่ 12 กรกฎาคม พ.ศ. 2556
lab16 การเพิ่ม progress control ใน Dialog based Application
// lab16Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "lab16.h"
#include "lab16Dlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// Clab16Dlg dialog
Clab16Dlg::Clab16Dlg(CWnd* pParent /*=NULL*/)
: CDialogEx(Clab16Dlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void Clab16Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PROGRESS, m_progress);
DDX_Control(pDX, IDC_SLIDER, m_slider);
}
BEGIN_MESSAGE_MAP(Clab16Dlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER, &Clab16Dlg::OnNMCustomdrawSlider)
ON_WM_HSCROLL()
END_MESSAGE_MAP()
// Clab16Dlg message handlers
BOOL Clab16Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_slider.SetRange(0,100);
m_progress.SetRange (0,100);
m_progress.SetPos(1);
m_progress.SetStep(1);
return TRUE; // return TRUE unless you set the focus to a control
}
void Clab16Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void Clab16Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR Clab16Dlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void Clab16Dlg::OnNMCustomdrawSlider(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
// TODO: Add your control notification handler code here
*pResult = 0;
}
void Clab16Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
CSliderCtrl *slider;
slider = (CSliderCtrl*) pScrollBar;
int pos = 0;
if(slider ==&m_slider)
{
UpdateData(TRUE);
pos = m_slider.GetPos();
m_progress.SetPos(pos);
UpdateData(FALSE);
// TODO: Add your message handler code here and/or call default
CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}
}
สมัครสมาชิก:
บทความ (Atom)



