| 方法 | 核心 API | 速度 | 安全性 | 適用場景 |
|---|---|---|---|---|
| 1. 提取像素法 | Bitmap.GetPixel/SetPixel | 最慢(每次呼叫跨 Managed/Unmanaged 邊界) | 完全安全 | 原型驗證、教學、極小圖片(<100x100) |
| 2. 記憶體複製法 | Bitmap.LockBits + Marshal.Copy | 快(一次複製整張圖到陣列) | 安全 | 批次處理、中大型圖片、需跨執行緒 |
| 3. Unsafe 指標法 | Bitmap.LockBits + unsafe byte* | 最快(直接操作非受控記憶體) | 需 unsafe 區塊、專案啟用 "Allow unsafe code" | 高效能即時處理、影片逐幀、大量圖片批次 |
標準亮度加權(ITU-R BT.601):
Gray = 0.299 * R + 0.587 * G + 0.114 * B
簡化版(僅取 G 通道):
Gray = G // 速度最快,視覺差異極小
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Diagnostics;
public class GrayscaleDemo {
public static void Main() {
using (var src = new Bitmap("test.jpg")) {
// 方法 1:GetPixel/SetPixel
var sw = Stopwatch.StartNew();
using (var bmp1 = ToGrayscale_GetPixel(src)) {
sw.Stop();
Console.WriteLine($"GetPixel: {sw.ElapsedMilliseconds} ms");
bmp1.Save("gray_getpixel.jpg");
}
// 方法 2:LockBits + Marshal.Copy
sw.Restart();
using (var bmp2 = ToGrayscale_LockBits(src)) {
sw.Stop();
Console.WriteLine($"LockBits+Copy: {sw.ElapsedMilliseconds} ms");
bmp2.Save("gray_lockbits.jpg");
}
// 方法 3:Unsafe 指標
sw.Restart();
using (var bmp3 = ToGrayscale_Unsafe(src)) {
sw.Stop();
Console.WriteLine($"Unsafe Pointer: {sw.ElapsedMilliseconds} ms");
bmp3.Save("gray_unsafe.jpg");
}
}
}
// 1. GetPixel/SetPixel
static Bitmap ToGrayscale_GetPixel(Bitmap src) {
var dst = new Bitmap(src.Width, src.Height, PixelFormat.Format24bppRgb);
for (int y = 0; y < src.Height; y++) {
for (int x = 0; x < src.Width; x++) {
Color c = src.GetPixel(x, y);
int gray = (int)(c.R * 0.299 + c.G * 0.587 + c.B * 0.114);
dst.SetPixel(x, y, Color.FromArgb(gray, gray, gray));
}
}
return dst;
}
// 2. LockBits + Marshal.Copy
static Bitmap ToGrayscale_LockBits(Bitmap src) {
var dst = new Bitmap(src.Width, src.Height, PixelFormat.Format24bppRgb);
var rect = new Rectangle(0, 0, src.Width, src.Height);
var srcData = src.LockBits(rect, ImageLockMode.ReadOnly, src.PixelFormat);
var dstData = dst.LockBits(rect, ImageLockMode.WriteOnly, dst.PixelFormat);
int bytes = Math.Abs(srcData.Stride) * src.Height;
byte[] rgb = new byte[bytes];
Marshal.Copy(srcData.Scan0, rgb, 0, bytes);
// Format24bppRgb: BGR 順序,每像素 3 bytes
for (int i = 0; i < rgb.Length; i += 3) {
byte b = rgb[i];
byte g = rgb[i + 1];
byte r = rgb[i + 2];
byte gray = (byte)(r * 0.299 + g * 0.587 + b * 0.114);
rgb[i] = rgb[i + 1] = rgb[i + 2] = gray;
}
Marshal.Copy(rgb, 0, dstData.Scan0, bytes);
src.UnlockBits(srcData);
dst.UnlockBits(dstData);
return dst;
}
// 3. Unsafe 指標直接操作
static Bitmap ToGrayscale_Unsafe(Bitmap src) {
var dst = new Bitmap(src.Width, src.Height, PixelFormat.Format24bppRgb);
var rect = new Rectangle(0, 0, src.Width, src.Height);
var srcData = src.LockBits(rect, ImageLockMode.ReadOnly, src.PixelFormat);
var dstData = dst.LockBits(rect, ImageLockMode.WriteOnly, dst.PixelFormat);
unsafe {
byte* srcPtr = (byte*)srcData.Scan0;
byte* dstPtr = (byte*)dstData.Scan0;
int stride = srcData.Stride; // 可能為負(底向上 DIB)
int width = srcData.Width;
int height = srcData.Height;
for (int y = 0; y < height; y++) {
byte* rowSrc = srcPtr + y * stride;
byte* rowDst = dstPtr + y * stride;
for (int x = 0; x < width; x++) {
byte b = rowSrc[x * 3];
byte g = rowSrc[x * 3 + 1];
byte r = rowSrc[x * 3 + 2];
byte gray = (byte)(r * 0.299 + g * 0.587 + b * 0.114);
rowDst[x * 3] = rowDst[x * 3 + 1] = rowDst[x * 3 + 2] = gray;
}
}
}
src.UnlockBits(srcData);
dst.UnlockBits(dstData);
return dst;
}
}
**結論**:LockBits + Marshal.Copy 已足應付多數場合,兼顧安全與速度;極致效能才需 Unsafe。
Stride 可能大於 Width * 3(4-byte 對齊),複製時必須用 Stride 計算行位移。Parallel.For 處理每一行,配合 LockBits 可再提升多核效能。