標籤:style blog http io ar color os 使用 sp
一、System.Drawing.Bitmap
Bitmap 類: 封裝GDI+ 位元影像,此位元影像由圖形映像及其屬性的像素資料群組成。Bitmap 是用於處理由像素定義的映像的對象
命名空間: System.Drawing
程式集: System.Drawing.dll
繼承關係:
原型定義:
[SerializableAttribute][ComVisibleAttribute(true)]public sealed class Bitmap : Image
備忘:
GDI+ 支援下列檔案格式:BMP、GIF、EXIF、JPG、PNG 和 TIFF
構造器:
// 從指定的現有映像初始化 Bitmap 類的新執行個體public Bitmap(Image original) // 從指定的資料流初始化 Bitmap 類的新執行個體public Bitmap(Stream stream) // 從指定的檔案初始化 Bitmap 類的新執行個體 (filename 位元影像檔案的名稱和路徑)public Bitmap(string filename)
注意:在 在釋放 Bitmap 之前,此filename 對應的檔案將一直保持鎖定狀態
常用執行個體方法:
1. 擷取指定像素的顏色public Color GetPixel(int x,int y) 參數:x : 指定像素的 x 座標y : 指定像素的 y 座標傳回值:System.Drawing.Color2. 設定指定像素的顏色public void SetPixel(int x,int y,Color color)參數:x : 指定像素的 x 座標y : 指定像素的 y 座標color: 顏色3. 將 Bitmap 鎖定到系統記憶體中[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]public BitmapData LockBits(Rectangle rect,ImageLockMode flags,PixelFormat format)參數:rect: 指定要鎖定 Bitmap 部分 flags: 指定 Bitmap 的存取層級(讀/寫)format: 指定此 Bitmap 的資料格式傳回值:BitmapData 包含有關此鎖定操作的資訊4. 從系統記憶體解鎖此 BitmapUnlockBits(BitmapData)5. 使預設的透明顏色對此 Bitmap 透明MakeTransparent() MakeTransparent(Color transparentColor)
透明化的例子
1 using System; 2 using System.Drawing; 3 4 class App 5 { 6 static void Main() 7 { 8 var img = new Bitmap(@"透明化.png"); 9 img.MakeTransparent();10 img.Save(@"透明化_處理後.png") ;11 12 // MakeTransparent(Color transparentColor) 對指定Color 也執行透明操作13 img.MakeTransparent(Color.FromArgb(0x1364C4));14 img.Save(@"透明化_處理後_0x1364C4.png");15 }16 }View Code
附本小節代碼下載
二、System.Drawing.Imaging.ImageLockMode
ImageLockMode 枚舉:指定傳遞給 LockBits 方法的標誌參數的標誌。 LockBits 方法可鎖定映像的一部分,以便讀取或寫入其像素資料
枚舉項:
ReadOnly: 指定鎖定映像的一部分以便讀取
WriteOnly: 指定鎖定映像的一部分以便寫入
ReadWrite: 指定鎖定映像的一部分以便讀取和寫入
三、System.Drawing.Imaging.PixelFormat
PixelFormat 枚舉:指定映像中每個像素的顏色資料的格式
枚舉項:見MSDN
四、System.Drawing.Imaging.BitmapData
BitmapData 類:指定位元影像映像的屬性。BitmapData 類由 Bitmap 類的 LockBits 和 UnlockBits 方法使用。 不可繼承
命名空間: System.Drawing.Imaging
程式集: System.Drawing.dll
繼承關係:
執行個體屬性:
Height //擷取或設定Bitmap 對象的像素高度,有時也稱掃描行數Width //擷取或設定Bitmap 對象的像素寬度,這可以看做是一個掃描行中的像素數PixelFormat: //擷取或設定此 BitmapData對象的 Bitmap 對象中像素資訊的格式Scan0 //擷取或設定位元影像中第一個像素資料的地址。它可看成是位元影像中的第一個掃描行Stride //擷取或設定 Bitmap 對象的跨距寬度(也稱為掃描寬度)
五、System.Drawing.Color
Color 結構:表示一種 ARGB 顏色(alpha、紅色、綠色、藍色)。
命名空間: System.Drawing
程式集: System.Drawing.dll
靜態屬性: 各種顏色(如 Color.Red、Color.Yellow 等)
靜態方法:
Color.FromArgb(int argb)例子:SolidBrush trnsRedBrush = new SolidBrush(Color.FromArgb(0x78FF0000));從四個 ARGB 分量(alpha、紅色、綠色和藍色)值建立 Color 結構。 儘管此方法允許為每個分量傳遞 32 位值,但每個分量的值僅限於 8 位// 78 FF 00 00public static Color FromArgb(int alpha,int red,int green,int blue)
六、例子 彩色映像簡單灰階化
公式:
Gray(i,j) = 0.229*R(i,j) + 0.587*G(i,j) + 0.114*B(i,j)
代碼
1 using System; 2 using System.Drawing; 3 using System.Drawing.Imaging; 4 5 class App 6 { 7 static void Main() 8 { 9 using(var img = new Bitmap(@"BingWallpaper.jpg"))10 {11 Rectangle rect = new Rectangle(0, 0, img.Width, img.Height);12 BitmapData bmpData = img.LockBits(rect,ImageLockMode.ReadWrite,img.PixelFormat);13 IntPtr ptr = bmpData.Scan0;14 int bytes = img.Width * img.Height * 3;15 byte[] rgbValues = new byte[bytes];16 System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);17 double colorTemp = 0;18 for (int i = 0; i < rgbValues.Length; i += 3)19 {20 colorTemp = rgbValues[i + 2] * 0.299 + rgbValues[i + 1] * 0.587 + rgbValues[i] * 0.114;21 rgbValues[i] = rgbValues[i + 1] = rgbValues[i + 2] = (byte)colorTemp;22 }23 System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);24 img.UnlockBits(bmpData);25 26 img.Save(@"BingWallpaper_灰階化.jpg");27 }28 }29 }View Code
附本小節代碼下載
效果
原圖:
灰階化圖:
類庫探源——System.Drawing.Bitmap