首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

.NET 3.5 GDI+ 圖形操作(三):筆刷、字體與影像處理

     編輯:關於.NET

10.2.4 Brush 筆刷

Brush 為抽象基類,用於定義填充圖形內部(矩形、橢圓、扇形、多邊形、封閉路徑)的物件。無法直接實例化,需使用衍生類:SolidBrush(單色)、TextureBrush(紋理)、LinearGradientBrush(漸層)。

SolidBrush 單色筆刷

System.Drawing.Brushes 提供預定義筆刷,如 Brushes.Blue。自訂紅色筆刷:

System.Drawing.Brush brush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);

SolidBrush 常用屬性方法表

TextureBrush 紋理筆刷

使用圖片作為填充紋理:

TextureBrush 相關屬性

string brushfile = Server.MapPath("~/logo.gif");
string file = Server.MapPath("~/Element.jpg");
System.Drawing.Image brushimg = System.Drawing.Image.FromFile(brushfile);
System.Drawing.TextureBrush texture = new System.Drawing.TextureBrush(brushimg);
texture.WrapMode = System.Drawing.Drawing2D.WrapMode.Tile;

System.Drawing.Image img = System.Drawing.Image.FromFile(file);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
// 以紋理填充橢圓
g.FillEllipse(texture, new System.Drawing.RectangleF(90.0f, 110.0f, 100, 100));
g.Dispose();

Response.ContentType = "image/JPEG";
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

紋理填充效果

10.2.5 Font 字體

Font 定義文字格式(字體、大小、樣式)。常用屬性方法:

Font 屬性方法表1

Font 屬性方法表2

建立 10 點大小的新細明體:

System.Drawing.Font f1 = new System.Drawing.Font("新細明體", 10);

10.2.6 Image 影像

Image 為 Bitmap 與 Metafile 的抽象基類。常用成員:

Image 常用屬性方法表

Image 不可直接實例化,透過靜態方法建構:

System.Drawing.Image img1 = System.Drawing.Image.FromFile(Server.MapPath("~/logo.gif"));

實務上常用衍生類 Bitmap 處理像素資料。建構方式多樣:

System.Drawing.Bitmap bmp1 = new System.Drawing.Bitmap(Server.MapPath("~/logo.gif")); // 從檔案
System.Drawing.Bitmap bmp2 = new System.Drawing.Bitmap(300, 200); // 新建 300x200 畫布

Bitmap 提供 RotateFlip 實現圖像翻轉旋轉,參數為 RotateFlipType:

RotateFlipType 列舉值

System.Drawing.Bitmap bmp1 = new System.Drawing.Bitmap(Server.MapPath("~/logo.gif"));
bmp1.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipX); // 水平翻轉 90 度

Response.ContentType = "image/JPEG";
bmp1.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

旋轉翻轉效果

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved