C#GDI+編程基礎(一:Graphics畫布類)


 

GDI+存在的意義:將變成與具體硬件實現細節分開。

GDI+步驟:獲取畫布,繪制圖像、處理圖像

命名空間:

using System.Drawing;//提供對GDI+基本圖形功能的訪問
using System.Drawing.Drawing2D;//提供高級的二維和矢量圖像功能
using System.Drawing.Imaging;//提供高級GDI+圖像處理功能
using System.Drawing.Printing;//提供打印相關服務
using System.Drawing.Text;//提供高級GDI+排版功能
using System.Drawing.Design;//擴展設計時,用戶界面邏輯和繪制的類。用於擴展,自定義

 

畫圖工具:

Graphics(畫布):類封裝一個GDI+繪圖圖面,提供將對象繪制到顯示設備的方法,Graphics與特定的設備上下文關聯。畫圖方法被包括在

Graphics類中,在畫任何對象之前都需要創建一個Graphics類實例作為畫圖。

創建畫布三種方法:

1.利用控件或窗體的Paint事件的PaintEventArgs

適用場景:為控件創建繪制代碼。

示例:

//窗體的Paint事件響應方法

 

  private void Form1_Paint(object sender, PaintEventArgs e)
    {
           Graphics g = e.Graphics;
    }

//直接重載控件或者窗體的OnPaint方法:

 

  protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
        }

注意:Paint事件在重繪控件時發生。

2.調用某控件或窗體的CreateGraphics方法以獲取對Graphics對象的引用,該對象表示控件或窗體的繪圖圖面。

 

適用場景:在已經存在的窗體或控件上繪圖

 

  private void button1_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.Dispose();

        }

3.由從Image繼承的任何對象創建Graphics對象

適用場景:需要更改已經存在的圖像

 

 private void button1_Click(object sender, EventArgs e)
   {
         Image img = Image.FromFile(@"images\pic.jpg");
         Graphics g=Graphics.FromImage(img);
   }

 

Graphics類主要成員方法:

名稱 說明
DrawArc 畫弧
DrawBezier 畫立體的貝塞爾曲線
DrawBeziers 畫連續立體的貝塞爾曲線
DrawClosedCurve 畫閉合曲線
DrawCurve 畫曲線
DrawEllipse 畫橢圓
DrawImage  畫圖像
DrawLine 畫線
DrawPath 通過路勁畫線和曲線
DrawPie 畫餅圖
DrawPolygon 畫多邊形
DrawRectangle 畫矩形
DrawString 繪制文字
FillEllipse 填充橢圓
FillPath 填充路勁
FillPie 填充餅圖
FillPolygon 填充多邊形
FillRectangle 填充矩形
FillRectangles 填充矩形組
FillRegion 填充區域

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM