private void CreateBitmap(int width, int height)
{
Debug.Assert(_hSection == IntPtr.Zero);
Debug.Assert(_hBitmap == IntPtr.Zero);
Debug.Assert(_interopBitmap == null);
if (width == 0 || height == 0) return;
_stride = (width * _format.BitsPerPixel + 7) / 8;
int size = height * _stride;
_hSection = NativeMethods.CreateFileMapping(
PAGE.READWRITE,
SEC.NONE,
0,
(uint)size,
null);
BITMAPINFO bmi = new BITMAPINFO();
bmi.biSize = Marshal.SizeOf(bmi);
bmi.biWidth = width;
bmi.biHeight = -height; // top-down
bmi.biPlanes = 1;
bmi.biBitCount = 32;
bmi.biCompression = BI.RGB;
IntPtr hdcScreen = NativeMethods.GetDC(HWND.NULL);
_hBitmap = NativeMethods.CreateDIBSection(
hdcScreen,
ref bmi,
DIB.RGB_COLORS,
out _pBits,
_hSection,
0);
// TODO: probably don't need a new DC every time...
_hDC = NativeMethods.CreateCompatibleDC(hdcScreen);
NativeMethods.SelectObject(_hDC, _hBitmap);
_interopBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromMemorySection(
_hSection,
width,
height,
_format,
_stride,
0) as InteropBitmap;
_bitmapWidth = width;
_bitmapHeight = height;
}