public ChartArea(Control parent) { Parent = parent; Left = 0; Top = 0; base.Height = 10; base.Width = 10; IntPtr hDC = WinApi.GetDC(Handle); bDC = WinApi.CreateCompatibleDC(hDC); bitmapGDI = WinApi.CreateCompatibleBitmap(hDC, Height, Width); oldBitmap = WinApi.SelectObject(bDC, bitmapGDI); WinApi.ReleaseDC(Handle, hDC); }
private void UpdateForm(Image backgroundImage) { IntPtr screenDc = WinApi.GetDC(IntPtr.Zero); IntPtr memDc = WinApi.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { //Display-image Bitmap bmp = new Bitmap(this.Width, this.Height); Graphics g = Graphics.FromImage(bmp); g.TextRenderingHint = TextRenderingHint.AntiAlias; DrawString(g, lblTime); hBitmap = bmp.GetHbitmap(Color.FromArgb(0)); //Set the fact that background is transparent oldBitmap = WinApi.SelectObject(memDc, hBitmap); //Display-rectangle Size size = bmp.Size; Point pointSource = new Point(0, 0); Point topPos = new Point(this.Left, this.Top); //Set up blending options WinApi.BLENDFUNCTION blend = new WinApi.BLENDFUNCTION(); blend.BlendOp = WinApi.AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; blend.AlphaFormat = WinApi.AC_SRC_ALPHA; WinApi.UpdateLayeredWindow(this.Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, WinApi.ULW_ALPHA); //Clean-up bmp.Dispose(); WinApi.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { WinApi.SelectObject(memDc, oldBitmap); WinApi.DeleteObject(hBitmap); } WinApi.DeleteDC(memDc); } catch { } }
private void SetBitmap(Bitmap bitmap, byte opacity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); } var screenDc = WinApi.GetDC(IntPtr.Zero); var memDc = WinApi.CreateCompatibleDC(screenDc); var hBitmap = IntPtr.Zero; var oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); oldBitmap = WinApi.SelectObject(memDc, hBitmap); var size = new WinApi.SIZE(bitmap.Width, bitmap.Height); var pointSource = new WinApi.POINT(0, 0); var topPos = new WinApi.POINT(Left, Top); var blend = new WinApi.BLENDFUNCTION { BlendOp = WinApi.AC_SRC_OVER, BlendFlags = 0, SourceConstantAlpha = opacity, AlphaFormat = WinApi.AC_SRC_ALPHA }; WinApi.UpdateLayeredWindow(Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, WinApi.ULW_ALPHA); } finally { WinApi.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { WinApi.SelectObject(memDc, oldBitmap); WinApi.DeleteObject(hBitmap); } WinApi.DeleteDC(memDc); } }
public static Bitmap GetBitmapWindowClient(IntPtr handle) { try { // get te hDC of the target window IntPtr hdcSrc = WinApi.GetWindowDC(handle); // get the size //RECT windowRect; //WinApi.GetWindowRect(handle, out windowRect); //int width = windowRect.Right - windowRect.Left; //int height = windowRect.Bottom - windowRect.Top; Rectangle rw = WinApi.GetWindowRectangle(handle); Rectangle rc = WinApi.GetClientRectangle(handle); int width = rc.Right - rc.Left; int height = rc.Bottom - rc.Top; // create a device context we can copy to IntPtr hdcDest = WinApi.CreateCompatibleDC(hdcSrc); // create a bitmap we can copy it to, // using GetDeviceCaps to get the width/height IntPtr hBitmap = WinApi.CreateCompatibleBitmap(hdcSrc, width, height); // select the bitmap object IntPtr hOld = WinApi.SelectObject(hdcDest, hBitmap); // bitblt over WinApi.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, rc.X - rw.X, rc.Y - rw.Y, WinApi.SRCCOPY); // restore selection WinApi.SelectObject(hdcDest, hOld); // clean up WinApi.DeleteDC(hdcDest); WinApi.ReleaseDC(handle, hdcSrc); Bitmap bitmap = System.Drawing.Image.FromHbitmap(hBitmap); // free up the Bitmap object WinApi.DeleteObject(hBitmap); return(bitmap); } catch (Exception) { return(null); } }
protected void SetBitmap(Bitmap bitmap, byte opacity) { if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { throw new ApplicationException("The bitmap must be 32ppp with alpha-channel."); } IntPtr dc = WinApi.GetDC(IntPtr.Zero); IntPtr intPtr = WinApi.CreateCompatibleDC(dc); IntPtr intPtr2 = IntPtr.Zero; IntPtr hObject = IntPtr.Zero; try { intPtr2 = bitmap.GetHbitmap(Color.FromArgb(0)); hObject = WinApi.SelectObject(intPtr, intPtr2); Size size = new Size(bitmap.Width, bitmap.Height); Point point = new Point(0, 0); Point point2 = new Point(base.Left, base.Top); WinApi.BLENDFUNCTION blendfunction = new WinApi.BLENDFUNCTION { BlendOp = 0, BlendFlags = 0, SourceConstantAlpha = opacity, AlphaFormat = 1 }; WinApi.UpdateLayeredWindow(base.Handle, dc, ref point2, ref size, intPtr, ref point, 0, ref blendfunction, 2); } finally { WinApi.ReleaseDC(IntPtr.Zero, dc); if (intPtr2 != IntPtr.Zero) { WinApi.SelectObject(intPtr, hObject); WinApi.DeleteObject(intPtr2); } WinApi.DeleteDC(intPtr); } }
private void Render() { var screenDc = WinApi.GetDC(WinApi.NullHandleRef); if (screenDc == IntPtr.Zero) { return; } try { var memDc = WinApi.CreateCompatibleDC(new HandleRef(null, screenDc)); if (memDc == IntPtr.Zero) { return; } try { using (Bitmap bmp = GetWindowBitmap(Size.Width, Size.Height)) { IntPtr hBitmap = bmp.GetHbitmap(_transparent); IntPtr hOldBitmap = WinApi.SelectObject(memDc, hBitmap); WinApi.POINT newLocation = new WinApi.POINT(Location); WinApi.SIZE newSize = new WinApi.SIZE(Size); WinApi.UpdateLayeredWindow(Handle, screenDc, ref newLocation, ref newSize, memDc, ref _ptZero, 0, ref _blend, WinApi.BlendingFlags.ULW_ALPHA); if (hBitmap != IntPtr.Zero) { WinApi.SelectObject(memDc, hOldBitmap); WinApi.DeleteObject(hBitmap); } } } finally { WinApi.DeleteDC(new HandleRef(null, memDc)); } } finally { WinApi.ReleaseDC(WinApi.NullHandleRef, new HandleRef(null, screenDc)); } }