public static bool Run(Graphics graphPort, Bitmap dstBitmap, Bitmap srcBitmap, Rectangle frame, int iterations) { IntPtr realDC = graphPort.GetHdc(); //IntPtr hDCDst = Graphics.FromImage(dstBitmap).GetHdc(true); IntPtr hDCDst = Graphics.FromImage(dstBitmap).GetHdc(); int XDst = 0; int YDst = 0; int nDstW = dstBitmap.Width; int nDstH = dstBitmap.Height; //IntPtr hDCSrc = Graphics.FromImage(srcBitmap).GetHdc(true); IntPtr hDCSrc = Graphics.FromImage(srcBitmap).GetHdc(); int XSrc = 0; int YSrc = 0; int nSrcW = srcBitmap.Width; int nSrcH = srcBitmap.Height; //bool success = StretchBlt(realDC, frame.Left, frame.Top, frame.Width, frame.Height, hDCSrc, 0, 0, nDstW, nDstH, SRCCOPY); for (int i = iterations; i >= 1; i--) { BLENDFUNCTION blender = new BLENDFUNCTION(AC_SRC_OVER, 0, (byte)(255 / i), 0); if (!AlphaBlend(hDCDst, XDst, YDst, nDstW, nDstH, hDCSrc, XSrc, YSrc, nSrcW, nSrcH, blender)) return false; bool success = StretchBlt(realDC, frame.Left, frame.Top, frame.Width, frame.Height, hDCDst, 0, 0, nDstW, nDstH, SRCCOPY); } return true; }
private static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
public static extern bool AlphaBlend(IntPtr hdcDest, int xoriginDest, int yoriginDest, int wDest, int hDest, IntPtr hdcSrc, int xoriginSrc, int yoriginSrc, int wSrc, int hSrc, BLENDFUNCTION ftn);
public void UpdatePaint() { // Get device contexts IntPtr screenDc = GetDC(IntPtr.Zero); IntPtr memDc = CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr hOldBitmap = IntPtr.Zero; try { // Get handle to the new bitmap and select it into the current // device context. Bitmap bitmap = new Bitmap(this.Width, this.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); using (Graphics gF = Graphics.FromImage(bitmap)) { SolidBrush brush = new SolidBrush(Color.FromArgb(0, 240, 248, 255)); gF.FillRectangle(brush, 0, 0, bitmap.Width, bitmap.Height); } Font textFont = FormManager.Instace.MyMainForm.MySettingManager.TextFont; // Set parameters for layered window update. FrameSize newSize = new FrameSize(bitmap.Width, bitmap.Height); FramePoint sourceLocation = new FramePoint(0, 0); FramePoint newLocation = new FramePoint(this.Left, this.Top); BLENDFUNCTION blend = new BLENDFUNCTION(); blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; blend.AlphaFormat = AC_SRC_ALPHA; Graphics g = Graphics.FromImage(bitmap); Color OutlineForeColor = FormManager.Instace.MyMainForm.MySettingManager.OutLineColor1; float OutlineWidth = 2; using (GraphicsPath gp = new GraphicsPath()) using (Pen outline = new Pen(OutlineForeColor, OutlineWidth) { LineJoin = LineJoin.Round }) using (StringFormat sf = new StringFormat()) using (Brush foreBrush = new SolidBrush(FormManager.Instace.MyMainForm.MySettingManager.TextColor)) { sf.Alignment = stringFormat.Alignment; Color backgroundColor = Color.FromArgb(alpha, Color.AliceBlue); g.Clear(backgroundColor); Rectangle rectangle = ClientRectangle; rectangle.X = 15; rectangle.Y = 15; rectangle.Width -= 15; rectangle.Height -= 15; if (isActiveGDI) { try { gp.AddString(resultText, textFont.FontFamily, (int)textFont.Style, g.DpiY * textFont.Size / 72, rectangle, sf); } catch (Exception ex) { //MessageBox.Show(ex.ToString()); TransFormLayer.isActiveGDI = false; CustomLabel.isActiveGDI = false; if (DialogResult.OK == MessageBox.Show("GDI+ 가 작동하지 않습니다. \n레이어 번역창의 일부 기능을 사용할 수 없습니다.\n해결법을 확인해 보겠습니까? ", "GDI+ 에서 일반 오류가 발생했습니다.", MessageBoxButtons.OKCancel)) { try { System.Diagnostics.Process.Start("https://blog.naver.com/killkimno/70185869419"); } catch { } } } } g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; if (isStart) { if (FormManager.Instace.MyMainForm.MySettingManager.NowIsUseBackColor) { CharacterRange[] characterRanges = { new CharacterRange(0, resultText.Length) }; sf.SetMeasurableCharacterRanges(characterRanges); Region[] stringRegions = g.MeasureCharacterRanges(resultText, textFont, rectangle, sf); if (stringRegions.Length > 0) { // Draw rectangle for first measured range. RectangleF measureRect1 = stringRegions[0].GetBounds(g); SolidBrush backColorBrush = new SolidBrush(FormManager.Instace.MyMainForm.MySettingManager.BackgroundColor); g.FillRectangle(backColorBrush, measureRect1.X - 8, measureRect1.Y - 4, measureRect1.Width + 16, measureRect1.Height + 8); } } } else { using (Pen layerOutline = new Pen(Color.FromArgb(40, 134, 249), 3) { LineJoin = LineJoin.Round }) g.DrawRectangle(layerOutline, ClientRectangle); } g.SmoothingMode = SmoothingMode.HighQuality; if (isActiveGDI) { using (Pen outline2 = new Pen(FormManager.Instace.MyMainForm.MySettingManager.OutLineColor2, 5) { LineJoin = LineJoin.Round }) g.DrawPath(outline2, gp); g.DrawPath(outline, gp); g.FillPath(foreBrush, gp); } else { g.DrawString(resultText, textFont, foreBrush, rectangle); } } hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); //Set the fact that background is transparent hOldBitmap = SelectObject(memDc, hBitmap); // Update the window. if (this == null || this.IsDisposed || this.isDestroyFormFlag) { return; } UpdateLayeredWindow( this.Handle, // Handle to the layered window screenDc, // Handle to the screen DC ref newLocation, // New screen position of the layered window ref newSize, // New size of the layered window memDc, // Handle to the layered window surface DC ref sourceLocation, // Location of the layer in the DC 0, // Color key of the layered window ref blend, // Transparency of the layered window ULW_ALPHA // Use blend as the blend function ); //SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS); } finally { // Release device context. ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { SelectObject(memDc, hOldBitmap); DeleteObject(hBitmap); } DeleteDC(memDc); GC.Collect(); /* * IntPtr screenDc = GetDC(IntPtr.Zero); * IntPtr memDc = CreateCompatibleDC(screenDc); * IntPtr hBitmap = IntPtr.Zero; * IntPtr hOldBitmap = IntPtr.Zero; */ } //Util.ShowLog("end"); }
/// <summary> /// 画面を背景画像でブレンド表示します。 /// </summary> /// <param name="alpha">アルファ値</param> private void SetBackGroundLayer(int alpha) { // デバイスコンテキストを取得 Bitmap bmp = base.BackgroundImage as Bitmap; if (bmp == null) { return; } IntPtr screenDc = GetDC(IntPtr.Zero); IntPtr memDc = CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr hOldBitmap = IntPtr.Zero; try { hBitmap = bmp.GetHbitmap(Color.FromArgb(0)); hOldBitmap = SelectObject(memDc, hBitmap); // BLENDFUNCTION を初期化 BLENDFUNCTION blend = new BLENDFUNCTION(); blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = (byte)alpha; blend.AlphaFormat = AC_SRC_ALPHA; Size size = new Size(bmp.Width, bmp.Height); Point pointDst = this.Location; Point pointSrc = new Point(0, 0); // レイヤードウィンドウを更新 bool r = UpdateLayeredWindow(_blendForm.Handle, screenDc, ref pointDst, ref size, memDc, ref pointSrc, 0, ref blend, ULW_ALPHA); } finally { ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { SelectObject(memDc, hOldBitmap); DeleteObject(hBitmap); } DeleteDC(memDc); } }
public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
public static extern int UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pptSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);
public static extern int UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pptSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
private static void GlowWindow_DrawLayeredWindow(IntPtr hwnd) { //Prepare Bitmap bitmap = null; //Get window rect if (User32.GetWindowRect(hwnd, out RECT wndRect)) { //Get parent IntPtr parentHWnd = User32.GetParent(hwnd); try { //Create bitmap bitmap = new Bitmap(wndRect.Width, wndRect.Height); //Draw using (Graphics g = Graphics.FromImage(bitmap)) { //Clear g.Clear(Color.Transparent); g.PageUnit = GraphicsUnit.Pixel; //Check if (hostDictionary.ContainsKey(parentHWnd)) { GlowWindowHost host = hostDictionary[parentHWnd]; //Draw if (hwnd == host.leftHandle.Handle) { LeftGlow_DrawLayeredWindow(host.glowTextures, g, wndRect.Width, wndRect.Height); } if (hwnd == host.topHandle.Handle) { TopGlow_DrawLayeredWindow(host.glowTextures, g, wndRect.Width, wndRect.Height); } if (hwnd == host.rightHandle.Handle) { RightGlow_DrawLayeredWindow(host.glowTextures, g, wndRect.Width, wndRect.Height); } if (hwnd == host.bottomHandle.Handle) { BottomGlow_DrawLayeredWindow(host.glowTextures, g, wndRect.Width, wndRect.Height); } } } } catch { } //Prepare IntPtr screenDc = User32.GetDC(IntPtr.Zero); IntPtr memDc = Gdi32.CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap(Color.FromArgb(0x0)); oldBitmap = Gdi32.SelectObject(memDc, hBitmap); SIZE sz = new SIZE(wndRect.Width, wndRect.Height); POINT ptSrc = new POINT(0, 0); POINT topPos = new POINT(wndRect.X, wndRect.Y); BLENDFUNCTION blend = new BLENDFUNCTION() { BlendOp = 0x0, BlendFlags = 0x0, SourceConstantAlpha = 0xFF, AlphaFormat = 0x01, }; //Update layered window User32.UpdateLayeredWindow(hwnd, screenDc, ref topPos, ref sz, memDc, ref ptSrc, 0, ref blend, 0x00000002); } finally { User32.ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { Gdi32.SelectObject(memDc, oldBitmap); Gdi32.DeleteDC(hBitmap); } Gdi32.DeleteDC(memDc); //Dispose of bitmap bitmap.Dispose(); } } }
private static extern bool UpdateLayeredWindow(IntPtr hWnd, IntPtr hdcDst, PointOrSize pptDst, PointOrSize pSizeDst, IntPtr hdcSrc, PointOrSize pptSrc, int crKey, ref BLENDFUNCTION pBlend, int dwFlags);
private bool SelectBitmap(Bitmap bitmap, int opacity) { // Does this bitmap contain an alpha channel? if (bitmap.PixelFormat != PixelFormat.Format32bppArgb) { return(false); } // Get device contexts IntPtr screenDc = GetDC(IntPtr.Zero); IntPtr memDc = CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr hOldBitmap = IntPtr.Zero; try { // Get handle to the new bitmap and select it into the current // device context. hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); hOldBitmap = SelectObject(memDc, hBitmap); // Set parameters for layered window update. Size newSize = new Size(bitmap.Width, bitmap.Height); Point sourceLocation = new Point(0, 0); Point newLocation = new Point(this.Left, this.Top); BLENDFUNCTION blend = new BLENDFUNCTION(); blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = (byte)opacity; blend.AlphaFormat = AC_SRC_ALPHA; // Update the window. UpdateLayeredWindow( this.Handle, // Handle to the layered window screenDc, // Handle to the screen DC ref newLocation, // New screen position of the layered window ref newSize, // New size of the layered window memDc, // Handle to the layered window surface DC ref sourceLocation, // Location of the layer in the DC 0, // Color key of the layered window ref blend, // Transparency of the layered window ULW_ALPHA // Use blend as the blend function ); return(true); } catch { } finally { // Release device context. ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { SelectObject(memDc, hOldBitmap); DeleteObject(hBitmap); } DeleteDC(memDc); } return(false); }
public void UpdateFormDisplay(bool draw) { IntPtr screenDc = GetDC(IntPtr.Zero); //Display-rectangle Size size = new Size(this.Width, this.Height); Point pointSource = new Point(0, 0); Point topPos = new Point(this.Left, this.Top); //Set up blending options BLENDFUNCTION blend = new BLENDFUNCTION(); blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; // additional alpha multiplier to the whole image. value 255 means multiply with 1. blend.AlphaFormat = AC_SRC_ALPHA; if (draw) UpdateLayeredWindow(this.Handle, screenDc, ref topPos, ref size, canvusDc, ref pointSource, 0, ref blend, ULW_ALPHA); else UpdateLayeredWindow(this.Handle, screenDc, ref topPos, ref size, blankcanvusDc, ref pointSource, 0, ref blend, ULW_ALPHA); //Clean-up ReleaseDC(IntPtr.Zero, screenDc); }
public static extern int UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, [In] ref Point pptDst, [In] ref System.Drawing.Size psize, IntPtr hdcSrc, [In] ref System.Drawing.Point pptSrc, int crKey, [In] ref BLENDFUNCTION pblend, int dwFlags);
/// <summary> /// Renders the tabs and then calls <see cref="User32.UpdateLayeredWindow" /> to blend the tab content with the underlying window ( /// <see cref="_parentForm" />). /// </summary> /// <param name="cursorPosition">Current position of the cursor.</param> /// <param name="forceRedraw">Flag indicating whether a full render should be forced.</param> public void Render(Point cursorPosition, bool forceRedraw = false) { if (!IsDisposed && _parentForm.TabRenderer != null && _parentForm.WindowState != FormWindowState.Minimized && _parentForm.ClientRectangle.Width > 0) { cursorPosition = GetRelativeCursorPosition(cursorPosition); using (Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format32bppArgb)) { using (Graphics graphics = Graphics.FromImage(bitmap)) { DrawTitleBarBackground(graphics); // Since classic mode themes draw over the *entire* titlebar, not just the area immediately behind the tabs, we have to offset the tabs // when rendering in the window Point offset = _parentForm.WindowState != FormWindowState.Maximized && DisplayType == DisplayType.Classic ? new Point(0, SystemInformation.CaptionButtonSize.Height) : _parentForm.WindowState != FormWindowState.Maximized ? new Point(0, SystemInformation.VerticalResizeBorderThickness - SystemInformation.BorderSize.Height) : new Point(0, 0); // Render the tabs into the bitmap _parentForm.TabRenderer.Render(_parentForm.Tabs, graphics, offset, cursorPosition, forceRedraw); // Cut out a hole in the background so that the control box on the underlying window can be shown if (DisplayType == DisplayType.Classic && (_parentForm.ControlBox || _parentForm.MaximizeBox || _parentForm.MinimizeBox)) { int boxWidth = 0; if (_parentForm.ControlBox) { boxWidth += SystemInformation.CaptionButtonSize.Width; } if (_parentForm.MinimizeBox) { boxWidth += SystemInformation.CaptionButtonSize.Width; } if (_parentForm.MaximizeBox) { boxWidth += SystemInformation.CaptionButtonSize.Width; } CompositingMode oldCompositingMode = graphics.CompositingMode; graphics.CompositingMode = CompositingMode.SourceCopy; graphics.FillRectangle( new SolidBrush(Color.Transparent), Width - boxWidth, 0, boxWidth, SystemInformation.CaptionButtonSize.Height); graphics.CompositingMode = oldCompositingMode; } IntPtr screenDc = User32.GetDC(IntPtr.Zero); IntPtr memDc = Gdi32.CreateCompatibleDC(screenDc); IntPtr oldBitmap = IntPtr.Zero; IntPtr bitmapHandle = IntPtr.Zero; try { // Copy the contents of the bitmap into memDc bitmapHandle = bitmap.GetHbitmap(Color.FromArgb(0)); oldBitmap = Gdi32.SelectObject(memDc, bitmapHandle); SIZE size = new SIZE { cx = bitmap.Width, cy = bitmap.Height }; POINT pointSource = new POINT { x = 0, y = 0 }; POINT topPos = new POINT { x = Left, y = Top }; BLENDFUNCTION blend = new BLENDFUNCTION { // We want to blend the bitmap's content with the screen content under it BlendOp = Convert.ToByte((int)AC.AC_SRC_OVER), BlendFlags = 0, SourceConstantAlpha = 255, // We use the bitmap's alpha channel for blending instead of a pre-defined transparency key AlphaFormat = Convert.ToByte((int)AC.AC_SRC_ALPHA) }; // Blend the tab content with the underlying content if (!User32.UpdateLayeredWindow( Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, ULW.ULW_ALPHA)) { int error = Marshal.GetLastWin32Error(); throw new Win32Exception(error, "Error while calling UpdateLayeredWindow()."); } } // Clean up after ourselves finally { User32.ReleaseDC(IntPtr.Zero, screenDc); if (bitmapHandle != IntPtr.Zero) { Gdi32.SelectObject(memDc, oldBitmap); Gdi32.DeleteObject(bitmapHandle); } Gdi32.DeleteDC(memDc); } } } } }
static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pptSrc, uint crKey, [In] ref BLENDFUNCTION pblend, uint dwFlags);
/// <summary> /// Creates a new transparent layered window. /// </summary> /// <param name="bmp">To be layered bitmap</param> /// <param name="transparency">Alpha value</param> private void GDIWindow(Bitmap bmp, byte transparency) { IntPtr hDC = GetDC(IntPtr.Zero); try { IntPtr hMemDC = CreateCompatibleDC(hDC); try { IntPtr hBmp = bmp.GetHbitmap(Color.FromArgb(0)); try { IntPtr previousBmp = SelectObject(hMemDC, hBmp); try { Point ptDst = new Point(Left, Top); Size size = new Size(bmp.Width, bmp.Height); Point ptSrc = new Point(0, 0); BLENDFUNCTION blend = new BLENDFUNCTION(); blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = transparency; blend.AlphaFormat = AC_SRC_ALPHA; UpdateLayeredWindow( Handle, hDC, ref ptDst, ref size, hMemDC, ref ptSrc, 0, ref blend, LWA_ALPHA); } finally { SelectObject(hDC, previousBmp); } } finally { DeleteObject(hBmp); } } finally { DeleteDC(hMemDC); } } finally { ReleaseDC(IntPtr.Zero, hDC); } }
public extern static Bool UpdateLayeredWindow(IntPtr handle, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);
private void FlushGraphicsToScreen() { var ps = Screen.PrimaryScreen; var pDest = new Point(ps.WorkingArea.Right - ClientSize.Width, ps.WorkingArea.Bottom - ClientSize.Height); var pSource = new Point(); var pSize = ClientSize; var blendFunction = new BLENDFUNCTION { AlphaFormat = AC_SRC_ALPHA, BlendFlags = 0, BlendOp = AC_SRC_OVER, SourceConstantAlpha = 255 }; SetWindowPos( Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE ); UpdateLayeredWindow( Handle, ScreenDC, ref pDest, ref pSize, BitmapDC, ref pSource, 0, ref blendFunction, ULW_ALPHA ); }
/// <summary> /// レイヤード ウィンドウを設定します。 /// </summary> /// <param name="srcBitmap">表示する画像</param> public void SetLayeredWindow(Bitmap srcBitmap) { MethodInvoker proc = (MethodInvoker)delegate { // デバイスコンテキストを取得 IntPtr screenDc = IntPtr.Zero; IntPtr memDc = IntPtr.Zero; IntPtr hBitmap = IntPtr.Zero; IntPtr hOldBitmap = IntPtr.Zero; try { screenDc = GetDC(IntPtr.Zero); memDc = CreateCompatibleDC(screenDc); hBitmap = srcBitmap.GetHbitmap(Color.FromArgb(0)); hOldBitmap = SelectObject(memDc, hBitmap); // BLENDFUNCTION を初期化 BLENDFUNCTION blend = new BLENDFUNCTION(); blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; blend.AlphaFormat = AC_SRC_ALPHA; // レイヤードウィンドウを更新 this.Size = new Size(srcBitmap.Width, srcBitmap.Height); Point pptDst = new Point(this.Left, this.Top); Size psize = new Size(this.Width, this.Height); Point pptSrc = new Point(0, 0); UpdateLayeredWindow( this.Handle, screenDc, ref pptDst, ref psize, memDc, ref pptSrc, 0, ref blend, ULW_ALPHA); } finally { if (screenDc != IntPtr.Zero) { ReleaseDC(IntPtr.Zero, screenDc); } if (hBitmap != IntPtr.Zero) { SelectObject(memDc, hOldBitmap); DeleteObject(hBitmap); } if (memDc != IntPtr.Zero) { DeleteDC(memDc); } } }; try { if (this.InvokeRequired) { this.Invoke(proc); } else { proc.Invoke(); } } catch { } }
/// <summary> /// 静态构造 /// </summary> static BLENDFUNCTION() { Empty = new BLENDFUNCTION(); Default = new BLENDFUNCTION(NativeMethods.AC_SRC_OVER, 0, 255, NativeMethods.AC_SRC_ALPHA); }
private static extern bool AlphaBlend(IntPtr hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, BLENDFUNCTION blendFunction);
private static extern bool UpdateLayeredWindow(IntPtr hWnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pptSrc, uint crKey, [In] ref BLENDFUNCTION pblend, uint dwFlags);
protected override void OnPaint(CefBrowser browser, CefPaintElementType type, CefRectangle[] dirtyRects, IntPtr buffer, int width, int height) { if (width <= 0 || height <= 0) { return; } var handle = _owner.HostWindowHandle; CreateDeviceResource(); _renderTarget.BeginDraw(); if (type == CefPaintElementType.View) { var bmp = _renderTarget.CreateBitmap(new Vortice.Mathematics.Size(width, height), buffer, width * 4, new BitmapProperties(new PixelFormat(Vortice.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied))); if (!_isPopupShown) { _renderTarget.Clear(Color.Transparent); } _renderTarget.DrawBitmap(bmp); bmp.Dispose(); } else if (type == CefPaintElementType.Popup) { var bmp = _renderTarget.CreateBitmap(new Vortice.Mathematics.Size(width, height), buffer, width * 4, new BitmapProperties(new PixelFormat(Vortice.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied))); if (_cachedPopupImage != null) { _cachedPopupImage.Dispose(); _cachedPopupImage = null; GC.Collect(); } _cachedPopupImage = _renderTarget.CreateSharedBitmap(bmp, new BitmapProperties { PixelFormat = new Vortice.Direct2D1.PixelFormat(Vortice.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Premultiplied) }); bmp.Dispose(); } if (_cachedPopupImage != null && _isPopupShown && _popupRect.HasValue) { var scaleFactor = DpiHelper.GetScaleFactorForCurrentWindow(_owner.HostWindowHandle); var x = _popupRect.Value.X * scaleFactor; var y = _popupRect.Value.Y * scaleFactor; var popupWidth = _popupRect.Value.Width * scaleFactor; var popupHeight = _popupRect.Value.Height * scaleFactor; var right = x + popupWidth; var bottom = y + popupHeight; _renderTarget.DrawBitmap(_cachedPopupImage, new Vortice.RawRectF(x, y, right, bottom), 1f, BitmapInterpolationMode.Linear, new Vortice.RawRectF(0, 0, popupWidth, popupHeight)); } if (_renderTarget.EndDraw().Failure) { DiscardDeviceResources(); } unsafe { var rect = new RECT(); User32.GetWindowRect(handle, ref rect); var newLocation = new POINT(rect.left, rect.top); var newSize = new SIZE(rect.Width, rect.Height); var zeroPoint = new POINT(0, 0); if (rect.Width == 0 || rect.Height == 0) { return; } var blend = new BLENDFUNCTION { BlendOp = AcSrcOver, BlendFlags = 0, SourceConstantAlpha = 255, AlphaFormat = AcSrcAlpha }; var ulwi = new UPDATELAYEREDWINDOWINFO { cbSize = Marshal.SizeOf(typeof(UPDATELAYEREDWINDOWINFO)), hdcDst = _screenDC, pptDst = &newLocation, psize = &newSize, hdcSrc = _memDC, pptSrc = &zeroPoint, //crKey = new COLORREF(0), pblend = &blend, dwFlags = BlendFlags.ULW_ALPHA, prcDirty = null }; User32.UpdateLayeredWindowIndirect(_owner.HostWindowHandle, ref ulwi); } GC.Collect(); }
public void Show() { _VerifyMutability(); Stream imageStream = null; try { // Try to use the filepath first. If it's not provided or not available, use the embedded resource. if (!string.IsNullOrEmpty(ImageFileName) && File.Exists(ImageFileName)) { try { imageStream = new FileStream(ImageFileName, FileMode.Open); } catch (IOException) { } } if (imageStream == null) { imageStream = _resourceManager.GetStream(ResourceName, CultureInfo.CurrentUICulture); if (imageStream == null) { throw new IOException("The resource could not be found."); } } Size bitmapSize; _hBitmap = _CreateHBITMAPFromImageStream(imageStream, out bitmapSize); Point location = new Point( (NativeMethods.GetSystemMetrics(SM.CXSCREEN) - bitmapSize.Width) / 2, (NativeMethods.GetSystemMetrics(SM.CYSCREEN) - bitmapSize.Height) / 2); // Pass a null WndProc. Let the MessageWindow use DefWindowProc. _hwndWrapper = new MessageWindow( CS.HREDRAW | CS.VREDRAW, WS.POPUP | WS.VISIBLE, WS_EX.WINDOWEDGE | WS_EX.TOOLWINDOW | WS_EX.LAYERED | (IsTopMost ? WS_EX.TOPMOST : 0), new Rect(location, bitmapSize), "Splash Screen", null); byte opacity = (byte)(FadeInDuration > TimeSpan.Zero ? 0 : 255); using (SafeDC hScreenDC = SafeDC.GetDesktop()) { using (SafeDC memDC = SafeDC.CreateCompatibleDC(hScreenDC)) { IntPtr hOldBitmap = NativeMethods.SelectObject(memDC, _hBitmap); RECT hwndRect = NativeMethods.GetWindowRect(_hwndWrapper.Handle); POINT hwndPos = hwndRect.Position; SIZE hwndSize = hwndRect.Size; POINT origin = new POINT(); BLENDFUNCTION bf = _BaseBlendFunction; bf.SourceConstantAlpha = opacity; NativeMethods.UpdateLayeredWindow(_hwndWrapper.Handle, hScreenDC, ref hwndPos, ref hwndSize, memDC, ref origin, 0, ref bf, ULW.ALPHA); NativeMethods.SelectObject(memDC, hOldBitmap); } } if (CloseOnMainWindowCreation) { Dispatcher.CurrentDispatcher.BeginInvoke( DispatcherPriority.Loaded, (DispatcherOperationCallback) delegate(object splashObj) { var splashScreen = (SplashScreen)splashObj; if (!splashScreen._isClosed) { splashScreen.Close(); } return(null); }, this); } _dispatcher = Dispatcher.CurrentDispatcher; if (FadeInDuration > TimeSpan.Zero) { _fadeInEnd = DateTime.UtcNow + FadeInDuration; _dt = new DispatcherTimer(FadeInDuration, DispatcherPriority.Normal, _FadeInTick, _dispatcher); _dt.Start(); } } finally { Utility.SafeDispose(ref imageStream); } }
internal static extern BOOL UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pprSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);
public void UpdatePaint() { if (isLockPaint) { Util.ShowLog("Lock Paint!!!!"); return; } isLockPaint = true; CheckSizeAndLocation(); Util.ShowLog("Update paint + " + makeIndex); // Get device contexts IntPtr screenDc = GetDC(IntPtr.Zero); IntPtr memDc = CreateCompatibleDC(screenDc); IntPtr hBitmap = IntPtr.Zero; IntPtr hOldBitmap = IntPtr.Zero; try { if (bitmap == null || bitmap.Width != this.Width || bitmap.Height != Height) { bitmap = new Bitmap(this.Width, this.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); } using (Graphics gF = Graphics.FromImage(bitmap)) { SolidBrush brush = new SolidBrush(Color.FromArgb(0, 240, 248, 255)); gF.FillRectangle(brush, 0, 0, bitmap.Width, bitmap.Height); } Font textFont = FormManager.Instace.MyMainForm.MySettingManager.TextFont; // Set parameters for layered window update. FrameSize newSize = new FrameSize(bitmap.Width, bitmap.Height); FramePoint sourceLocation = new FramePoint(0, 0); FramePoint newLocation = new FramePoint(this.Left, this.Top); BLENDFUNCTION blend = new BLENDFUNCTION(); blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; blend.AlphaFormat = AC_SRC_ALPHA; Graphics g = Graphics.FromImage(bitmap); Color OutlineForeColor = FormManager.Instace.MyMainForm.MySettingManager.OutLineColor1; float OutlineWidth = 2; using (GraphicsPath gp = new GraphicsPath()) using (Pen outline = new Pen(OutlineForeColor, OutlineWidth) { LineJoin = LineJoin.Round }) using (StringFormat sf = new StringFormat()) using (Brush foreBrush = new SolidBrush(FormManager.Instace.MyMainForm.MySettingManager.TextColor)) { sf.Alignment = stringFormat.Alignment; Color backgroundColor = Color.FromArgb(alpha, Color.Red); g.Clear(backgroundColor); Rectangle rectangle = ClientRectangle; rectangle.X = this.Location.X; rectangle.Y = this.Location.Y; AddText(gp, g, textFont, rectangle, sf); g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; g.SmoothingMode = SmoothingMode.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; if (!isStart) { using (Pen layerOutline = new Pen(Color.FromArgb(40, 134, 249), 3) { LineJoin = LineJoin.Round }) g.DrawRectangle(layerOutline, ClientRectangle); } g.SmoothingMode = SmoothingMode.HighQuality; if (isActiveGDI) { using (Pen outline2 = new Pen(FormManager.Instace.MyMainForm.MySettingManager.OutLineColor2, 5) { LineJoin = LineJoin.Round }) g.DrawPath(outline2, gp); g.DrawPath(outline, gp); g.FillPath(foreBrush, gp); } else { g.DrawString(resultText, textFont, foreBrush, rectangle); } } if (!isStart) { g.Clear(Color.FromArgb(0)); } hBitmap = bitmap.GetHbitmap(Color.FromArgb(0)); //Set the fact that background is transparent hOldBitmap = SelectObject(memDc, hBitmap); // Update the window. if (this == null || this.IsDisposed || this.isDestroyFormFlag) { return; } UpdateLayeredWindow( this.Handle, // Handle to the layered window screenDc, // Handle to the screen DC ref newLocation, // New screen position of the layered window ref newSize, // New size of the layered window memDc, // Handle to the layered window surface DC ref sourceLocation, // Location of the layer in the DC 0, // Color key of the layered window ref blend, // Transparency of the layered window ULW_ALPHA // Use blend as the blend function ); //SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS); } finally { // Release device context. ReleaseDC(IntPtr.Zero, screenDc); if (hBitmap != IntPtr.Zero) { SelectObject(memDc, hOldBitmap); DeleteObject(hBitmap); } DeleteDC(memDc); GC.Collect(); } isLockPaint = false; }
static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref FramePoint pptDst, ref FrameSize psize, IntPtr hdcSrc, ref FramePoint pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
/// <summary> /// Sets the bitmap to use for this form. /// </summary> private void SetBitmap( Bitmap bitmap ) { // Collect some variables we'll use to render. IntPtr screenDc = GetDC( IntPtr.Zero ); IntPtr memDc = CreateCompatibleDC( screenDc ); IntPtr hBitmap = IntPtr.Zero; IntPtr oldBitmap = IntPtr.Zero; try { hBitmap = bitmap.GetHbitmap( Color.FromArgb(0) ); oldBitmap = SelectObject( memDc, hBitmap ); W32Size size = new W32Size( bitmap.Width, bitmap.Height ); W32Point pointSource = new W32Point( 0, 0 ); W32Point topPos = new W32Point( this.Left, this.Top ); BLENDFUNCTION blend = new BLENDFUNCTION(); blend.BlendOp = 0x00; // AC_SRC_OVER blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; blend.AlphaFormat = 0x01; // AC_SRC_ALPHA lock( this ) if( !Disposing ) UpdateLayeredWindow( Handle, screenDc, ref topPos, ref size, memDc, ref pointSource, 0, ref blend, (Int32) 0x00000002 // ULW_ALPHA ); } catch { /* Ignore errors */ } finally { // Clean things up... ReleaseDC( IntPtr.Zero, screenDc ); if( hBitmap != IntPtr.Zero ) { SelectObject( memDc, oldBitmap ); DeleteObject( hBitmap ); } DeleteDC( memDc ); } }
public static extern int UpdateLayeredWindow(IntPtr hWnd, IntPtr hDcDst, ref Point pPtDst, ref Size pSize, IntPtr hDcSrc, ref Point pPtSrc, int crKey, ref BLENDFUNCTION pBlend, int dwFlags);
public static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref UI.Docking.Win32.POINT pptDst, ref UI.Docking.Win32.SIZE psize, IntPtr hdcSrc, ref UI.Docking.Win32.POINT pprSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);
public static bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, BlendFlags dwFlags) { NotImplemented(MethodBase.GetCurrentMethod()); return(false); }
public static extern Bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref Point pptDst, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);
private extern static Bool UpdateLayeredWindow(IntPtr handle, IntPtr hdcDst, ref point pptDst, ref size psize, IntPtr hdcSrc, ref point pprSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);
/// <summary> /// レイヤードウィンドウを作成します。 /// </summary> /// <param name="src">元になる画像。</param> public void SetLayeredWindow( Bitmap src ) { // GetDeviceContext IntPtr screenDc = IntPtr.Zero; IntPtr memDc = IntPtr.Zero; IntPtr hBitmap = IntPtr.Zero; IntPtr hOldBitmap = IntPtr.Zero; try { screenDc = GetDC( IntPtr.Zero ); memDc = CreateCompatibleDC( screenDc ); hBitmap = src.GetHbitmap( Color.FromArgb( 0 ) ); hOldBitmap = SelectObject( memDc, hBitmap ); BLENDFUNCTION blend = new BLENDFUNCTION(); blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; blend.AlphaFormat = AC_SRC_ALPHA; //Size = new Size( src.Width, src.Height ); Point pptDst = new Point( this.Left, this.Top ); Size psize = new Size( this.Width, this.Height ); Point pptSrc = new Point( 0, 0 ); UpdateLayeredWindow( this.Handle, screenDc, ref pptDst, ref psize, memDc, ref pptSrc, 0, ref blend, ULW_ALPHA ); } finally { if ( screenDc != IntPtr.Zero ) { ReleaseDC( IntPtr.Zero, screenDc ); } if ( hBitmap != IntPtr.Zero ) { SelectObject( memDc, hOldBitmap ); DeleteObject( hBitmap ); } if ( memDc != IntPtr.Zero ) { DeleteDC( memDc ); } } }
static extern bool AlphaBlend(IntPtr hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, BLENDFUNCTION blendFunction);
protected void SetLayeredWindow(Bitmap srcBitmap) { const byte AC_SRC_OVER = 0; const byte AC_SRC_ALPHA = 1; const int ULW_ALPHA = 2; using (var graphicsScreen = Graphics.FromHwnd(IntPtr.Zero)) using (var graphicsBitmap = Graphics.FromImage(srcBitmap)) { var hdcScreen = graphicsScreen.GetHdc(); var hdcBitmap = graphicsBitmap.GetHdc(); var old = Win32API.SelectObject(hdcBitmap, srcBitmap.GetHbitmap(Color.FromArgb(0))); var blend = new BLENDFUNCTION { BlendOp = AC_SRC_OVER, BlendFlags = 0, SourceConstantAlpha = 255, AlphaFormat = AC_SRC_ALPHA }; var point = new POINT { x = Left, y = Top }; var surfaceSize = new Size(Width, Height); var surfacePoint = new POINT(); Win32API.UpdateLayeredWindow(Handle, hdcScreen, ref point, ref surfaceSize, hdcBitmap, ref surfacePoint, 0, ref blend, ULW_ALPHA); Win32API.DeleteObject(Win32API.SelectObject(hdcBitmap, old)); graphicsScreen.ReleaseHdc(hdcScreen); graphicsBitmap.ReleaseHdc(hdcBitmap); } }
public void UpdateLayeredWindow(byte opacity) { Point pointSource = new Point(0, 0); Point topPos = new Point(Left, Top); var blend = new BLENDFUNCTION(); blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = opacity; blend.AlphaFormat = AC_SRC_ALPHA; UpdateLayeredWindow(Handle, _screenDc, ref topPos, ref _bitmapSize, _compatibleDc, ref pointSource, 0, ref blend, ULW_ALPHA); }
internal static extern bool UpdateLayeredWindow(IntPtr hwnd, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pprSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);
public extern static bool UpdateLayeredWindow(IntPtr handle, IntPtr hdcDst, ref POINT pptDst, ref SIZE psize, IntPtr hdcSrc, ref POINT pprSrc, int crKey, ref BLENDFUNCTION pblend, int dwFlags);
private static extern int UpdateLayeredWindow(IntPtr hw, IntPtr hdcDst, ref Point pDst, ref Size s, IntPtr hdcSrc, ref Point pSrc, int c, ref BLENDFUNCTION b, int f);
public AlphaStretch(IntPtr sourceDc, IntPtr destDc, Rectangle src, Rectangle dest, int depth, byte opacity) { BLENDFUNCTION bf = new BLENDFUNCTION(AC_SRC_OVER, 0x0, opacity, 0x0); // left AlphaBlend(destDc, dest.Left, dest.Top, depth, dest.Height, sourceDc, src.Left, 0, depth, src.Height, bf); // right AlphaBlend(destDc, dest.Right - depth, dest.Top, depth, dest.Height, sourceDc, src.Right - depth, 0, depth, src.Height, bf); // top AlphaBlend(destDc, dest.Left + depth, dest.Top, dest.Width - (2 * depth), depth, sourceDc, src.Left + depth, 0, src.Width - (2 * depth), depth, bf); // bottom AlphaBlend(destDc, dest.Left + depth, dest.Bottom - depth, dest.Width - (2 * depth), depth, sourceDc, src.Left + depth, src.Bottom - depth, src.Width - (2 * depth), depth, bf); // center AlphaBlend(destDc, dest.Left + depth, dest.Top + depth, dest.Width - (2 * depth), dest.Height - (2 * depth), sourceDc, src.Left + depth, depth, src.Width - (2 * depth), src.Height - (2 * depth), bf); }
/// <summary>Causes the control to redraw the invalidated regions within its client area.</summary> new public void Update() { if (Layered && !DesignMode) { //Send the buffer to the o/s SIZE size = new SIZE(Width, Height); POINT pointSource = new POINT(0, 0); POINT topPos = new POINT(Left, Top); BLENDFUNCTION blend = new BLENDFUNCTION(); blend.BlendOp = AC_SRC_OVER; blend.BlendFlags = 0; blend.SourceConstantAlpha = 255; blend.AlphaFormat = AC_SRC_ALPHA; UpdateLayeredWindow(Handle, IntPtr.Zero, ref topPos, ref size, hBufferDC, ref pointSource, 0, ref blend, ULW_ALPHA); } else { base.Update(); } }
public virtual void UpdateWindow(Nub nub, Bitmap buffer) { if (buffer == null || buffer.PixelFormat == 0) { return; } IntPtr dcMemory = default(IntPtr); dcMemory = CreateCompatibleDC(IntPtr.Zero); // calculate the new window position/size based on the bitmap size POINTAPI ptWindowScreenPosition = default(POINTAPI); ptWindowScreenPosition.x = nub.Left; ptWindowScreenPosition.y = nub.Top; ST_SIZE szWindow = default(ST_SIZE); szWindow.hSize = nub.Height; szWindow.vSize = nub.Width; // setup the blend function BLENDFUNCTION blendPixelFunction = new BLENDFUNCTION() { BlendOp = AC_SRC_OVER, BlendFlags = 0, SourceConstantAlpha = 255, AlphaFormat = AC_SRC_ALPHA }; POINTAPI ptSrc = default(POINTAPI); // start point of the copy from dcMemory to dcScreen ptSrc.x = 0; ptSrc.y = 0; //This is where anything needs to get drawn to be shown or updated on the window using (Graphics g = Graphics.FromImage(buffer)) { Draw(nub, g); } // perform the alpha blend IntPtr useBmp = IntPtr.Zero; useBmp = buffer.GetHbitmap(Color.FromArgb(0)); IntPtr bmpOld = IntPtr.Zero; bmpOld = SelectObject(dcMemory, useBmp); try { UpdateLayeredWindow( nub.Handle, IntPtr.Zero, ref ptWindowScreenPosition, ref szWindow, dcMemory, ref ptSrc, 0, ref blendPixelFunction, ULW_ALPHA ); } catch (System.ObjectDisposedException) { } catch (Exception) { } SelectObject(dcMemory, bmpOld); DeleteObject(useBmp); DeleteObject(bmpOld); ReleaseDC(IntPtr.Zero, dcMemory); DeleteDC(dcMemory); dcMemory = IntPtr.Zero; bmpOld = IntPtr.Zero; useBmp = IntPtr.Zero; }
public virtual bool AlphaBlend(int x, int y, int width, int height, IntPtr srchDC, int srcX, int srcY, int srcWidth, int srcHeight, byte alpha) { BLENDFUNCTION blender = new BLENDFUNCTION(GDI32.AC_SRC_OVER, 0, alpha, 0); bool result = GDI32.AlphaBlend(this.HDC, x, y, width, height, srchDC, srcX, srcY, srcWidth, srcHeight, blender); return result; }
public static extern bool AlphaBlend(SafeDCHandle hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, SafeDCHandle hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, BLENDFUNCTION blendFunction);
public static extern Bool UpdateLayeredWindow(IntPtr hWind, IntPtr hdcDat, ref Point pptDat, ref Size psize, IntPtr hdcSrc, ref Point pprSrc, Int32 crKey, ref BLENDFUNCTION pblend, Int32 dwFlags);
/// <summary> /// Creates a handle to a PARGB32 bitmap from an Icon. /// </summary> /// <param name="iconHandle">The handle to the icon.</param> /// <param name="iconSize">The iconSize of the icon.</param> /// <returns>A PARGB32 bitmap with the contents of the icon, including transparency.</returns> public static IntPtr CreatePARGB32HBitmap(IntPtr iconHandle, Size iconSize) { // Make sure we're ready for buffered painting. // TODO: Really we only need to do this once per thread, so an improvement // might be to have a manager create this as required per thread. Uxtheme.BufferedPaintInit(); // Create a compatible device context to work with. var deviceContextHandle = Gdi32.CreateCompatibleDC(IntPtr.Zero); if (deviceContextHandle == IntPtr.Zero) { return(IntPtr.Zero); } // Now create a 32 bit bitmap of the appropriate size, getting it's bits and handle. IntPtr bits; IntPtr hBitmap; if (!Create32BitHBITMAP(deviceContextHandle, iconSize, out bits, out hBitmap)) { Gdi32.DeleteDC(deviceContextHandle); return(IntPtr.Zero); } // Select the bitmap, keeping track of the old one. If this fails, // delete the device context and return a null handle. var oldBitmapHandle = Gdi32.SelectObject(deviceContextHandle, hBitmap); if (oldBitmapHandle == IntPtr.Zero) { Gdi32.DeleteDC(deviceContextHandle); return(IntPtr.Zero); } // Create paint params that represent our alpha blending. var bfAlpha = new BLENDFUNCTION { BlendOp = AC_SRC_OVER, BlendFlags = 0, SourceConstantAlpha = 255, AlphaFormat = AC_SRC_ALPHA }; var paintParams = new BP_PAINTPARAMS(); paintParams.cbSize = (uint)Marshal.SizeOf(paintParams); paintParams.dwFlags = BPPF_ERASE; paintParams.pBlendFunction = Marshal.AllocHGlobal(Marshal.SizeOf(bfAlpha)); Marshal.StructureToPtr(bfAlpha, paintParams.pBlendFunction, false); // Create the pointer that'll hold the device context to the buffer, set the icon rectangle. IntPtr bufferDeviceContextHandle; var iconRect = new RECT(0, 0, iconSize.Width, iconSize.Height); // Create a paint buffer handle. var paintBufferHandle = Uxtheme.BeginBufferedPaint(deviceContextHandle, ref iconRect, BP_BUFFERFORMAT.BPBF_DIB, ref paintParams, out bufferDeviceContextHandle); // Free the memory we allocated for the blend function. Marshal.FreeHGlobal(paintParams.pBlendFunction); // If we created a paint buffer successfully, we can draw the icon into it. if (paintBufferHandle != IntPtr.Zero) { // Try and draw the icon. if (Gdi32.DrawIconEx(bufferDeviceContextHandle, 0, 0, iconHandle, iconSize.Width, iconSize.Height, 0, IntPtr.Zero, (int)DI_NORMAL)) { // Now convert the buffer we've painted into PARGB32, meaning we'll end up // with a PARGB32 bitmap. ConvertBufferToPARGB32(paintBufferHandle, deviceContextHandle, iconHandle, iconSize); } // This will write the buffer contents to the destination bitmap. Uxtheme.EndBufferedPaint(paintBufferHandle, true); } // Select the old bitmpa and delete the device context. Gdi32.SelectObject(deviceContextHandle, oldBitmapHandle); Gdi32.DeleteDC(deviceContextHandle); // We're done with buffered painting. Uxtheme.BufferedPaintUnInit(); // Baddabing-baddabom, PARGB32. return(hBitmap); }