一、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.Color 2. 设置指定像素的颜色 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. 从系统内存解锁此 Bitmap UnlockBits(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 }