Skip to content

Instantly share code, notes, and snippets.

@ImagingSolution
Created November 7, 2019 14:53
Show Gist options
  • Save ImagingSolution/c4965d41b181b5bfa3eb091faa2007c7 to your computer and use it in GitHub Desktop.
Save ImagingSolution/c4965d41b181b5bfa3eb091faa2007c7 to your computer and use it in GitHub Desktop.
【C#】画像の輝度値の取得設定速度の比較
/// <summary>
/// LockBits、UnlockBitsで画像データのポインタを取得し、配列を介して処理を行う
/// </summary>
/// <param name="bmp"></param>
private void NegativeImage2(Bitmap bmp)
{
var width = bmp.Width;
var height = bmp.Height;
// Bitmapをロック
var bmpData = bmp.LockBits(
new Rectangle(0, 0, width, height),
System.Drawing.Imaging.ImageLockMode.ReadWrite,
bmp.PixelFormat
);
// メモリの幅のバイト数を取得
var stride = Math.Abs(bmpData.Stride);
// 画像データ格納用配列
var data = new byte[stride * bmpData.Height];
// Bitmapデータを配列へコピー
System.Runtime.InteropServices.Marshal.Copy(
bmpData.Scan0,
data,
0,
stride * bmpData.Height
);
byte r, g, b;
int lineIndex = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width * 3; x += 3)
{
// 輝度値の取得
r = data[lineIndex + x + 2];
g = data[lineIndex + x + 1];
b = data[lineIndex + x]; ;
// 輝度値の設定
data[lineIndex + x + 2] = (byte)(255 - r);
data[lineIndex + x + 1] = (byte)(255 - g);
data[lineIndex + x] = (byte)(255 - b);
}
lineIndex += stride;
}
// 配列をBitmapデータへコピー
System.Runtime.InteropServices.Marshal.Copy(
data,
0,
bmpData.Scan0,
stride * bmpData.Height
);
// アンロック
bmp.UnlockBits(bmpData);
}
@ImagingSolution
Copy link
Author

【C#】画像の輝度値の取得設定速度の比較
https://imagingsolution.net/program/set_get_bright_speed/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment