/// <summary> /// Creates a new <see cref="NativeBuffer"/> with the given dimensions. /// </summary> /// <param name="buffer">The storage location for the buffer object.</param> /// <param name="w">The width.</param> /// <param name="h">The height.</param> /// <param name="isDIB">True if the buffer should be DIB backed, false for Marshal.</param> /// <returns>Returns the data buffer.</returns> public static ARGB* CreateBuffer(out NativeBuffer buffer, int w, int h, bool isDIB) { ARGB* pixels; IntPtr handle; DeviceContext context = null; if(isDIB) { context = new DeviceContext(); //create info BITMAPINFO info = new BITMAPINFO(); //init with size info.init(w, h); //create DIB handle = CreateDIBSection(context.Handle, ref info, DIB_RGB_COLORS, out pixels, IntPtr.Zero, 0); WinAPIUtils.Assert(handle != IntPtr.Zero); //select the DIB into the DC context.Push(handle); }else { handle = Marshal.AllocHGlobal(w * h * 4); pixels = (ARGB*)handle; } //create buffer wrapper buffer = new NativeBuffer{isDIB = isDIB, Handle = handle, Context = context}; //return the data return pixels; }
public static IntPtr Create24bppDIBSection(int nWidth, int nHeight) { BITMAPINFO bmi = new BITMAPINFO(nWidth, nHeight, 24); IntPtr pBits; return CreateDIBSection(IntPtr.Zero, bmi, DIB_RGB_COLORS, out pBits, IntPtr.Zero, 0); }
/// <summary> /// Resizes the section. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="bitCount">The bit count.</param> public void Resize(int width, int height, int bitCount) { // Destroy existing objects. Destroy(); // Set parameters. Width = width; Height = height; // Create a bitmap info structure. BITMAPINFO info = new BITMAPINFO(); info.Init(); // Set the data. info.biBitCount = (short)bitCount; info.biPlanes = 1; info.biWidth = width; info.biHeight = height; // Create the bitmap. HBitmap = Win32.CreateDIBSection(parentDC, ref info, Win32.DIB_RGB_COLORS, out bits, IntPtr.Zero, 0); Win32.SelectObject(parentDC, HBitmap); }
/// <summary> /// Creates the specified width. /// </summary> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="bitCount">The bit count.</param> /// <returns></returns> public virtual bool Create(IntPtr hDC, int width, int height, int bitCount) { this.Width = width; this.Height = height; parentDC = hDC; // Destroy existing objects. Destroy(); // Create a bitmap info structure. BITMAPINFO info = new BITMAPINFO(); info.Init(); // Set the data. info.biBitCount = (short)bitCount; info.biPlanes = 1; info.biWidth = width; info.biHeight = height; // Create the bitmap. HBitmap = Win32.CreateDIBSection(hDC, ref info, Win32.DIB_RGB_COLORS, out bits, IntPtr.Zero, 0); Win32.SelectObject(hDC, HBitmap); // Set the OpenGL pixel format. SetPixelFormat(hDC, bitCount); return true; }
public static void DrawText(Graphics graphics, string text, Font font, Rectangle bounds, Color color, TextFormatFlags flags, TextStyle textStyle) { if (!VisualStyleRenderer.IsSupported) { TextRenderer.DrawText(graphics, text, font, bounds, color, flags); return; } IntPtr primaryHdc = graphics.GetHdc(); // Create a memory DC so we can work offscreen IntPtr memoryHdc = CreateCompatibleDC(primaryHdc); // Create a device-independent bitmap and select it into our DC BITMAPINFO info = new BITMAPINFO(); info.biSize = Marshal.SizeOf(info); info.biWidth = bounds.Width; info.biHeight = -bounds.Height; info.biPlanes = 1; info.biBitCount = 32; info.biCompression = 0; // BI_RGB IntPtr dib = CreateDIBSection(primaryHdc, info, 0, 0, IntPtr.Zero, 0); SelectObject(memoryHdc, dib); // Create and select font IntPtr fontHandle = font.ToHfont(); SelectObject(memoryHdc, fontHandle); // Draw glowing text VisualStyleRenderer renderer = new VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active); DTTOPTS dttOpts = new DTTOPTS(); dttOpts.dwSize = Marshal.SizeOf(typeof(DTTOPTS)); if (textStyle == TextStyle.Glowing) { dttOpts.dwFlags = DTT_COMPOSITED | DTT_GLOWSIZE | DTT_TEXTCOLOR; } else { dttOpts.dwFlags = DTT_COMPOSITED | DTT_TEXTCOLOR; } dttOpts.crText = ColorTranslator.ToWin32(color); dttOpts.iGlowSize = 8; // This is about the size Microsoft Word 2007 uses RECT textBounds = new RECT(0, 0, bounds.Right - bounds.Left, bounds.Bottom - bounds.Top); DrawThemeTextEx(renderer.Handle, memoryHdc, 0, 0, text, -1, (int)flags, ref textBounds, ref dttOpts); // Copy to foreground const int SRCCOPY = 0x00CC0020; BitBlt(primaryHdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, memoryHdc, 0, 0, SRCCOPY); // Clean up DeleteObject(fontHandle); DeleteObject(dib); DeleteDC(memoryHdc); graphics.ReleaseHdc(primaryHdc); }
/// <summary> /// 初始化视频编解码器 /// </summary> /// <param name="bitmapInfoHeader">图像头信息</param> /// <param name="isEncode">标识完成编码还是解码功能</param> public VideoEncoder(BITMAPINFOHEADER bitmapInfoHeader, bool isEncode) { #region //BITMAPINFOHEADER bmi = new BITMAPINFOHEADER (); //bmi.biWidth = bitmapInfoHeader.biWidth; //bmi.biHeight = bitmapInfoHeader.biHeight; //if (isEncode) //{ // bmi.biCompression =bitmapInfoHeader.biCompression; //} //else //{ // bmi.biCompression = FOURCC.MP42; //} //bmi.biSizeImage =bitmapInfoHeader.biSizeImage; //bmi.biPlanes = bitmapInfoHeader.biPlanes; //bmi.biBitCount = bitmapInfoHeader.biBitCount; //bmi.biXPelsPerMeter = bitmapInfoHeader.biXPelsPerMeter; //bmi.biYPelsPerMeter = bitmapInfoHeader.biYPelsPerMeter; //bmi.biClrUsed = bitmapInfoHeader.biClrUsed; //bmi.biClrImportant = bitmapInfoHeader.biClrImportant; //bmi.biSize = bitmapInfoHeader.biSize; //bitmapInfo.bmiHeader = bmi; #endregion BITMAPINFO bitmapInfo = new BITMAPINFO(); bitmapInfo.bmiHeader = bitmapInfoHeader; this.IsEncode = isEncode; if (isEncode) { COMPVARS compvars = new COMPVARS(); compvars.cbSize = Marshal.SizeOf(compvars); compvars.dwFlags = 1; compvars.fccHandler = FOURCC.MP42; compvars.fccType = FOURCC.ICTYPE_VIDEO; compvars.lDataRate = 780; compvars.lKey = 15; compvars.lQ = -1; compvars.lQ = 500; this.Compressor = new ICCompressor(compvars, bitmapInfo, FOURCC.MP42); this.Compressor.Open();//打开编码器 } else { bitmapInfo.bmiHeader.biCompression = FOURCC.MP42; this.Decompressor = new ICDecompressor(new COMPVARS(), bitmapInfo, FOURCC.MP42); this.Decompressor.Open(); } }
public Display() { InitializeComponent(); bmi = new BITMAPINFO { biHeader = { bihBitCount = 32, bihPlanes = 1, bihSize = 40, bihWidth = 320, bihHeight = 240, bihSizeImage = 320 * 240 * 4 } }; g = pbDisplay.CreateGraphics(); display = pbDisplay; stopWatch.Start(); }
public PixelBuffer(int width, int height) { // Create a MemoryDC to hold the bitmap. Use IntPtr.Zero // to create a device context that is compatible with the screen. fMemoryDC = GDI32.CreateCompatibleDC(IntPtr.Zero); // Create a bitmap compatible with the screen fBitmapInfo = new BITMAPINFO(); fBitmapInfo.Init(); fBitmapInfo.bmiHeader.biWidth = width; fBitmapInfo.bmiHeader.biHeight = -height; fBitmapInfo.bmiHeader.biPlanes = 1; fBitmapInfo.bmiHeader.biBitCount = 32; fBitmapInfo.bmiHeader.biClrImportant = 0; fBitmapInfo.bmiHeader.biClrUsed = 0; fBitmapInfo.bmiHeader.biCompression = GDI32.BI_RGB; fBitmapInfo.bmiColors = IntPtr.Zero; fBitmapHandle = GDI32.CreateDIBSection(User32.GetDC(IntPtr.Zero), ref fBitmapInfo, GDI32.DIB_RGB_COLORS, ref fBits, IntPtr.Zero, 0); fPixelData = new PixelData(width, height, 32, width * 4, fBits); // Get the bitmap structure back out so we can // get our hands on the created pointer and whatnot //GDI32.GetBitmap(fBitmapHandle, ref fBitmapStructure); //fBits = fBitmapStructure.bmBits; // Select the bitmap into the memoryDC fOldBitmapHandle = GDI32.SelectObject(fMemoryDC, fBitmapHandle); fAlpha = 255; }
/// <summary> /// Add or replace an entry in the given image list /// </summary> /// <param name="bm">The image</param> /// <param name="il">The image list to modify</param> /// <param name="replace">If true replace the existing index with the one given</param> /// <param name="replaceIndex">The replacement index</param> private static void AddHelper( Bitmap bm, ImageList il, bool replace, int replaceIndex ) { IntPtr hBitmap, ppvBits; BITMAPINFO bmi = new BITMAPINFO(); // Resize the image to dimensions of imagelist before adding if( bm.Size != il.ImageSize ) { bm = new Bitmap( bm, il.ImageSize.Width, il.ImageSize.Height ); } // Required due to the way bitmap is copied and read bmi.biSize = 40; // Needed for RtlMoveMemory() bmi.biBitCount = 32; // Number of bits bmi.biPlanes = 1; // Number of planes bmi.biWidth = bm.Width; // Width of our new bitmap bmi.biHeight = bm.Height; // Height of our new bitmap bm.RotateFlip( RotateFlipType.RotateNoneFlipY ); // Create our new bitmap hBitmap = CreateDIBSection( new IntPtr( 0 ), bmi, 0, out ppvBits, new IntPtr( 0 ), 0 ); // Copy the bitmap BitmapData bitmapData = bm.LockBits( new Rectangle( 0, 0, bm.Width, bm.Height ), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb ); RtlMoveMemory( ppvBits, bitmapData.Scan0, bm.Height * bitmapData.Stride ); bm.UnlockBits( bitmapData ); // Adds the new bitmap to the imagelist control or replaces the existing bitmap if( replace ) ImageList_Replace( il.Handle, replaceIndex, hBitmap, new IntPtr( 0 ) ); else ImageList_Add( il.Handle, hBitmap, new IntPtr( 0 ) ); }
private static extern int GetObject(SafeGdiObjectHandle hgdiobj, int cbBuffer, ref BITMAPINFO lpvObject);
public static extern bool SendMessage(IntPtr hWnd, int wMsg, int wParam, ref BITMAPINFO lParam);
/// <summary> /// Draws theme text on glass. /// </summary> /// <param name="hwnd"></param> /// <param name="text"></param> /// <param name="font"></param> /// <param name="bounds"></param> /// <param name="glowSize"></param> /// <remarks>This method is courtesy of 版权所有 (I hope the name's right)</remarks> public static void DrawTextOnGlass(Graphics graphics, String text, Font font, Rectangle bounds, int glowSize) { if (IsGlassEnabled) { IntPtr destdc = IntPtr.Zero; try { destdc = graphics.GetHdc(); IntPtr Memdc = CreateCompatibleDC(destdc); // Set up a memory DC where we'll draw the text. IntPtr bitmap; IntPtr bitmapOld = IntPtr.Zero; IntPtr logfnotOld; int uFormat = DT_SINGLELINE | DT_CENTER | DT_VCENTER | DT_NOPREFIX; //text format BITMAPINFO dib = new BITMAPINFO(); dib.bmiHeader.biHeight = -bounds.Height; // negative because DrawThemeTextEx() uses a top-down DIB dib.bmiHeader.biWidth = bounds.Width; dib.bmiHeader.biPlanes = 1; dib.bmiHeader.biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER)); dib.bmiHeader.biBitCount = 32; dib.bmiHeader.biCompression = BI_RGB; if (!(SaveDC(Memdc) == 0)) { bitmap = CreateDIBSection(Memdc, ref dib, DIB_RGB_COLORS, (IntPtr)0, IntPtr.Zero, 0); // Create a 32-bit bmp for use in offscreen drawing when glass is on if (!(bitmap == IntPtr.Zero)) { bitmapOld = SelectObject(Memdc, bitmap); IntPtr hFont = font.ToHfont(); logfnotOld = SelectObject(Memdc, hFont); try { VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.Window.Caption.Active); DTTOPTS dttOpts = new DTTOPTS { dwSize = (uint)Marshal.SizeOf(typeof(DTTOPTS)), dwFlags = DTT_COMPOSITED | DTT_GLOWSIZE, iGlowSize = glowSize }; RECT rc2 = new RECT(0, 0, bounds.Width, bounds.Height); int dtter = DrawThemeTextEx(renderer.Handle, Memdc, 0, 0, text, -1, uFormat, ref rc2, ref dttOpts); bool bbr = BitBlt(destdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, Memdc, 0, 0, SRCCOPY); if (!bbr) { //throw new Exception("???"); } } catch (Exception) { //Console.WriteLine(e.ToString()); //throw new Exception("???"); } //Remember to clean up SelectObject(Memdc, bitmapOld); SelectObject(Memdc, logfnotOld); DeleteObject(bitmap); DeleteObject(hFont); ReleaseDC(Memdc, (IntPtr)(-1)); DeleteDC(Memdc); } } } finally { if (destdc != IntPtr.Zero) { graphics.ReleaseHdc(destdc); } } } }
private static extern int SetDIBitsToDevice(IntPtr hdc, int xDest, int yDest, UInt32 dwWidth, UInt32 dwHeight, int xSrc, int ySrc, UInt32 uStartScan, UInt32 cScanLines, IntPtr lpvBits, ref BITMAPINFO lpbmi, UInt32 fuColorUse);
private static extern IntPtr CreateDIBSection(IntPtr hdc, ref BITMAPINFO pbmi, uint iUsage, int ppvBits, IntPtr hSection, uint dwOffset);
static extern int SendBitmapMessage(int hWnd, uint wMsg, int wParam, ref BITMAPINFO lParam);
public static void DrawTextOnGlass( IntPtr hwnd, string text, Font font, Rectangle ctlrct, int iglowSize = 10 ) { if ( !CanUseAero ) { return; } RECT rc = new RECT() { Left = ctlrct.Left, Right = ctlrct.Right + 2 * iglowSize, Top = ctlrct.Top, Bottom = ctlrct.Bottom + 2 * iglowSize, }; RECT rc2 = new RECT() { Left = 0, Top = 0, Right = rc.Right - rc.Left, Bottom = rc.Bottom - rc.Top }; IntPtr destdc = GetDC( hwnd ); IntPtr Memdc = CreateCompatibleDC( destdc ); IntPtr bitmap; IntPtr bitmapOld = IntPtr.Zero; IntPtr logfnotOld; int uFormat = DT_SINGLELINE | DT_CENTER | DT_VCENTER | DT_NOPREFIX; BITMAPINFO dib = new BITMAPINFO() { bmiHeader = new BITMAPINFOHEADER() { biHeight = -( rc.Bottom - rc.Top ), biWidth = rc.Right - rc.Left, biPlanes = 1, biSize = Marshal.SizeOf( typeof( BITMAPINFOHEADER ) ), biBitCount = 32, biCompression = BI_RGB } }; if ( SaveDC( Memdc ) != 0 ) { bitmap = CreateDIBSection( Memdc, ref dib, DIB_RGB_COLORS, 0, IntPtr.Zero, 0 ); // Create a 32-bit bmp for use in offscreen drawing when glass is on if ( bitmap != IntPtr.Zero ) { bitmapOld = SelectObject( Memdc, bitmap ); IntPtr hFont = font.ToHfont(); logfnotOld = SelectObject( Memdc, hFont ); try { VisualStyleRenderer renderer = new VisualStyleRenderer( VisualStyleElement.Window.Caption.Active ); DTTOPTS dttOpts = new DTTOPTS() { dwSize = ( uint ) Marshal.SizeOf( typeof( DTTOPTS ) ), dwFlags = DTT_COMPOSITED | DTT_GLOWSIZE, iGlowSize = iglowSize }; DrawThemeTextEx( renderer.Handle, Memdc, 0, 0, text, -1, uFormat, ref rc2, ref dttOpts ); BitBlt( destdc, rc.Left, rc.Top, rc.Right - rc.Left, rc.Bottom - rc.Top, Memdc, 0, 0, SOURCE_COPY ); } catch ( Exception e ) { Logger.LogError( e ); } finally { try { SelectObject( Memdc, bitmapOld ); SelectObject( Memdc, logfnotOld ); DeleteObject( bitmap ); DeleteObject( hFont ); ReleaseDC( Memdc, -1 ); DeleteDC( Memdc ); } catch { } } } } }
public static extern int GetDIBits(IntPtr hdc, IntPtr hbmp, uint uStartScan, uint cScanLines, [Out] byte [] lpvBits, ref BITMAPINFO lpbi, uint uUsage);
private static extern int SetDIBitsToDevice(HandleRef hDC, int xDest, int yDest, int dwWidth, int dwHeight, int XSrc, int YSrc, int uStartScan, int cScanLines, ref int lpvBits, ref BITMAPINFO lpbmi, uint fuColorUse);
public static extern int GetDIBits(SafeDCHandle hdc, IntPtr hbmp, int uStartScan, int cScanLines, IntPtr lpvBits, ref BITMAPINFO lpbi, DIBColorMode uUsage);
public static extern IntPtr CreateDIBSection(SafeDCHandle hdc, ref BITMAPINFO pbmi, DIBColorMode iUsage, out IntPtr ppvBits, IntPtr hSection, int dwOffset);
/// <summary> /// 设置对方视频格式事件 /// </summary> /// <param name="BITMAPINFO"></param> public void SetRemoteBITMAPINFOHEADER(BITMAPINFO BITMAPINFO) { if (VD == null) VD = new VideoEncoder(BITMAPINFO, false);//创建视频解码器 if (VR == null) { VR = new VideoRender(this.cRemote);//创建视频回显组件 VR.IniVideoRender(BITMAPINFO.bmiHeader); } }
private static Image ConvertPixelFormat(Image image, PixelFormat newPixelFormat, Color[] palette) { int bpp = Image.GetPixelFormatSize(newPixelFormat); if (newPixelFormat == PixelFormat.Format16bppArgb1555 || newPixelFormat == PixelFormat.Format16bppGrayScale) { throw new NotSupportedException("This pixel format is not supported by GDI+"); } Bitmap result; // non-indexed target image (transparency preserved automatically) if (bpp > 8) { result = new Bitmap(image.Width, image.Height, newPixelFormat); using (Graphics g = Graphics.FromImage(result)) { g.DrawImage(image, 0, 0, image.Width, image.Height); } return(result); } int transparentIndex; Bitmap bmp; // indexed colors: using GDI+ natively RGBQUAD[] targetPalette = new RGBQUAD[256]; int colorCount = InitPalette(targetPalette, bpp, (image is Bitmap) ? image.Palette : null, palette, out transparentIndex); BITMAPINFO bmi = new BITMAPINFO(); bmi.icHeader.biSize = (uint)Marshal.SizeOf(typeof(BITMAPINFOHEADER)); bmi.icHeader.biWidth = image.Width; bmi.icHeader.biHeight = image.Height; bmi.icHeader.biPlanes = 1; bmi.icHeader.biBitCount = (ushort)bpp; bmi.icHeader.biCompression = BI_RGB; bmi.icHeader.biSizeImage = (uint)(((image.Width + 7) & 0xFFFFFFF8) * image.Height / (8 / bpp)); bmi.icHeader.biXPelsPerMeter = 0; bmi.icHeader.biYPelsPerMeter = 0; bmi.icHeader.biClrUsed = (uint)colorCount; bmi.icHeader.biClrImportant = (uint)colorCount; bmi.icColors = targetPalette; bmp = (image as Bitmap) ?? new Bitmap(image); // Creating the indexed bitmap IntPtr bits; IntPtr hbmResult = CreateDIBSection(IntPtr.Zero, ref bmi, DIB_RGB_COLORS, out bits, IntPtr.Zero, 0); // Obtaining screen DC IntPtr dcScreen = GetDC(IntPtr.Zero); // DC for the original hbitmap IntPtr hbmSource = bmp.GetHbitmap(); IntPtr dcSource = CreateCompatibleDC(dcScreen); SelectObject(dcSource, hbmSource); // DC for the indexed hbitmap IntPtr dcTarget = CreateCompatibleDC(dcScreen); SelectObject(dcTarget, hbmResult); // Copy content BitBlt(dcTarget, 0, 0, image.Width, image.Height, dcSource, 0, 0, 0x00CC0020 /*TernaryRasterOperations.SRCCOPY*/); // obtaining result result = Image.FromHbitmap(hbmResult); result.SetResolution(image.HorizontalResolution, image.VerticalResolution); // cleanup DeleteDC(dcSource); DeleteDC(dcTarget); ReleaseDC(IntPtr.Zero, dcScreen); DeleteObject(hbmSource); DeleteObject(hbmResult); ColorPalette resultPalette = result.Palette; bool resetPalette = false; // restoring transparency if (transparentIndex >= 0) { // updating palette if transparent color is not actually transparent if (resultPalette.Entries[transparentIndex].A != 0) { resultPalette.Entries[transparentIndex] = Color.Transparent; resetPalette = true; } ToIndexedTransparentByArgb(result, bmp, transparentIndex); } if (resetPalette) { result.Palette = resultPalette; } if (!ReferenceEquals(bmp, image)) { bmp.Dispose(); } return(result); }
public static void DrawTextOnGlass(Graphics graphics, string text, Font font, Rectangle bounds, Color color, TextFormatFlags flags, bool withGlow) { IntPtr primaryHdc = graphics.GetHdc(); // Memory DC for off screen rendering. IntPtr memoryHdc = CreateCompatibleDC(primaryHdc); BITMAPINFO info = new BITMAPINFO(); info.biSize = Marshal.SizeOf(info); info.biWidth = bounds.Width; info.biHeight = -bounds.Height; info.biPlanes = 1; info.biBitCount = 32; info.biCompression = 0; // BI_RGB IntPtr dib = CreateDIBSection(primaryHdc, info, 0, 0, IntPtr.Zero, 0); SelectObject(memoryHdc, dib); IntPtr fontHandle = font.ToHfont(); SelectObject(memoryHdc, fontHandle); // Draw glowing text if enabled. VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.Window.Caption.Active); DTTOPTS dttOpts = new DTTOPTS(); dttOpts.dwSize = (uint)Marshal.SizeOf(typeof(DTTOPTS)); if (withGlow) { dttOpts.dwFlags = DTT_COMPOSITED | DTT_GLOWSIZE | DTT_TEXTCOLOR; dttOpts.iGlowSize = 12; } else { dttOpts.dwFlags = DTT_COMPOSITED | DTT_TEXTCOLOR; dttOpts.iGlowSize = 0; } dttOpts.crText = (uint)ColorTranslator.ToWin32(color); RECT textBounds = new RECT(dttOpts.iGlowSize, 0, bounds.Width - dttOpts.iGlowSize, bounds.Height); DrawThemeTextEx(renderer.Handle, memoryHdc, 0, 0, text, -1, (int)flags, ref textBounds, ref dttOpts); //const int SRCCOPY = 0x00CC0020; //BitBlt(primaryHdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, memoryHdc, 0, 0, SRCCOPY); BLENDFUNCTION blend = new Win32.BLENDFUNCTION(AC_SRC_OVER, 0, 255, AC_SRC_ALPHA); AlphaBlend(primaryHdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, memoryHdc, 0, 0, bounds.Width, bounds.Height, blend); // Clean up DeleteObject(fontHandle); DeleteObject(dib); DeleteDC(memoryHdc); graphics.ReleaseHdc(primaryHdc); }
static extern IntPtr CreateDIBSection(IntPtr hdc, ref BITMAPINFO bmi, uint Usage, out IntPtr bits, IntPtr hSection, uint dwOffset);
public static extern SafeGDIHandle CreateDIBSection(IntPtr hdc, BITMAPINFO pbmi, uint iUsage, IntPtr ppvBits, IntPtr hSection, uint dwOffset);
public static void FillBlackRegion( Graphics gph, Rectangle rgn ) { RECT rc = new RECT() { Left = rgn.Left, Right = rgn.Right, Top = rgn.Top, Bottom = rgn.Bottom, }; IntPtr destdc = gph.GetHdc(); IntPtr Memdc = CreateCompatibleDC( destdc ); IntPtr bitmap; IntPtr bitmapOld = IntPtr.Zero; BITMAPINFO dib = new BITMAPINFO() { bmiHeader = new BITMAPINFOHEADER() { biHeight = -( rc.Bottom - rc.Top ), biWidth = rc.Right - rc.Left, biPlanes = 1, biSize = Marshal.SizeOf( typeof( BITMAPINFOHEADER ) ), biBitCount = 32, biCompression = BI_RGB } }; if ( SaveDC( Memdc ) != 0 ) { bitmap = CreateDIBSection( Memdc, ref dib, DIB_RGB_COLORS, 0, IntPtr.Zero, 0 ); try { if ( bitmap != IntPtr.Zero ) { bitmapOld = SelectObject( Memdc, bitmap ); BitBlt( destdc, rc.Left, rc.Top, rc.Right - rc.Left, rc.Bottom - rc.Top, Memdc, 0, 0, SOURCE_COPY ); } } finally { SelectObject( Memdc, bitmapOld ); DeleteObject( bitmap ); ReleaseDC( Memdc, -1 ); DeleteDC( Memdc ); } } gph.ReleaseHdc(); }
private static extern IntPtr CreateDIBSection(IntPtr hdc,ref BITMAPINFO pbmi, uint iUsage, int ppvBits, IntPtr hSection, uint dwOffset);
public static extern IntPtr CreateDIBSection([In] SafeHandle hdc, [In] ref BITMAPINFO lpbmi, uint usage, ref IntPtr ppvBits, IntPtr hSection, int offset);
public static extern int SetDIBitsToDevice(IntPtr hdc, Int32 XDest, Int32 YDest, UInt32 dwWidth, UInt32 dwHeight, Int32 XSrc, Int32 YSrc, UInt32 uStartScan, UInt32 cScanLines, IntPtr lpvBits, ref BITMAPINFO lpbmi, UInt32 fuColorUse);
public static extern int SendMessage(int hWnd, uint wMsg, int wParam, ref BITMAPINFO bitmapInfo);
private static void DrawTextOnGlass(IntPtr hDC, string text, Font font, Rectangle rct, int iGlowSize, StringAlignment horizontalAlign) { RECT rect = new RECT(); RECT pRect = new RECT(); rect.left = rct.Left; rect.right = rct.Right + (4 * iGlowSize); rect.top = rct.Top; rect.bottom = rct.Bottom + (2 * iGlowSize); pRect.left = 2 * iGlowSize; pRect.top = 2; pRect.right = rect.Width - (3 * iGlowSize); pRect.bottom = rect.Height - 2; TextFormatFlags dwFlags = TextFormatFlags.ModifyString | TextFormatFlags.NoPrefix | TextFormatFlags.SingleLine; switch (horizontalAlign) { case StringAlignment.Near: dwFlags |= TextFormatFlags.EndEllipsis; break; case StringAlignment.Center: dwFlags |= TextFormatFlags.PathEllipsis | TextFormatFlags.HorizontalCenter; break; case StringAlignment.Far: dwFlags |= TextFormatFlags.Right; break; } IntPtr ptr = PInvoke.CreateCompatibleDC(hDC); if (ptr != IntPtr.Zero) { IntPtr ptr5; BITMAPINFO pbmi = new BITMAPINFO(); pbmi.bmiHeader.biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER)); pbmi.bmiHeader.biWidth = rect.Width; pbmi.bmiHeader.biHeight = -rect.Height; pbmi.bmiHeader.biPlanes = 1; pbmi.bmiHeader.biBitCount = 0x20; pbmi.bmiHeader.biCompression = 0; IntPtr hgdiobj = PInvoke.CreateDIBSection(ptr, ref pbmi, 0, out ptr5, IntPtr.Zero, 0); if (hgdiobj != IntPtr.Zero) { IntPtr ptr3 = PInvoke.SelectObject(ptr, hgdiobj); IntPtr ptr6 = font.ToHfont(); IntPtr ptr4 = PInvoke.SelectObject(ptr, ptr6); VisualStyleRenderer renderer = new VisualStyleRenderer(VisualStyleElement.Window.Caption.Active); DTTOPTS pOptions = new DTTOPTS(); pOptions.dwSize = Marshal.SizeOf(typeof(DTTOPTS)); pOptions.dwFlags = 0x2800; pOptions.iGlowSize = iGlowSize; PInvoke.DrawThemeTextEx(renderer.Handle, ptr, 0, 0, text, -1, dwFlags, ref pRect, ref pOptions); PInvoke.BitBlt(hDC, rect.left, rect.top, rect.Width, rect.Height, ptr, 0, 0, 0xcc0020); PInvoke.SelectObject(ptr, ptr3); PInvoke.SelectObject(ptr, ptr4); PInvoke.DeleteObject(hgdiobj); PInvoke.DeleteObject(ptr6); } PInvoke.DeleteDC(ptr); } }
public static extern int StretchDIBits(IntPtr hdc, int XDest, int YDest, int nDestWidth, int nDestHeight, int XSrc, int YSrc, int nSrcWidth, int nSrcHeight, IntPtr lpBits, [In] ref BITMAPINFO lpBitsInfo, uint iUsage, uint dwRop);
public static extern int StretchDIBits([In] SafeHandle hdc, int xDest, int yDest, int DestWidth, int DestHeight, int xSrc, int ySrc, int SrcWidth, int SrcHeight, IntPtr lpBits, [In] ref BITMAPINFO lpbmi, uint iUsage, uint rop);
public static extern int SetDIBitsToDevice(IntPtr hdc, int XDest, int YDest, int nDestWidth, int nDestHeight, int XSrc, int YSrc, int startScan, int scanLines, IntPtr lpBits, [In] ref BITMAPINFO lpBitsInfo, uint iUsage);
public void Start() { try { camHwnd = capCreateCaptureWindow("WebCam", 0, 0, 0, frameWidth, frameHeight, parentHwnd, camID); // connect to the device if (SendMessage(camHwnd, WM_CAP_DRIVER_CONNECT, 0, 0)) { BITMAPINFO bInfo = new BITMAPINFO(); bInfo.bmiHeader = new BITMAPINFOHEADER(); bInfo.bmiHeader.biSize = (uint)Marshal.SizeOf(bInfo.bmiHeader); bInfo.bmiHeader.biWidth = frameWidth; bInfo.bmiHeader.biHeight = frameHeight; bInfo.bmiHeader.biPlanes = 1; bInfo.bmiHeader.biBitCount = 24; // bits per frame, 24 - RGB //Enable preview mode. In preview mode, frames are transferred from the //capture hardware to system memory and then displayed in the capture //window using GDI functions. SendMessage(camHwnd, WM_CAP_SET_PREVIEW, 1, 0); SendMessage(camHwnd, WM_CAP_SET_PREVIEWRATE, 34, 0); // sets the frame display rate in preview mode SendBitmapMessage(camHwnd, WM_CAP_SET_VIDEOFORMAT, Marshal.SizeOf(bInfo), ref bInfo); frameThread = new Thread(new ThreadStart(this.FrameGrabber)); bStart = true; // First, set variable frameThread.Priority = ThreadPriority.Lowest; frameThread.Start(); // Only then put thread to the queue } else throw new Exception("Cannot connect to VFW device"); } catch (Exception e) { Stop(); MessageBox.Show("Error: " + e.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
internal static extern IntPtr CreateDIBSection(IntPtr hDC, BITMAPINFO pBMI, uint iUsage, int ppvBits, IntPtr hSection, uint dwOffset);
public static extern int StretchDIBits(IntPtr hdc, Int32 XDest, Int32 YDest, Int32 nDestWidth, Int32 nDestHeight, Int32 XSrc, Int32 YSrc, Int32 nSrcWidth, Int32 nSrcHeight, IntPtr lpBits, ref BITMAPINFO lpBitsInfo, UInt32 iUsage, UInt32 dwRop);
public void DrawTextOnGlass(IntPtr hwnd, String text, Font font, Rectangle ctlrct, int iglowSize) { if (IsCompositionEnabled()) { RECT rc = new RECT(); RECT rc2 = new RECT(); rc.left = ctlrct.Left; rc.right = ctlrct.Right + 2 * iglowSize; //make it larger to contain the glow effect rc.top = ctlrct.Top; rc.bottom = ctlrct.Bottom + 2 * iglowSize; //Just the same rect with rc,but (0,0) at the lefttop rc2.left = 0; rc2.top = 0; rc2.right = rc.right - rc.left; rc2.bottom = rc.bottom - rc.top; IntPtr destdc = GetDC(hwnd); //hwnd must be the handle of form,not control IntPtr Memdc = CreateCompatibleDC(destdc); // Set up a memory DC where we'll draw the text. IntPtr bitmap; IntPtr bitmapOld = IntPtr.Zero; IntPtr logfnotOld; int uFormat = DT_SINGLELINE | DT_CENTER | DT_VCENTER | DT_NOPREFIX; //text format BITMAPINFO dib = new BITMAPINFO(); dib.bmiHeader.biHeight = -(rc.bottom - rc.top); // negative because DrawThemeTextEx() uses a top-down DIB dib.bmiHeader.biWidth = rc.right - rc.left; dib.bmiHeader.biPlanes = 1; dib.bmiHeader.biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER)); dib.bmiHeader.biBitCount = 32; dib.bmiHeader.biCompression = BI_RGB; if (!(SaveDC(Memdc) == 0)) { bitmap = CreateDIBSection(Memdc, ref dib, DIB_RGB_COLORS, 0, IntPtr.Zero, 0); // Create a 32-bit bmp for use in offscreen drawing when glass is on if (!(bitmap == IntPtr.Zero)) { bitmapOld = SelectObject(Memdc, bitmap); IntPtr hFont = font.ToHfont(); logfnotOld = SelectObject(Memdc, hFont); try { System.Windows.Forms.VisualStyles.VisualStyleRenderer renderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active); DTTOPTS dttOpts = new DTTOPTS(); dttOpts.dwSize = (uint)Marshal.SizeOf(typeof(DTTOPTS)); dttOpts.dwFlags = DTT_COMPOSITED | DTT_GLOWSIZE; dttOpts.iGlowSize = iglowSize; DrawThemeTextEx(renderer.Handle, Memdc, 0, 0, text, -1, uFormat, ref rc2, ref dttOpts); BitBlt(destdc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, Memdc, 0, 0, SRCCOPY); } catch (Exception e) { Trace.WriteLine(e.Message); } //Remember to clean up SelectObject(Memdc, bitmapOld); SelectObject(Memdc, logfnotOld); DeleteObject(bitmap); DeleteObject(hFont); ReleaseDC(Memdc, -1); DeleteDC(Memdc); } } } }
public static extern int AVIStreamSetFormat( IntPtr aviStream, Int32 lPos, //ref BITMAPINFOHEADER lpFormat, ref BITMAPINFO lpFormat, Int32 cbFormat);
public static extern int AVIStreamReadFormat( IntPtr aviStream, Int32 lPos, ref BITMAPINFO lpFormat, ref Int32 cbFormat );
public static extern int GetDIBits([In] IntPtr hdc, [In] IntPtr hbmp, uint uStartScan, uint cScanLines, [Out] byte[] lpvBits, ref BITMAPINFO lpbi, DIB_Color_Mode uUsage);
//Draw text on the glass surface public void DrawTextOnGlass(IntPtr hwnd, String text, Font font, Rectangle ctlrct, int iglowSize) { if (isGlassSupported()) { RECT rc = new RECT(); RECT rc2 = new RECT(); rc.left = ctlrct.Left; rc.right = ctlrct.Right; rc.top = ctlrct.Top; rc.bottom = ctlrct.Bottom; rc2.left = 6; rc2.top = 5; rc2.right = rc.right - rc.left; rc2.bottom = rc.bottom - rc.top; IntPtr destdc = GetDC(hwnd); IntPtr Memdc = CreateCompatibleDC(destdc); IntPtr bitmap; IntPtr bitmapOld = IntPtr.Zero; IntPtr logfnotOld; int uFormat = DT_SINGLELINE | DT_NOPREFIX; BITMAPINFO dib = new BITMAPINFO(); dib.bmiHeader.biHeight = -(rc.bottom - rc.top); dib.bmiHeader.biWidth = rc.right - rc.left; dib.bmiHeader.biPlanes = 1; dib.bmiHeader.biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER)); dib.bmiHeader.biBitCount = 32; dib.bmiHeader.biCompression = BI_RGB; if (!(SaveDC(Memdc) == 0)) { bitmap = CreateDIBSection(Memdc, ref dib, DIB_RGB_COLORS, 0, IntPtr.Zero, 0); if (!(bitmap == IntPtr.Zero)) { bitmapOld = SelectObject(Memdc, bitmap); IntPtr hFont = font.ToHfont(); logfnotOld = SelectObject(Memdc, hFont); try { System.Windows.Forms.VisualStyles.VisualStyleRenderer renderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active); DTTOPTS dttOpts = new DTTOPTS(); dttOpts.dwSize = (uint)Marshal.SizeOf(typeof(DTTOPTS)); dttOpts.dwFlags = DTT_COMPOSITED | DTT_GLOWSIZE; dttOpts.iGlowSize = iglowSize; DrawThemeTextEx(renderer.Handle, Memdc, 0, 0, text, -1, uFormat, ref rc2, ref dttOpts); BitBlt(destdc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, Memdc, 0, 0, SRCCOPY); } catch (Exception e) { Console.WriteLine(e.Message); } SelectObject(Memdc, bitmapOld); SelectObject(Memdc, logfnotOld); DeleteObject(bitmap); DeleteObject(hFont); ReleaseDC(Memdc, -1); DeleteDC(Memdc); } } } }
private static extern int GetDIBits(SafeDeviceContextHandle hdc, SafeGdiObjectHandle hbmp, uint uStartScan, uint cScanLines, int[] lpvBits, ref BITMAPINFO lpbmi, uint uUsage);
public static extern IntPtr CreateDIBSection(IntPtr hdc, BITMAPINFO pbmi, uint iUsage, ref IntPtr ppvBits, IntPtr hSection, uint dwOffset);
static extern IntPtr CreateDIBSection(IntPtr hdc, [In] ref BITMAPINFO pbmi, uint iUsage, out IntPtr ppvBits, IntPtr hSection, uint dwOffset);
public static System.Drawing.Bitmap CopyToBpp(System.Drawing.Bitmap b, int bpp) { if (bpp != 1 && bpp != 8) { throw new System.ArgumentException("1 or 8", "bpp"); } // Plan: built into Windows GDI is the ability to convert // bitmaps from one format to another. Most of the time, this // job is actually done by the graphics hardware accelerator card // and so is extremely fast. The rest of the time, the job is done by // very fast native code. // We will call into this GDI functionality from C#. Our plan: // (1) Convert our Bitmap into a GDI hbitmap (ie. copy unmanaged->managed) // (2) Create a GDI monochrome hbitmap // (3) Use GDI "BitBlt" function to copy from hbitmap into monochrome (as above) // (4) Convert the monochrone hbitmap into a Bitmap (ie. copy unmanaged->managed) int w = b.Width, h = b.Height; IntPtr hbm = b.GetHbitmap(); // this is step (1) // // Step (2): create the monochrome bitmap. // "BITMAPINFO" is an interop-struct which we define below. // In GDI terms, it's a BITMAPHEADERINFO followed by an array of two RGBQUADs BITMAPINFO bmi = new BITMAPINFO(); bmi.biSize = 40; // the size of the BITMAPHEADERINFO struct bmi.biWidth = w; bmi.biHeight = h; bmi.biPlanes = 1; // "planes" are confusing. We always use just 1. Read MSDN for more info. bmi.biBitCount = (short)bpp; // ie. 1bpp or 8bpp bmi.biCompression = BI_RGB; // ie. the pixels in our RGBQUAD table are stored as RGBs, not palette indexes bmi.biSizeImage = (uint)(((w + 7) & 0xFFFFFFF8) * h / 8); bmi.biXPelsPerMeter = 1000000; // not really important bmi.biYPelsPerMeter = 1000000; // not really important // Now for the colour table. uint ncols = (uint)1 << bpp; // 2 colours for 1bpp; 256 colours for 8bpp bmi.biClrUsed = ncols; bmi.biClrImportant = ncols; bmi.cols = new uint[256]; // The structure always has fixed size 256, even if we end up using fewer colours if (bpp == 1) { bmi.cols[0] = MAKERGB(0, 0, 0); bmi.cols[1] = MAKERGB(255, 255, 255); } else { for (int i = 0; i < ncols; i++) { bmi.cols[i] = MAKERGB(i, i, i); } } // For 8bpp we've created an palette with just greyscale colours. // You can set up any palette you want here. Here are some possibilities: // greyscale: for (int i=0; i<256; i++) bmi.cols[i]=MAKERGB(i,i,i); // rainbow: bmi.biClrUsed=216; bmi.biClrImportant=216; int[] colv=new int[6]{0,51,102,153,204,255}; // for (int i=0; i<216; i++) bmi.cols[i]=MAKERGB(colv[i/36],colv[(i/6)%6],colv[i%6]); // optimal: a difficult topic: http://en.wikipedia.org/wiki/Color_quantization // // Now create the indexed bitmap "hbm0" IntPtr bits0; // not used for our purposes. It returns a pointer to the raw bits that make up the bitmap. IntPtr hbm0 = CreateDIBSection(IntPtr.Zero, ref bmi, DIB_RGB_COLORS, out bits0, IntPtr.Zero, 0); // // Step (3): use GDI's BitBlt function to copy from original hbitmap into monocrhome bitmap // GDI programming is kind of confusing... nb. The GDI equivalent of "Graphics" is called a "DC". IntPtr sdc = GetDC(IntPtr.Zero); // First we obtain the DC for the screen // Next, create a DC for the original hbitmap IntPtr hdc = CreateCompatibleDC(sdc); SelectObject(hdc, hbm); // and create a DC for the monochrome hbitmap IntPtr hdc0 = CreateCompatibleDC(sdc); SelectObject(hdc0, hbm0); // Now we can do the BitBlt: BitBlt(hdc0, 0, 0, w, h, hdc, 0, 0, SRCCOPY); // Step (4): convert this monochrome hbitmap back into a Bitmap: System.Drawing.Bitmap b0 = System.Drawing.Bitmap.FromHbitmap(hbm0); // // Finally some cleanup. DeleteDC(hdc); DeleteDC(hdc0); ReleaseDC(IntPtr.Zero, sdc); DeleteObject(hbm); DeleteObject(hbm0); // return(b0); }
static public extern IntPtr CreateDIBSection(IntPtr hdc, ref BITMAPINFO bmi, uint Usage, out IntPtr bits, IntPtr hSection, uint dwOffset);
private void DrawTextOnGlassInternal(IntPtr destdc, String text, Font font, Rectangle ctlrct, ContentAlignment textAlign, int iglowSize) { if (Glass.IsDesktopCompositionEnabled()) { RECT rc = new RECT(); RECT rc2 = new RECT(); var iglowExpand = (int)(iglowSize * 0.5); //iglowExpand = 0; //make it larger to contain the glow effect rc.left = ctlrct.Left - iglowExpand; rc.right = ctlrct.Right;// + iglowExpand; rc.top = ctlrct.Top - iglowExpand; rc.bottom = ctlrct.Bottom + iglowExpand; //Just the same rect with rc,but (0,0) at the lefttop rc2.left = iglowExpand; rc2.top = iglowExpand; rc2.right = rc.right - rc.left;// - iglowExpand; rc2.bottom = rc.bottom - rc.top - iglowExpand; IntPtr Memdc = CreateCompatibleDC(destdc); // Set up a memory DC where we'll draw the text. IntPtr bitmap; IntPtr bitmapOld = IntPtr.Zero; IntPtr logfnotOld; //int uFormat = DT_SINGLELINE | DT_CENTER | DT_VCENTER | DT_NOPREFIX; //text format TextFormatFlags uFormat = TextFormatFlags.NoPrefix; //text format //if ((textAlign == ContentAlignment.TopCenter) || // (textAlign == ContentAlignment.MiddleCenter) || // (textAlign == ContentAlignment.BottomCenter)) // uFormat |= TextFormatFlags.HorizontalCenter; //if ((textAlign == ContentAlignment.TopRight) || // (textAlign == ContentAlignment.MiddleRight) || // (textAlign == ContentAlignment.BottomRight)) // uFormat |= TextFormatFlags.Right; //if ((textAlign == ContentAlignment.MiddleLeft) || // (textAlign == ContentAlignment.MiddleCenter) || // (textAlign == ContentAlignment.MiddleRight)) // uFormat |= TextFormatFlags.VerticalCenter; //if ((textAlign == ContentAlignment.BottomLeft) || // (textAlign == ContentAlignment.BottomCenter) || // (textAlign == ContentAlignment.BottomRight)) // uFormat |= TextFormatFlags.Bottom; BITMAPINFO dib = new BITMAPINFO(); dib.bmiHeader.biHeight = -(rc.bottom - rc.top); // negative because DrawThemeTextEx() uses a top-down DIB dib.bmiHeader.biWidth = rc.right - rc.left; dib.bmiHeader.biPlanes = 1; dib.bmiHeader.biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER)); dib.bmiHeader.biBitCount = 32; dib.bmiHeader.biCompression = BI_RGB; if (!(SaveDC(Memdc) == 0)) { bitmap = CreateDIBSection(Memdc, ref dib, DIB_RGB_COLORS, 0, IntPtr.Zero, 0); // Create a 32-bit bmp for use in offscreen drawing when glass is on if (!(bitmap == IntPtr.Zero)) { bitmapOld = SelectObject(Memdc, bitmap); IntPtr hFont = font.ToHfont(); logfnotOld = SelectObject(Memdc, hFont); try { System.Windows.Forms.VisualStyles.VisualStyleRenderer renderer = new System.Windows.Forms.VisualStyles.VisualStyleRenderer(System.Windows.Forms.VisualStyles.VisualStyleElement.Window.Caption.Active); DTTOPTS dttOpts = new DTTOPTS(); dttOpts.dwSize = (uint)Marshal.SizeOf(typeof(DTTOPTS)); dttOpts.dwFlags = DTT_COMPOSITED | DTT_GLOWSIZE; dttOpts.iGlowSize = iglowSize; DrawThemeTextEx(renderer.Handle, Memdc, 0, 0, text, -1, (int)uFormat, ref rc2, ref dttOpts); BitBlt(destdc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, Memdc, 0, 0, SRCCOPY); } catch (Exception e) { Trace.WriteLine(e.Message); } //using (var image = GetImageFromHBitmap(bitmap)) //{ // image.Save(@"d:\temp\sqlupdate.png", System.Drawing.Imaging.ImageFormat.Png); //} //Remember to clean up SelectObject(Memdc, bitmapOld); SelectObject(Memdc, logfnotOld); DeleteObject(bitmap); DeleteObject(hFont); ReleaseDC(Memdc, -1); DeleteDC(Memdc); } } } }
public static SafeGDIHandle CreateDib(Rectangle bounds, IntPtr primaryHdc, SafeDeviceHandle memoryHdc) { BITMAPINFO info = new BITMAPINFO(); info.biSize = Marshal.SizeOf(info); info.biWidth = bounds.Width; info.biHeight = -bounds.Height; info.biPlanes = 1; info.biBitCount = 32; info.biCompression = 0; // BI_RGB SafeGDIHandle dib = CreateDIBSection(primaryHdc, info, 0, IntPtr.Zero, IntPtr.Zero, 0); SelectObject(memoryHdc, dib); return dib; }
private extern static int SetDIBitsToDevice(HandleRef hDC, int xDest, int yDest, int dwWidth, int dwHeight, int XSrc, int YSrc, int uStartScan, int cScanLines, ref int lpvBits, ref BITMAPINFO lpbmi, uint fuColorUse);
public void FillBlackRegion(Graphics gph, Rectangle rgn) { RECT rc = new RECT(); rc.left = rgn.Left; rc.right = rgn.Right; rc.top = rgn.Top; rc.bottom = rgn.Bottom; IntPtr destdc = gph.GetHdc(); //hwnd must be the handle of form,not control IntPtr Memdc = CreateCompatibleDC(destdc); IntPtr bitmap; IntPtr bitmapOld = IntPtr.Zero; BITMAPINFO dib = new BITMAPINFO(); dib.bmiHeader.biHeight = -(rc.bottom - rc.top); dib.bmiHeader.biWidth = rc.right - rc.left; dib.bmiHeader.biPlanes = 1; dib.bmiHeader.biSize = Marshal.SizeOf(typeof(BITMAPINFOHEADER)); dib.bmiHeader.biBitCount = 32; dib.bmiHeader.biCompression = BI_RGB; if (!(SaveDC(Memdc) == 0)) { bitmap = CreateDIBSection(Memdc, ref dib, DIB_RGB_COLORS, 0, IntPtr.Zero, 0); if (!(bitmap == IntPtr.Zero)) { bitmapOld = SelectObject(Memdc, bitmap); BitBlt(destdc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, Memdc, 0, 0, SRCCOPY); } //Remember to clean up SelectObject(Memdc, bitmapOld); DeleteObject(bitmap); ReleaseDC(Memdc, -1); DeleteDC(Memdc); } gph.ReleaseHdc(); }
public static extern int StretchDIBits( IntPtr hdc, // handle to DC int XDest, // x-coord of destination upper-left corner int YDest, // y-coord of destination upper-left corner int nDestWidth, // width of destination rectangle int nDestHeight, // height of destination rectangle int XSrc, // x-coord of source upper-left corner int YSrc, // y-coord of source upper-left corner int nSrcWidth, // width of source rectangle int nSrcHeight, // height of source rectangle IntPtr lpBits, // bitmap bits CONST VOID *lpBits ref BITMAPINFO lpBitsInfo, // bitmap data CONST BITMAPINFO *lpBitsInfo uint iUsage, // usage options uint dwRop // raster operation code );
public unsafe virtual bool Create(int width, int height, uint bitCount) { this.width = width; this.height = height; // Destroy existing objects. Destroy(); // Create a bitmap info structure. BITMAPINFO info = new BITMAPINFO(); // Set the data. info.biSize = 40; info.biBitCount = (Int16)bitCount; info.biPlanes = 1; info.biWidth = width; info.biHeight = height; // Create the bitmap. IntPtr ppvBits; hBitmap = CreateDIBSection(hDC, info, DIB_RGB_COLORS, out ppvBits, IntPtr.Zero, 0); SelectObject(hDC, hBitmap); // Set the OpenGL pixel format. SetPixelFormat(bitCount); return true; }
/// <summary> /// Gets the background image of the current visual style element within the specified background color. If <paramref name="states"/> /// is set, the resulting image will contain each of the state images side by side. /// </summary> /// <param name="rnd">The <see cref="VisualStyleRenderer"/> instance.</param> /// <param name="clr">The background color. This color cannot have an alpha channel.</param> /// <param name="states">The optional list of states to render side by side.</param> /// <returns>The background image.</returns> public static Bitmap GetBackgroundBitmap(this VisualStyleRenderer rnd, Color clr, int[] states = null) { const int wh = 200; if (rnd == null) { throw new ArgumentNullException(nameof(rnd)); } rnd.SetParameters(rnd.Class, rnd.Part, 0); if (states == null || states.Length == 0) { states = new[] { rnd.State } } ; var i = states.Length; // Get image size Size imgSz; using (var sg = Graphics.FromHwnd(IntPtr.Zero)) imgSz = rnd.GetPartSize(sg, new Rectangle(0, 0, wh, wh), ThemeSizeType.Draw); if (imgSz.Width == 0 || imgSz.Height == 0) { imgSz = new Size(rnd.GetInteger(IntegerProperty.Width), rnd.GetInteger(IntegerProperty.Height)); } if (imgSz.Width == 0 || imgSz.Height == 0) { using (var sg = Graphics.FromHwnd(IntPtr.Zero)) imgSz = MaxSize(rnd.GetPartSize(sg, new Rectangle(0, 0, wh, wh), ThemeSizeType.Minimum), imgSz); } var bounds = new Rectangle(0, 0, imgSz.Width * i, imgSz.Height); // Draw each background linearly down the bitmap using (var memoryHdc = SafeHDC.ScreenCompatibleDCHandle) { // Create a device-independent bitmap and select it into our DC var info = new BITMAPINFO(bounds.Width, -bounds.Height); using (memoryHdc.SelectObject(CreateDIBSection(HDC.NULL, info, DIBColorMode.DIB_RGB_COLORS, out var ppv))) { using (var memoryGraphics = Graphics.FromHdc(memoryHdc.DangerousGetHandle())) { // Setup graphics memoryGraphics.CompositingMode = CompositingMode.SourceOver; memoryGraphics.CompositingQuality = CompositingQuality.HighQuality; memoryGraphics.SmoothingMode = SmoothingMode.HighQuality; memoryGraphics.Clear(clr); // Draw each background linearly down the bitmap var rect = new Rectangle(0, 0, imgSz.Width, imgSz.Height); foreach (var state in states) { rnd.SetParameters(rnd.Class, rnd.Part, state); rnd.DrawBackground(memoryGraphics, rect); rect.X += imgSz.Width; } } // Copy DIB to Bitmap var bmp = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb); using (var primaryHdc = new SafeTempHDC(Graphics.FromImage(bmp))) BitBlt(primaryHdc, bounds.Left, bounds.Top, bounds.Width, bounds.Height, memoryHdc, 0, 0, RasterOperationMode.SRCCOPY); return(bmp); } } }