Files
docs-desktop/dotnet-desktop-guide/framework/winforms/advanced/how-to-draw-opaque-and-semitransparent-lines.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

2.7 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Draw Opaque and Semitransparent Lines 03/30/2017
csharp
vb
drawing [Windows Forms], lines
transparency [Windows Forms], lines
lines [Windows Forms], drawing alpha blended
alpha blending [Windows Forms], drawing lines
8f2508af-f495-4223-b5cc-646cbbb520eb

How to: Draw Opaque and Semitransparent Lines

When you draw a line, you must pass a xref:System.Drawing.Pen object to the xref:System.Drawing.Graphics.DrawLine%2A method of the xref:System.Drawing.Graphics class. One of the parameters of the xref:System.Drawing.Pen.%23ctor%2A constructor is a xref:System.Drawing.Color object. To draw an opaque line, set the alpha component of the color to 255. To draw a semitransparent line, set the alpha component to any value from 1 through 254.

When you draw a semitransparent line over a background, the color of the line is blended with the colors of the background. The alpha component specifies how the line and background colors are mixed; alpha values near 0 place more weight on the background colors, and alpha values near 255 place more weight on the line color.

Example

The following example draws a bitmap and then draws three lines that use the bitmap as a background. The first line uses an alpha component of 255, so it is opaque. The second and third lines use an alpha component of 128, so they are semitransparent; you can see the background image through the lines. The statement that sets the xref:System.Drawing.Graphics.CompositingQuality%2A property causes the blending for the third line to be done in conjunction with gamma correction.

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

The following illustration shows the output of the following code:

Illustration that shows opaque and semitransparent output

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 the xref:System.Windows.Forms.Control.Paint event handler.

See also