Other Posts in Image Editing

  1. Perlin Noise
  2. Fault Formation
  3. Cellular Textures
  4. Resizing an Image in C#
  5. Box Blur and Gaussian Blur... Sort of...
  6. Thermal Erosion
  7. Using Mid Point Displacement to Create Cracks
  8. Fluvial Erosion
  9. Creating Marble Like Textures Procedurally
  10. Procedural Textures and Dilation
  11. Converting Image to Black and White in C#
  12. Getting an HTML Based Color Palette from an Image in C#
  13. Adding Noise/Jitter to an Image in C#
  14. Creating Pixelated Images in C#
  15. Edge detection in C#
  16. Using Sin to Get What You Want... In C#...
  17. Noise Reduction of an Image in C# using Median Filters
  18. Image Dilation in C#
  19. Sepia Tone in C#
  20. Kuwahara Filter in C#
  21. Matrix Convolution Filters in C#
  22. Symmetric Nearest Neighbor in C#
  23. Bump Map Creation Using C#
  24. Normal Map Creation Using C#
  25. Creating Negative Images using C#
  26. Red, Blue, and Green Filters in C#
  27. Converting an Image to ASCII Art in C#
  28. Adjusting Brightness of an Image in C#
  29. Adding Noise to an Image in C#
  30. Adjusting the Gamma of an Image Using C#
  31. Adjusting Contrast of an Image in C#
  32. Drawing a Box With Rounded Corners in C#
  33. Anding Two Images Together Using C#
  34. Motion Detection in C#
  35. Creating Thermometer Chart in C#
  36. Colorizing a Black and White Image in C#
  37. Extracting an Icon From a File
  38. Setting the Pixel Format and Image Format of an Image in .Net
  39. Using Unsafe Code for Faster Image Manipulation
  40. Sobel Edge Detection and Laplace Edge Detection in C#

Getting an HTML Based Color Palette from an Image in C#

12/19/2008

I'll be honest and say that I don't know if this is going to be that useful to anyone out there, but hey, it's free code. Anyway, I recently ran into a situation where I needed to get the palette that was used by an image, take that palette and convert it to HTML based colors (#012345, etc.). It's actually very simple but unless you know about the ColorTranslator class, you'd be scratching your head as to how to do it in a simple manner. Luckily for us that class exists and contains a function called ToHTML which converts a color object to an HTML based string. For our purposes, we're just going to do the following:

   1: /// <summary>
   2: /// Gets a palette listing in HTML string format
   3: /// </summary>
   4: /// <param name="OriginalImage">Image to get the palette of</param>
   5: /// <returns>A list containing HTML color values (ex: #041845)</returns>
   6: public static List<string> GetHTMLPalette(Bitmap OriginalImage)
   7: {
   8:     List<string> ReturnArray = new List<string>();
   9:     if (OriginalImage.Palette != null && OriginalImage.Palette.Entries.Length > 0)
  10:     {
  11:         for (int x = 0; x < OriginalImage.Palette.Entries.Length; ++x)
  12:         {
  13:             string TempColor = ColorTranslator.ToHtml(OriginalImage.Palette.Entries[x]);
  14:             if (!ReturnArray.Contains(TempColor))
  15:             {
  16:                 ReturnArray.Add(TempColor);
  17:             }
  18:         }
  19:         return ReturnArray;
  20:     }
  21:     BitmapData ImageData = Image.LockImage(OriginalImage);
  22:     int PixelSize = Image.GetPixelSize(ImageData);
  23:     for (int x = 0; x < OriginalImage.Width; ++x)
  24:     {
  25:         for (int y = 0; y < OriginalImage.Height; ++y)
  26:         {
  27:             string TempColor = ColorTranslator.ToHtml(Image.GetPixel(ImageData, x, y, PixelSize));
  28:             if (!ReturnArray.Contains(TempColor))
  29:             {
  30:                 ReturnArray.Add(TempColor);
  31:             }
  32:         }
  33:     }
  34:     Image.UnlockImage(OriginalImage, ImageData);
  35:     return ReturnArray;
  36: }

That's it. All the code does, is checks if there is a palette. If there is, it simply copies those values (once converted) to the return array. Otherwise it goes through each pixel and converts it that way, building up the array as it goes. Very simple and gets the job done. Once again, I don't know if this code is going to help anyone out there, but you never know. Anyway, try it out, leave feedback, and happy coding.



Comments