Skip to content

Instantly share code, notes, and snippets.

@FremyCompany
Created February 4, 2019 19:57
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 FremyCompany/2ec404a2821ae46ac762bc5331db8ea4 to your computer and use it in GitHub Desktop.
Save FremyCompany/2ec404a2821ae46ac762bc5331db8ea4 to your computer and use it in GitHub Desktop.
Output all colors of an image in a file
# Open a command prompt and type the following lines:
cd C:\type\the\path\to\the\folder\containing\the\cs\file
C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe extract-colors.cs /reference:System.Drawing.dll
extract-colors.exe > result.txt
using System;
using System.Drawing;
using System.Collections.Generic;
public static class Program
{
public static void Main(string[] args)
{
// Create a set to store the colors
var colors = new HashSet<Color>();
// Load the image
var image = new Bitmap(
@"C:\image.png",
true
);
// Loop through the images pixels to get their color.
for(var x=0; x<image.Width; x++)
{
for(var y=0; y<image.Height; y++)
{
var pixelColor = image.GetPixel(x, y);
// add the color to the set of colors if it is not fully transparent
if(pixelColor.A != 0)
{
colors.Add(pixelColor);
}
}
}
// Loop through the colors and display them in the console
foreach(var color in colors)
{
Console.WriteLine("#" + color.R.ToString("x2") + color.G.ToString("x2") + color.B.ToString("x2"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment