CSharp examples for System.Drawing:Bitmap
Swaps the alpha and value channels of a monochrome image. Transparent areas become black, and black areas become transparent.
using System.Windows.Forms; using System.Text; using System.Linq; using System.Drawing; using System.Collections.Generic; using System;// w ww .j a va 2s .co m public class Main{ /// <summary> /// Swaps the alpha and value channels of a monochrome image. Transparent areas become black, and black areas become transparent. /// </summary> public static Bitmap AlphaSwap(Bitmap source) { Color c; if (IsSolidColor(source, out c) && c.A == 0) { // fully transparent image return source; } Bitmap ret = new Bitmap(source.Width, source.Height); for (int x = 0; x < ret.Width; x++) { for (int y = 0; y < ret.Height; y++) { Color fromColor = source.GetPixel(x, y); int toColor = fromColor.A == 0 ? -0x01000000 : fromColor.A * 0x10101 + fromColor.R * 0x1000000; ret.SetPixel(x, y, Color.FromArgb(toColor)); } } return ret; } /// <summary> /// Checks if the given bitmap is a single color. If the result is true, its color will be stored in the second parameter. /// </summary> public static bool IsSolidColor(Bitmap bmp, out Color c) { c = bmp.GetPixel(0, 0); for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { if (bmp.GetPixel(x, y) != c) { return false; } } } return true; } }