Skip to content

Latest commit

 

History

History
60 lines (39 loc) · 4.01 KB

how-to-use-compositing-mode-to-control-alpha-blending.md

File metadata and controls

60 lines (39 loc) · 4.01 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
How to: Use Compositing Mode to Control Alpha Blending
Learn how to use compositing mode to control alpha blending using a System.Drawing.Bitmap object and a System.Drawing.Graphics object.
03/30/2017
csharp
vb
alpha blending [Windows Forms], compositing
colors [Windows Forms], blending
colors [Windows Forms], controlling transparency
f331df2d-b395-4b0a-95be-24fec8c9bbb5

How to: Use Compositing Mode to Control Alpha Blending

There may be times when you want to create an off-screen bitmap that has the following characteristics:

  • Colors have alpha values that are less than 255.

  • Colors are not alpha blended with each other as you create the bitmap.

  • When you display the finished bitmap, colors in the bitmap are alpha blended with the background colors on the display device.

To create such a bitmap, construct a blank xref:System.Drawing.Bitmap object, and then construct a xref:System.Drawing.Graphics object based on that bitmap. Set the compositing mode of the xref:System.Drawing.Graphics object to xref:System.Drawing.Drawing2D.CompositingMode.SourceCopy?displayProperty=nameWithType.

Example

The following example creates a xref:System.Drawing.Graphics object based on a xref:System.Drawing.Bitmap object. The code uses the xref:System.Drawing.Graphics object along with two semitransparent brushes (alpha = 160) to paint on the bitmap. The code fills a red ellipse and a green ellipse using the semitransparent brushes. The green ellipse overlaps the red ellipse, but the green is not blended with the red because the compositing mode of the xref:System.Drawing.Graphics object is set to xref:System.Drawing.Drawing2D.CompositingMode.SourceCopy.

The code draws the bitmap on the screen twice: once on a white background and once on a multicolored background. The pixels in the bitmap that are part of the two ellipses have an alpha component of 160, so the ellipses are blended with the background colors on the screen.

The following illustration shows the output of the code example. Note that the ellipses are blended with the background, but they are not blended with each other.

Diagram showing ellipses blended with the background, not each other.

The code example contains this statement:

[!code-csharpSystem.Drawing.AlphaBlending#41] [!code-vbSystem.Drawing.AlphaBlending#41]

If you want the ellipses to be blended with each other as well as with the background, change that statement to the following:

[!code-csharpSystem.Drawing.AlphaBlending#42] [!code-vbSystem.Drawing.AlphaBlending#42]

The following illustration shows the output of the revised code.

Diagram that shows ellipses blended together and with background.

[!code-csharpSystem.Drawing.AlphaBlending#43] [!code-vbSystem.Drawing.AlphaBlending#43]

Compiling the Code

The preceding example is designed for use with Windows Forms, and it requires xref:System.Windows.Forms.PaintEventArgs e, which is a parameter of xref:System.Windows.Forms.PaintEventHandler.

See also