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

C# 位元組陣列轉 8-bit 灰度影像:完整實作(一)

     編輯:關於C語言

解決網路常見實作問題:效能、Stride 處理、調色盤修正。使用 LockBits/UnlockBits 直接操作記憶體,避免 GetPixel/SetPixel 低效能。

核心實作

public static Bitmap ToGrayBitmap(byte[] rawValues, int width, int height) {
    Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
    BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, width, height),
        ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

    int stride = bmpData.Stride;
    int offset = stride - width;
    IntPtr ptr = bmpData.Scan0;
    int scanBytes = stride * height;

    byte[] pixelValues = new byte[scanBytes];
    int posScan = 0, posReal = 0;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++)
            pixelValues[posScan++] = rawValues[posReal++];
        posScan += offset; // 跳過 Stride 補齊位元組
    }

    Marshal.Copy(pixelValues, 0, ptr, scanBytes);
    bmp.UnlockBits(bmpData);

    // 修正調色盤:偽彩 → 灰度
    ColorPalette palette;
    using (Bitmap tmp = new Bitmap(1, 1, PixelFormat.Format8bppIndexed))
        palette = tmp.Palette;
    for (int i = 0; i < 256; i++)
        palette.Entries[i] = Color.FromArgb(i, i, i);
    bmp.Palette = palette;

    return bmp;
}

關鍵點:Stride 為 4 位元組倍數,需計算 offset 跳過補齊區;調色盤預設為偽彩,必須重建為灰度映射。

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