---
title: "How to: Use Compositing Mode to Control Alpha Blending"
ms.date: "03/30/2017"
dev_langs: 
  - "csharp"
  - "vb"
helpviewer_keywords: 
  - "alpha blending [Windows Forms], compositing"
  - "colors [Windows Forms], blending"
  - "colors [Windows Forms], controlling transparency"
ms.assetid: 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.](./media/how-to-use-compositing-mode-to-control-alpha-blending/ellipses-blended-background.png)  
  
 The code example contains this statement:  
  
 [!code-csharp[System.Drawing.AlphaBlending#41](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.AlphaBlending/CS/Class1.cs#41)]
 [!code-vb[System.Drawing.AlphaBlending#41](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.AlphaBlending/VB/Class1.vb#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-csharp[System.Drawing.AlphaBlending#42](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.AlphaBlending/CS/Class1.cs#42)]
 [!code-vb[System.Drawing.AlphaBlending#42](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.AlphaBlending/VB/Class1.vb#42)]  
  
 The following illustration shows the output of the revised code.  
  
 ![Diagram that shows ellipses blended together and with background.](./media/how-to-use-compositing-mode-to-control-alpha-blending/blend-ellipses-background.png)  
  
 [!code-csharp[System.Drawing.AlphaBlending#43](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.AlphaBlending/CS/Class1.cs#43)]
 [!code-vb[System.Drawing.AlphaBlending#43](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.AlphaBlending/VB/Class1.vb#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

- <xref:System.Drawing.Color.FromArgb%2A>
- [Alpha Blending Lines and Fills](alpha-blending-lines-and-fills.md)
