Skip to content

Instantly share code, notes, and snippets.

@dotCoefficient
Last active December 16, 2016 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotCoefficient/ed1efe53bc3234fdc109b87fdee5d483 to your computer and use it in GitHub Desktop.
Save dotCoefficient/ed1efe53bc3234fdc109b87fdee5d483 to your computer and use it in GitHub Desktop.
Will change the overall transparency of an image.
using System.Drawing;
public static class GraphicHelpers
{
/// <summary>
/// Changes the overall transparency of an image
/// </summary>
/// <param name="originalImage">The input image to be tuned</param>
/// <param name="alpha">the desired alpha value. It is based on the 255 alpha pixels</param>
/// <param name="antialias">To conserve the antialiasing on the image</param>
/// <returns></returns>
public static Image TuneTransparency(Image originalImage, int alpha, bool antialias)
{
//Cast & clone image to bitmap
Bitmap bitmap = new Bitmap(originalImage);
//Cycle through all pixels
Color pixel;
//ratio between a max byte and desired alpha byte, this achieves the conservation of antialiasing
int ratio = 255 / alpha;
for (int i = 0; i < originalImage.Width; i++)
{
for (int j = 0; j < originalImage.Height; j++)
{
//Get pixel color
pixel = bitmap.GetPixel(i, j);
//If pixel is not already transparent
if (pixel.A != 0)
{
//Set the pixel to
bitmap.SetPixel(i, j, Color.FromArgb(
//If antialiasing is enabled the: pixel alpha/ratio else just put the alpha
antialias ? pixel.A / ratio : alpha,
pixel));
}
}
}
return bitmap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment