类库探源——System.Drawing.Bitmap

时间:2023-03-09 17:26:46
类库探源——System.Drawing.Bitmap

一、System.Drawing.Bitmap

Bitmap 类: 封装GDI+ 位图,此位图由图形图像及其属性的像素数据组成。Bitmap 是用于处理由像素定义的图像的对象

命名空间: System.Drawing

程序集:   System.Drawing.dll

继承关系:

类库探源——System.Drawing.Bitmap

原型定义:

[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 对应的文件将一直保持锁定状态

常用实例方法:

. 获取指定像素的颜色
public Color GetPixel(int x,int y)
参数:
x : 指定像素的 x 坐标
y : 指定像素的 y 坐标
返回值:
System.Drawing.Color . 设置指定像素的颜色
public void SetPixel(int x,int y,Color color)
参数:
x : 指定像素的 x 坐标
y : 指定像素的 y 坐标
color: 颜色 . 将 Bitmap 锁定到系统内存中
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public BitmapData LockBits(Rectangle rect,ImageLockMode flags,PixelFormat format)
参数:
rect: 指定要锁定 Bitmap 部分
flags: 指定 Bitmap 的访问级别(读/写)
format: 指定此 Bitmap 的数据格式
返回值:
BitmapData 包含有关此锁定操作的信息 . 从系统内存解锁此 Bitmap
UnlockBits(BitmapData) . 使默认的透明颜色对此 Bitmap 透明
MakeTransparent()
MakeTransparent(Color transparentColor)

透明化的例子

 using System;
using System.Drawing; class App
{
static void Main()
{
var img = new Bitmap(@"透明化.png");
img.MakeTransparent();
img.Save(@"透明化_处理后.png") ; // MakeTransparent(Color transparentColor) 对指定Color 也执行透明操作
img.MakeTransparent(Color.FromArgb(0x1364C4));
img.Save(@"透明化_处理后_0x1364C4.png");
}
}

附本小节代码下载

二、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

继承关系:

类库探源——System.Drawing.Bitmap

实例属性:

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 结构。 尽管此方法允许为每个分量传递 位值,但每个分量的值仅限于 位
// 78 FF 00 00
public 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)

代码

 using System;
using System.Drawing;
using System.Drawing.Imaging; class App
{
static void Main()
{
using(var img = new Bitmap(@"BingWallpaper.jpg"))
{
Rectangle rect = new Rectangle(, , img.Width, img.Height);
BitmapData bmpData = img.LockBits(rect,ImageLockMode.ReadWrite,img.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = img.Width * img.Height * ;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, , bytes);
double colorTemp = ;
for (int i = ; i < rgbValues.Length; i += )
{
colorTemp = rgbValues[i + ] * 0.299 + rgbValues[i + ] * 0.587 + rgbValues[i] * 0.114;
rgbValues[i] = rgbValues[i + ] = rgbValues[i + ] = (byte)colorTemp;
}
System.Runtime.InteropServices.Marshal.Copy(rgbValues, , ptr, bytes);
img.UnlockBits(bmpData); img.Save(@"BingWallpaper_灰度化.jpg");
}
}
}

附本小节代码下载

效果

原图:

类库探源——System.Drawing.Bitmap

灰度化图:

类库探源——System.Drawing.Bitmap

2014-12-21 更新:

1. Format24bppRgb : 每个像素24位,红色、绿色、蓝色分量分别使用8位,它们的顺序是 蓝、率、红(BGR)

2. Stride 即步幅也称扫描宽度,系统规定它必须是4的倍数

3. BmpData.Width 与 BmpData.Stride 的关系 BmpData.Stride = BmpData.Width * 3 + X(X为保留字节,用来调节,使Stride 能被4整除)

下面两幅图说明 Height、 Width 与 Stride

Scan0

|
|---------Stride-----------------|
|---------Width-------------| |    注:Width是图片(BGR作为一个单位)宽度
BGR BGR BGR BGR BGR BGR XX
BGR BGR BGR BGR BGR BGR XX
.
.
注:BGR
为一个像素,是byte[3]的数组

下图来自《C#数字图像处理算法》一书

类库探源——System.Drawing.Bitmap

4. 代码修正

 六、例子 彩色图像简单灰度化 这节的代码有问题,因为我给的图片Width正好是4的倍数,所以 Width *3 也是4的倍数,所以不需要额外字节,但如果Width不是4的倍数,代码就不能得到正确的结果了,下面将修正后的代码贴出

 using System;
using System.Drawing;
using System.Drawing.Imaging; class App
{
static void Main()
{
using(var img = new Bitmap(@"test.png"))
{
Rectangle rect = new Rectangle(, , img.Width, img.Height);
BitmapData bmpData = img.LockBits(rect,ImageLockMode.ReadWrite,img.PixelFormat);
IntPtr ptr = bmpData.Scan0; // 用步幅表示
int bytes = bmpData.Stride * bmpData.Height;
byte[] rgbValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, , bytes);
double colorTemp = ;
int bmpStride = bmpData.Stride; for(int bmpheight = ; bmpheight < bmpData.Height; bmpheight++)
{
// 只处理实际 Height , 不理会未用空间
for(int bmpwidth = ; bmpwidth < bmpData.Width *; bmpwidth += )
{
colorTemp = rgbValues[bmpheight * bmpStride + bmpwidth + ] * 0.299 +
rgbValues[bmpheight * bmpStride + bmpwidth + ] * 0.587 +
rgbValues[bmpheight * bmpStride + bmpwidth] * 0.114; rgbValues[bmpheight * bmpStride + bmpwidth] =
rgbValues[bmpheight * bmpStride + bmpwidth + ] =
rgbValues[bmpheight * bmpStride + bmpwidth + ] = (byte)colorTemp;
}
} System.Runtime.InteropServices.Marshal.Copy(rgbValues, , ptr, bytes);
img.UnlockBits(bmpData); img.Save(@"BingWallpaper_灰度化.jpg");
}
}
}