Skip to content

Instantly share code, notes, and snippets.

@ImagingSolution
Created November 7, 2019 14:54
Show Gist options
  • Save ImagingSolution/2281a15deb2f56a347cff6aa1678089d to your computer and use it in GitHub Desktop.
Save ImagingSolution/2281a15deb2f56a347cff6aa1678089d to your computer and use it in GitHub Desktop.
【C#】画像の輝度値の取得設定速度の比較
/// <summary>
/// MarshalクラスのReadByte、WriteByteで輝度値の取得設定
/// </summary>
/// <param name="bmp"></param>
private void NegativeImage3(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);
byte r, g, b;
int lineIndex = 0;
var ptr = bmpData.Scan0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width * 3; x += 3)
{
// 輝度値の取得
r = System.Runtime.InteropServices.Marshal.ReadByte(ptr, lineIndex + x + 2);
g = System.Runtime.InteropServices.Marshal.ReadByte(ptr, lineIndex + x + 1);
b = System.Runtime.InteropServices.Marshal.ReadByte(ptr, lineIndex + x);
// 輝度値の設定
System.Runtime.InteropServices.Marshal.WriteByte(ptr, lineIndex + x + 2, (byte)(255 - r));
System.Runtime.InteropServices.Marshal.WriteByte(ptr, lineIndex + x + 1, (byte)(255 - g));
System.Runtime.InteropServices.Marshal.WriteByte(ptr, lineIndex + x, (byte)(255 - b));
}
lineIndex += stride;
}
// アンロック
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