柚子快報邀請碼778899分享:開發(fā)語言 【C#】圖形圖像編程
柚子快報邀請碼778899分享:開發(fā)語言 【C#】圖形圖像編程
實(shí)驗(yàn)?zāi)繕?biāo)和要求:
掌握C#圖形繪制基本概念;掌握C#字體處理;能進(jìn)行C#圖形圖像綜合設(shè)計。
運(yùn)行效果如下所示:
1.功能說明與核心代碼
使用panel為畫板,完成以下設(shè)計內(nèi)容:
使用pen繪制基礎(chǔ)圖形;使用LinearGradientBrush實(shí)現(xiàn)漸變色字體;使用GraphicsPath實(shí)現(xiàn)藝術(shù)字,部分核心代碼如下所示;
GraphicsPath?gp = new?GraphicsPath(FillMode.Winding);
????????????gp.AddString(
????????????????"字體輪廓",new?FontFamily("方正舒體"),(int)FontStyle.Regular,
????????????????80,new?PointF(10, 20),new?StringFormat());
????????????Brush?brush = XXXXXXXXXXXXXXXXXXXXXX;
????????????XXX.DrawPath(Pens.Green, gp);
????????????XXX.FillPath(brush, gp);
實(shí)現(xiàn):
? ? ?1.在vs中開一個c#的窗體應(yīng)用:
? 2 .在設(shè)計界面中拉取兩個button,一個textbox(用于輸出自己想輸出的內(nèi)容(藝術(shù)字))以及一個panel(輸出在panel上顯示)
? 3.代碼
(1)button1:
private void button1_Click(object sender, EventArgs e)
{
drawBasicShapes = true;
drawArtText = false;
DrawBasicShapes();
panel1.Invalidate(); // 觸發(fā)重繪
}
(2)button2:
private void button2_Click(object sender, EventArgs e)
{
drawBasicShapes = false;
drawArtText = true;
DrawArtText();
panel1.Invalidate(); // 觸發(fā)重繪
}
(3)button1調(diào)用的函數(shù)DrawBasicShapes():
private void DrawBasicShapes()
{
// 使用 Pen 繪制基礎(chǔ)圖形
Pen blackPen = new Pen(Color.Black, 3);
drawingGraphics.DrawRectangle(blackPen, 10, 70, 100, 50);
drawingGraphics.DrawEllipse(blackPen, 10, 70, 100, 50);
// 使用 LinearGradientBrush 實(shí)現(xiàn)漸變色字體
Font font = new Font("Arial", 24);
LinearGradientBrush gradientBrush = new LinearGradientBrush(
new Rectangle(10, 20, 100, 50),
Color.Blue,
Color.Red,
45);
drawingGraphics.DrawString("直接拿捏", font, gradientBrush, new PointF(10, 150));
}
(4)button2調(diào)用的函數(shù)?DrawArtText():
private void DrawArtText()
{
string text = textBox1.Text;
using (Font font = new Font("方正舒體", 40, FontStyle.Regular))
using (GraphicsPath gp = new GraphicsPath(FillMode.Winding))
using (SolidBrush brush = new SolidBrush(Color.AliceBlue)) // 使用 SolidBrush 填充藝術(shù)字
using (Pen pen = new Pen(Color.Green, 2)) // 使用 Pen 繪制藝術(shù)字的輪廓
{
gp.AddString(
text,
font.FontFamily,
(int)font.Style,
font.Size,
new PointF(80, 30), // 設(shè)置藝術(shù)字的起始位置
StringFormat.GenericDefault);
drawingGraphics.DrawPath(pen, gp); // 繪制藝術(shù)字的輪廓
drawingGraphics.FillPath(brush, gp); // 填充藝術(shù)字
}
}
(5)panel:
private void panel1_Paint(object sender, PaintEventArgs e)
{
// 將 Bitmap 繪制到 Panel 上
e.Graphics.DrawImage(drawingBitmap, 0, 0);
}
4.代碼細(xì)節(jié)補(bǔ)充(完整代碼)
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Windows.Forms;
using System;
namespace 圖形圖像編程
{
public partial class Form1 : Form
{
// 標(biāo)志位
private bool drawBasicShapes = false;
private bool drawArtText = false;
// Bitmap 對象,用于保存繪制的圖像
private Bitmap drawingBitmap;
private Graphics drawingGraphics;
// 構(gòu)造函數(shù)
public Form1()
{
InitializeComponent();
// 初始化 Bitmap 和 Graphics 對象
drawingBitmap = new Bitmap(panel1.Width, panel1.Height);
drawingGraphics = Graphics.FromImage(drawingBitmap);
// 綁定按鈕點(diǎn)擊事件
button1.Click += new EventHandler(this.button1_Click);
button2.Click += new EventHandler(this.button2_Click);
panel1.Paint += new PaintEventHandler(this.panel1_Paint);
}
// 繪制基礎(chǔ)圖形按鈕點(diǎn)擊事件處理
private void button1_Click(object sender, EventArgs e)
{
drawBasicShapes = true;
drawArtText = false;
DrawBasicShapes();
panel1.Invalidate(); // 觸發(fā)重繪
}
// 添加藝術(shù)字按鈕點(diǎn)擊事件處理
private void button2_Click(object sender, EventArgs e)
{
drawBasicShapes = false;
drawArtText = true;
DrawArtText();
panel1.Invalidate(); // 觸發(fā)重繪
}
// 繪制基礎(chǔ)圖形的方法
private void DrawBasicShapes()
{
// 使用 Pen 繪制基礎(chǔ)圖形
Pen blackPen = new Pen(Color.Black, 3);
drawingGraphics.DrawRectangle(blackPen, 10, 70, 100, 50);
drawingGraphics.DrawEllipse(blackPen, 10, 70, 100, 50);
// 使用 LinearGradientBrush 實(shí)現(xiàn)漸變色字體
Font font = new Font("Arial", 24);
LinearGradientBrush gradientBrush = new LinearGradientBrush(
new Rectangle(10, 20, 100, 50),
Color.Blue,
Color.Red,
45);
drawingGraphics.DrawString("直接拿捏", font, gradientBrush, new PointF(10, 150));
}
// 繪制藝術(shù)字的方法
private void DrawArtText()
{
string text = textBox1.Text;
using (Font font = new Font("方正舒體", 40, FontStyle.Regular))
using (GraphicsPath gp = new GraphicsPath(FillMode.Winding))
using (SolidBrush brush = new SolidBrush(Color.AliceBlue)) // 使用 SolidBrush 填充藝術(shù)字
using (Pen pen = new Pen(Color.Green, 2)) // 使用 Pen 繪制藝術(shù)字的輪廓
{
gp.AddString(
text,
font.FontFamily,
(int)font.Style,
font.Size,
new PointF(80, 30), // 設(shè)置藝術(shù)字的起始位置
StringFormat.GenericDefault);
drawingGraphics.DrawPath(pen, gp); // 繪制藝術(shù)字的輪廓
drawingGraphics.FillPath(brush, gp); // 填充藝術(shù)字
}
}
// Panel的Paint事件處理
private void panel1_Paint(object sender, PaintEventArgs e)
{
// 將 Bitmap 繪制到 Panel 上
e.Graphics.DrawImage(drawingBitmap, 0, 0);
}
}
}
運(yùn)行結(jié)果:
小結(jié):
1.實(shí)現(xiàn)細(xì)節(jié)
·??創(chuàng)建項(xiàng)目和設(shè)計窗體:
新建一個Windows Forms應(yīng)用程序項(xiàng)目。在窗體上添加一個Panel控件和兩個Button控件以及textbox。設(shè)置Button控件的文本為“繪制基礎(chǔ)圖形”和“添加藝術(shù)字”。
·??初始化繪圖資源:
在窗體構(gòu)造函數(shù)中,初始化一個Bitmap對象和一個Graphics對象。將Bitmap對象的大小設(shè)置為Panel控件的大小。使用Graphics.FromImage方法從Bitmap對象創(chuàng)建一個Graphics對象,用于繪圖。
·??實(shí)現(xiàn)按鈕點(diǎn)擊事件處理函數(shù):
在第一個按鈕的點(diǎn)擊事件處理函數(shù)中,調(diào)用繪制基礎(chǔ)圖形的方法。在第二個按鈕的點(diǎn)擊事件處理函數(shù)中,調(diào)用繪制藝術(shù)文字的方法。在每個事件處理函數(shù)中,調(diào)用panel1.Invalidate方法,觸發(fā)重繪。
·??實(shí)現(xiàn)繪圖方法:
在繪制基礎(chǔ)圖形的方法中,使用Pen對象繪制矩形和橢圓,使用LinearGradientBrush對象繪制漸變色文字。在繪制藝術(shù)文字的方法中,使用GraphicsPath對象創(chuàng)建藝術(shù)文字的路徑,并使用SolidBrush對象填充文字。
·??處理Panel的Paint事件:
在Paint事件處理函數(shù)中,使用Graphics.DrawImage方法將Bitmap對象繪制到Panel控件上。
2.小結(jié)
本次實(shí)驗(yàn)通過使用GDI+進(jìn)行圖形和文字的繪制,成功實(shí)現(xiàn)了一個可以動態(tài)更新繪圖內(nèi)容的Windows窗體應(yīng)用程序。關(guān)鍵點(diǎn)在于使用Bitmap對象作為繪圖表面,從而保留之前的繪圖內(nèi)容。這種方法適用于需要動態(tài)更新和保留圖形內(nèi)容的應(yīng)用場景
難點(diǎn)分析
(1)Bitmap與Graphics的初始化和使用:
? ? ? ? ?初始化Bitmap和Graphics對象并確保它們的大小與Panel一致,這樣才能確保繪制內(nèi)容能夠正確顯示在Panel上。
? ? ? ? ?在Panel的Paint事件中使用Graphics.DrawImage方法將Bitmap繪制到Panel上,以實(shí)現(xiàn)內(nèi)容的保留和更新。
(2)不同繪圖操作的協(xié)調(diào):
? ? ? ? ?實(shí)現(xiàn)多個繪圖方法并確保它們能夠在同一個Bitmap對象上操作,不會互相覆蓋或清除之前的繪圖內(nèi)容。
(3)處理界面刷新:
? ? ? ? ?通過調(diào)用panel1.Invalidate方法觸發(fā)重繪,并在Paint事件中繪制Bitmap對象,這樣可以確保每次繪制新內(nèi)容時,之前的內(nèi)容不會被清除。
柚子快報邀請碼778899分享:開發(fā)語言 【C#】圖形圖像編程
參考文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。