0

Using C# I was trying to devide a photo in more than Photo. in Each Photo i have a piece colour. The way I am doing it have a problem and need your kind advice. the Problem is my Photo name will have an 8 ARGB Values! but i want just 6 values! i want the filename to take into account the color RGB value without the alpha.

public class PNG2PNGS
{
    public static void Convert(string sourcePath)
    {
        foreach (var fileName in Directory.GetFiles(sourcePath, "*.png"))
        {
            Bitmap bitmap = new Bitmap(fileName);
            List<Color> colors = new List<Color>();

            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    Color color = bitmap.GetPixel(i, j);
                    if (!colors.Contains(color))
                    {
                        colors.Add(color);
                    }
                }
            }

            foreach (Color color in colors)
            {
                Bitmap bitmap2 = new Bitmap(bitmap.Width, bitmap.Height, bitmap.PixelFormat);
                for (int i = 0; i < bitmap.Width; i++)
                {
                    for (int j = 0; j < bitmap.Height; j++)
                    {
                        if (bitmap.GetPixel(i, j) == color)
                        {
                            bitmap2.SetPixel(i, j, color);
                        }
                        else
                        {
                            bitmap2.SetPixel(i, j, Color.Transparent);
                        }
                    }
                }
                
                bitmap2.Save(Path.Combine(Path.GetDirectoryName(fileName), $"{Path.GetFileNameWithoutExtension(fileName)}_{color.ToArgb().ToString("X6")}.png"), ImageFormat.Png);
            }

            bitmap.GetPixel(0,0);
        }
    }
}
Sam
  • 31
  • 5
  • Does this answer your question? [How do I get the color from a hexadecimal color code using .NET?](https://stackoverflow.com/questions/2109756/how-do-i-get-the-color-from-a-hexadecimal-color-code-using-net) – jason.kaisersmith Jan 25 '23 at 12:00
  • unfortunately No :( – Sam Jan 25 '23 at 12:07
  • I don't really understand the question, you have an argb value which is for example: (255, 0, 0, 100) for a blue color. and instead of it turning into #ff000064 you want #000064 Just leave out the first 2 characters which is the alpha channel. – GHOST Jan 25 '23 at 12:18
  • yeah, how to have it with out alpha? – Sam Jan 25 '23 at 12:20
  • @Sam if you want to improve your question (also in the future) you should reduce the code to a [mre]. Most of the code is irrelevant for the actual problem you asked about. – wohlstad Jan 25 '23 at 12:31
  • @Sam you just have to not give it the alpha channel, or if you should set the alpha channel to "255" which is "ff" in hex – GHOST Jan 25 '23 at 12:39

1 Answers1

0

If I understood correctly, you want the filename to take into account the color RGB value without the alpha.

You can use a temporary that will be exactly that - the color with 0 for the alpha channel.

Then use your print format specifier as you already did:

Color color = Color.FromArgb(0x0A, 0x0B, 0x0C, 0x0D);
//...
Color colorNoAlpha = Color.FromArgb(0, color.R, color.G, color.B);
Console.WriteLine(colorNoAlpha.ToArgb().ToString("X6"));

Output:

0B0C0D

(In your code, add the line initializing colorNoAlpha before saving the file, and in the file save statement replace color with colorNoAlpha).

wohlstad
  • 12,661
  • 10
  • 26
  • 39