Sprache: C#
Dieses Snippet erstellt einen "Farbverlauf" und setzt ihn als BackgroundImage für ein Label.
Das ganze ist eher unsauber (-> nicht gleichmäßig), funktioniert aber flott und reicht locker für kleine Labels etc.
[code]myLabel.GradientBackground(Color.Blue, Color.DarkBlue, true);[/code]
erzeugt einen Vertikalen (von Oben nach unten) Farbverlauf. Oben col1, unten col2. Beim horizontalen ist links col1 und rechts col2!
public static class ExtensionMethods
{
public static void GradientBackground(this Control c, Color col1, Color col2, bool vertical)
{
Bitmap b;
if (vertical)
{
b = new Bitmap(1, 2);
b.SetPixel(0, 0, col1);
b.SetPixel(0, 1, col2);
}
else
{
b = new Bitmap(2, 1);
b.SetPixel(0, 0, col1);
b.SetPixel(1, 0, col2);
}
c.BackgroundImageLayout = ImageLayout.Stretch;
c.BackgroundImage = b;
}
}
public static class ExtensionMethods
{
public static void GradientBackground(this Control c, Color col1, Color col2, bool vertical)
{
Bitmap b;
if (vertical)
{
b = new Bitmap(1, 2);
b.SetPixel(0, 0, col1);
b.SetPixel(0, 1, col2);
}
else
{
b = new Bitmap(2, 1);
b.SetPixel(0, 0, col1);
b.SetPixel(1, 0, col2);
}
c.BackgroundImageLayout = ImageLayout.Stretch;
c.BackgroundImage = b;
}
}
Alte URL:
/snippet/einfacher-farbverlauf-fuer-steuerelemente-labels/1718
Hallo, gucke dir mal folgende Klasse an:
http://msdn.microsoft.com/de-de/library/system.drawing.drawing2d.lineargradientbrush.aspx
Die bekommt die Farbverläufe bei großen Flächen auch sauber hin.
Aye, aye sir. ;D
Danke für den Hinweis.