public Boolean AllocateBitmap(int BmpWidth, int BmpHeight, ushort BitCount)//在内存中创建位图 { if ((BitCount != 1) & (BitCount != 4) & (BitCount != 8) & (BitCount != 16) & (BitCount != 24) & (BitCount != 32)) { return(false); } Dispose(false); Win32Structure.BITMAPINFO BmpInfo = new Win32Structure.BITMAPINFO(); BmpInfo.Header.Size = (uint)sizeof(Win32Structure.BITMAPINFOHEADER); BmpInfo.Header.Width = BmpWidth; BmpInfo.Header.Height = -BmpHeight; BmpInfo.Header.BitCount = BitCount; BmpInfo.Header.Planes = 1; BmpInfo.Header.Compression = Win32Const.BI_RGB; BmpInfo.Header.XPelsPerMeter = 96; BmpInfo.Header.YPelsPerMeter = 96; BmpInfo.Header.ClrUsed = 0; BmpInfo.Header.SizeImage = 0; BmpInfo.Header.ClrImportant = 0; BmpInfo.Header.SizeImage = 0; IntPtr ScreecDC = Win32Api.GetDC(IntPtr.Zero); m_Hdc = Win32Api.CreateCompatibleDC(ScreecDC); int Result = Win32Api.ReleaseDC(IntPtr.Zero, ScreecDC); m_Handle = Win32Api.CreateDIBSection(m_Hdc, ref BmpInfo, Win32Const.DIB_RGB_COLORS, out m_Pointer, IntPtr.Zero, 0); if (m_Handle == IntPtr.Zero) { m_Hdc = IntPtr.Zero; m_Width = 0; m_Height = 0; m_Stride = 0; m_BitCount = BitCount; return(false); } else { Win32Api.SelectObject(m_Hdc, m_Handle); m_Width = BmpWidth; m_Height = BmpHeight; m_BitCount = BitCount; switch (BitCount) { case 1: m_Stride = (int)(((m_Width + 7) / 8 + 3) & 0XFFFFFFFC); break; case 4: m_Stride = (int)(((m_Width + 1) / 2 + 3) & 0XFFFFFFFC); break; case 8: m_Stride = (int)((m_Width + 3) & 0XFFFFFFFC); break; case 16: m_Stride = (int)((m_Width * 2 + 3) & 0XFFFFFFFC); break; case 24: m_Stride = (int)((m_Width * 3 + 3) & 0XFFFFFFFC); break; case 32: m_Stride = m_Width * 4; break; } if (m_BitCount <= 8) { m_PaletteAddress = Marshal.AllocHGlobal(1024); m_PaletteSize = 1 << (m_BitCount); } return(true); } }
internal static extern IntPtr CreateDIBSection(IntPtr Hdc, ref Win32Structure.BITMAPINFO BmpInfo, uint iUsage, out IntPtr ppvBits, IntPtr hSection, uint dwOffset);