首頁 後端開發 C#.Net教程 C#中GDI+程式10個基本技巧二

C#中GDI+程式10個基本技巧二

Dec 17, 2016 am 09:53 AM

5、漸層色填充

需要使用兩個刷子:

線性梯度刷子(LinearGradientBrush)

路徑梯度刷子(PathGuadientBrush)

private void button4_ClickDlickD.ClickS. /繪圖表面

Graphics g =this.pictureBoxII1.CreateGraphics();

g.FillRectangle(Brushes.White,this.pictureBoxII1.ClientRectangle);

// LinearGradientBrush(

new Point(0, 10),

new Point(150, 10),

Color.FromArgb(255, 0, 0),

Color.FromArgb(255, 0, 0),

Color.FromArg5b(0,0);

Pen pen = new Pen(lgbrush);

 

//用線性筆刷梯度效果的筆繪製一條直線段並填滿一個矩形

g.DrawLine(pen, 10, 130, 500, 130); g.FillRectangle(lgbrush, 10, 150, 370, 30);

 

//定義路徑並增加一個橢圓


GraphicsPath gp = new GraphicsPath();

GraphicsPath gp = new GraphicsPath();

GraphicsPath gp = new GraphicsPath();

;

//用此路徑定義路徑梯度刷子

PathGradientBrush brush =

new PathGradientBrush(gp);

//顏色數組

Color[] colors = {505,00,500,500,500,505 ),

Color.FromArgb(100, 100, 100),

Color.FromArgb(0, 255, 0),

Color.FromArgb(0, 0, 255)};

float[] r = {0.0f, 0.3f, 0.6f, 1.0f};

ColorBlend blend = new ColorBlend();

blend.Colorors = colors;

.InterpolationColors = blend;

//在橢圓外填滿一個矩形

g.FillRectangle(brush, 0, 0, 210, 110);

 

//用了橢圓的刷子路徑定義第二個路徑梯度

GraphicsPath gp2 = new GraphicsPath();

gp2.AddEllipse(300, 0, 200, 100);

PathGradientBrush brush2 = new PathGradientBrush(gp2); CenterPoint = new PointF(450, 50);

brush2.CenterColor = Color.FromArgb(0, 255, 0);


//設定邊界顏色

Color[] color2 = {Color.FArgb(2555, b(2555, 25555, 2555, 25555, 25555, 25555, 25555, 25555,25555, 25555, 25555, 2555 0)};

brush2.SurroundColors = color2;

//用第二個梯度刷填滿橢圓

g.FillEllipse(brush2, 300, 0, 200, 100);的座標系統

 

通用座標系-使用者自訂座標系。

頁面座標系-虛擬座標系。

設備座標系-螢幕座標系。

 

當頁面座標系和裝置座標系的單位都是像素時,它們相同。

private void button10_Click(object sender, System.EventArgse)

{

Graphics g = this.pictureBoxII1.CreateGraphics();

g.Clear(.);

}

private void Draw(Graphics g)

{


g.DrawLine(Pens.Black, 10, 10, 100, 100);

g.D. 100);

g.DrawArc(Pens.Black, 100, 10, 100, 100, 20, 160);

g.DrawRectangle(Pens.Green, 50, 200, 150,

)

);

private void button5_Click(object sender, System.EventArgs e)

{

//左移

Graphics g = this.pictureBoxII1.CreateGraphics();

TranslateTransform(-50, 0);


this.Draw(g);

}

 

private void button6_Click(object sender, System.EventArgs e)

.pictureBoxII1.CreateGraphics();

g.Clear(Color.White);

g.TranslateTransform(50, 0);

this.Draw(g);

}
}<樣

()); , System.EventArgs e)

{

//旋轉

Graphics g = this.pictureBoxII1.CreateGraphics();

g.Clear(Color.White); this.Draw(g);

}

 

private void button8_Click(object sender, System.EventArgs e)

{

//放大

Clear(Color.White);

g.ScaleTransform(1.2f, 1.2f);

this.Draw(g);

}

 

private void button9_CArgidates

//縮小

Graphics g = this.pictureBoxII1.CreateGraphics();


g.Clear(Color.White);

g.ScaleTransform(0.8f, 0.White);

g.ScaleTransform(0.8f, 0.White); ;

}

 

7、全域座標-變換對於繪圖面上的每個圖元都會產生影響。通常用於設定通用座標系。

下程式將原點移至控制中心,且Y軸正向朝上。

//先畫一個圓

Graphics g = e.Graphics;


g.FillRectangle(Brushes.White, this.ClientRectangle);

g.DrawEllipse(Pens.Black -100,020,02,02, 200);

 

//使y軸正向朝上,必須做相對於x軸鏡像

//變換矩陣為[1,0,0,-1,0,0]

Matrix mat = new Matrix(1, 0, 0, -1, 0, 0);

g.Transform = mat;

Rectangle rect = this.ClientRectangle;

int w = rect.Width;

;

g.TranslateTransform(w/2, -h/2);

 

//以原點為中心,做一個半徑為100的圓

g.DrawEllipse(Pens.Red, -100, -100, 200, 200);

g.TranslateTransform(100, 100);

g.DrawEllipse(Pens.Green, -100, -100, 200, 200);

g.ScaleTransform(2, 2);

g.DrawEllipse(Pens.Blue, -100, -100, 200, 200 );

 

8、局部座標系-只對某些圖形進行變換,而其它圖形元素不變。

 
protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

//客戶區設定為白色

g.Fyect,I /y軸朝上

Matrix mat = new Matrix(1, 0, 0, -1, 0, 0);

g.Transform = mat;

//移動座標原點到窗體中心

Rectangle = this.ClientRectangle;

int w = rect.Width;

int h = rect.Height;

g.TranslateTransform(w/2, -h/2);

//在全域座標下繪製橢圓

g.DrawEllipse(Pens.Red, -100, -100, 200, 200);

g.FillRectangle(Brushes.Black, -108, 0, 8, 8);

g.FillRect, 100, 0, 8, 8);

g.FillRectangle(Brushes.Black, 0, 100, 8, 8);

g.FillRectangle(Brushes.Black, 0, -108, 8, 8);

//建立一個橢圓然後在局部座標系中進行變換

GraphicsPath gp = new GraphicsPath();

gp.AddEllipse(-100, -100, 200, 200);

Mamat ;

//平移

mat2.Translate(150, 150);

//旋轉

mat2.Rotate(30);

gp.Transform(mat2); gp);

 

PointF[] p = gp.PathPoints;

g.FillRectangle(Brushes.Black, p[0].X-2, p[0].Y+2, 4, 4);

g.FillRectangle(Brushes.Black, p[3].X-2, p[3].Y+2, 4, 4);

g.FillRectangle(Brushes.Black, p[6].X-4, p[6].Y-4, 4, 4);

g.FillRectangle(Brushes.Black, p[9].X-4, p[9].Y-4, 4, 4);

gp. Dispose();

//base.OnPaint (e);

}


 
9、Alpha混合

Color.FromArgb()的A就是Alpha。 Alpha的值範圍從0到255。0表示完全透明,255完全不透明。


當前色=前景色×alpha/255+背景色×(255-alpha)/255


protected override void OnPaint(PaintEventArgs e)


{pto建立一個填充矩形

SolidBrush brush = new SolidBrush(Color.BlueViolet);

g.FillRectangle(brush, 180, 70, 200, 150);

//建立一個位元圖,其中兩個位元圖之間有透明效果

Bitmap bm1 = new Bitmap(200, 100);

Graphics bg1 = Graphics.FromImage(bm1);

SolidBrush redBrush =

Sol, SolidBrush(crom, SolidBrush redBrush =

);

SolidBrush greenBrush =

new SolidBrush(Color.FromArgb(210, 0, 255, 0));

bg1.FillRectangle(redBrush, 0, m , 30, 30, 150, 70);

g.DrawImage(bm1, 100, 100);

//建立一個點陣圖,其中兩個位元圖之間沒有透明效果

200, 100);

Graphics bg2 = Graphics.FromImage(bm2);

bg2.CompositingMode = CompositingMode.資料le( greenBrush, 30, 30, 150, 70);

g.CompositingQuality = CompositingQuality.GammaCorrected;

g.DrawImage(bm2, 300, 200);H

10、反走樣

protected override void OnPaint(PaintEventArgs e)

{

Graphics g = e.Graphics;

//放大8倍

沒有反走樣的圖形和文字

Draw(g);

 
//設定反走樣

g.SmoothingMode = SmoothingMode.AntiAlias;

e

/右移40 ;

//再繪製就是反走樣之後的了


Draw(g);

//base.OnPaint (e);

}

private void Draw(Graphics g)

{

private void Draw(Graphics g)

{🜎和文字

g.DrawLine(Pens.Gray, 10, 10, 40, 20);

g.DrawEllipse(Pens.Gray, 20, 20, 30, 10);

樣檢定;

Font font = new Font("宋體", 5);

SolidBrush brush = new SolidBrush(Color.Gray);


 

g.DrawString(Color, font, brush,

 

g.DrawString(s, font, brush, 10, 40);



更多 C#中GDI+程式10個基本技巧二相關文章請關注PHP中文網!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1665
14
CakePHP 教程
1424
52
Laravel 教程
1322
25
PHP教程
1270
29
C# 教程
1249
24
c#.net的持續相關性:查看當前用法 c#.net的持續相關性:查看當前用法 Apr 16, 2025 am 12:07 AM

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

從網絡到桌面:C#.NET的多功能性 從網絡到桌面:C#.NET的多功能性 Apr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#作為多功能.NET語言:應用程序和示例 C#作為多功能.NET語言:應用程序和示例 Apr 26, 2025 am 12:26 AM

C#在企業級應用、遊戲開發、移動應用和Web開發中均有廣泛應用。 1)在企業級應用中,C#常用於ASP.NETCore開發WebAPI。 2)在遊戲開發中,C#與Unity引擎結合,實現角色控制等功能。 3)C#支持多態性和異步編程,提高代碼靈活性和應用性能。

c#.net適合您嗎?評估其適用性 c#.net適合您嗎?評估其適用性 Apr 13, 2025 am 12:03 AM

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#.NET與未來:適應新技術 C#.NET與未來:適應新技術 Apr 14, 2025 am 12:06 AM

C#和.NET通過不斷的更新和優化,適應了新興技術的需求。 1)C#9.0和.NET5引入了記錄類型和性能優化。 2).NETCore增強了雲原生和容器化支持。 3)ASP.NETCore與現代Web技術集成。 4)ML.NET支持機器學習和人工智能。 5)異步編程和最佳實踐提升了性能。

.NET中的C#代碼:探索編程過程 .NET中的C#代碼:探索編程過程 Apr 12, 2025 am 12:02 AM

C#在.NET中的編程過程包括以下步驟:1)編寫C#代碼,2)編譯為中間語言(IL),3)由.NET運行時(CLR)執行。 C#在.NET中的優勢在於其現代化語法、強大的類型系統和與.NET框架的緊密集成,適用於從桌面應用到Web服務的各種開發場景。

將C#.NET應用程序部署到Azure/AWS:逐步指南 將C#.NET應用程序部署到Azure/AWS:逐步指南 Apr 23, 2025 am 12:06 AM

如何將C#.NET應用部署到Azure或AWS?答案是使用AzureAppService和AWSElasticBeanstalk。 1.在Azure上,使用AzureAppService和AzurePipelines自動化部署。 2.在AWS上,使用AmazonElasticBeanstalk和AWSLambda實現部署和無服務器計算。

C#和.NET運行時:它們如何一起工作 C#和.NET運行時:它們如何一起工作 Apr 19, 2025 am 12:04 AM

C#和.NET運行時緊密合作,賦予開發者高效、強大且跨平台的開發能力。 1)C#是一種類型安全且面向對象的編程語言,旨在與.NET框架無縫集成。 2).NET運行時管理C#代碼的執行,提供垃圾回收、類型安全等服務,確保高效和跨平台運行。

See all articles