Skip to content

Instantly share code, notes, and snippets.

@tesfabpel
Last active October 3, 2015 08:22
Show Gist options
  • Select an option

  • Save tesfabpel/b8fa0aceb1cfbefc198c to your computer and use it in GitHub Desktop.

Select an option

Save tesfabpel/b8fa0aceb1cfbefc198c to your computer and use it in GitHub Desktop.
Clear Bitmap Data
void ClearBitmapData(BitmapData bd, byte[] color)
{
var pf = bd.PixelFormat;
var size = sizeof(byte);
if(pf == PixelFormat.Format16bppRgb555)
size *= 2;
else if(pf == PixelFormat.Format24bppRgb)
size *= 3;
else if (pf == PixelFormat.Format32bppArgb)
size *= 4;
var bytesRow = new byte[bd.Width * size];
for(int x = 0; x < bytesRow.Length; x++)
{
int j = x % color.Length;
bytesRow[x] = color[j];
}
for(int y = 0; y < bd.Height; y++)
{
IntPtr rowPtr = bd.Scan0 + (bd.Stride * y);
Marshal.Copy(bytesRow, 0, rowPtr, bytesRow.Length);
}
}
private void DoWork()
{
pictureBox1.Image = null;
var start = DateTime.UtcNow;
Bitmap b = new Bitmap(4000, 1500, PixelFormat.Format32bppArgb);
var bd = b.LockBits(new Rectangle(Point.Empty, b.Size), ImageLockMode.ReadWrite, b.PixelFormat);
//ClearBitmapData(bd, new[] { (byte)0xFA, (byte)0xFA, (byte)0xB0, (byte)0x00 });
ClearBitmapData(bd, new[] { (byte)0x00, (byte)0xB0, (byte)0xFA, (byte)0xFF });
b.UnlockBits(bd);
var end = DateTime.UtcNow;
pictureBox1.Image = b;
MessageBox.Show((end - start).ToString());
b.Save("C:\\gnao.bmp");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment