Search code examples
c#winformsimagesystem.drawingpixelformat

A nicer PixelFormat.ToString output


The PixelFormat enumeration from the System.Drawing.Imaging namespace has members like Format32bppArgb or Format8bppIndexed, that are displayed when calling its ToString method.

Do you know of a built-in or other method that converts PixelFormat values to strings that are more appropriate for display in a graphic program, like 24 BPP or 24bpp?


Solution

  • Try something like this extension method I quickly threw together:

    public static string ToFancyString(this PixelFormat format)
    {
        switch (format)
        {
            case PixelFormat.Format16bppArgb1555:
                return "16bpp ARGB";
            case PixelFormat.Format16bppGrayScale:
                return "16bpp Gray";
            case PixelFormat.Format16bppRgb555:
                return "16bpp RGB";
            case PixelFormat.Format16bppRgb565:
                return "16bpp RGB";
            case PixelFormat.Format1bppIndexed:
                return "1bpp Indexed";
            case PixelFormat.Format24bppRgb:
                return "24bpp RGB";
            case PixelFormat.Format32bppArgb:
                return "32bpp ARGB";
            case PixelFormat.Format32bppPArgb:
                return "32bpp Premultiplied ARGB";
            case PixelFormat.Format32bppRgb:
                return "32bpp RGB";
            case PixelFormat.Format48bppRgb:
                return "48bpp RGB";
            case PixelFormat.Format4bppIndexed:
                return "4bpp Indexed";
            case PixelFormat.Format64bppArgb:
                return "64bpp ARGB";
            case PixelFormat.Format64bppPArgb:
                return "64bpp Premultiplied ARGB";
            case PixelFormat.Format8bppIndexed:
                return "8bpp Indexed";
            default:
                return format.ToString();
        }
    }