Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6210359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:08:43+00:00 2026-05-24T06:08:43+00:00

I need to be able to convert 8-bit or 16-bit grayscale pixel data into

  • 0

I need to be able to convert 8-bit or 16-bit grayscale pixel data into a file format that the .NET framework can support.

The data I have available is the width, height, orientation (bottom-left) and the pixel format as 4096 shades of gray (12-bit resolution) packed in 2 bytes per pixel.

So for example each pixel ranges from 0 to 4096, and each pixel is 2 bytes.

I have already tried using PixelFormat.Format16bppGrayScale with the Bitmap constructor, and it throws a GDI+ exception. Everything I have read says that this format is not supported and that MSDN is wrong.

I want to convert this pixel buffer into a .NET Bitmap format (such as Format32bppArgb) with as little image quality loss as possible.

Anyone know how?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:08:44+00:00Added an answer on May 24, 2026 at 6:08 am

    See the example below, which precomputes a lookup table (LUT) and uses that to convert each pixel. This version covers your 12-bit case; for 8-bit the code is very similar, but it is difficult to generalize across pixel formats.

    A conversion from 12-bit GS to effectively 8-bit GS will lose data. However, you can adjust the LUT table to focus on a smaller range of input values with better contrast (ex. DICOM Window Center/Window Width).

    class Program
    {
        static void Main( string[] args )
        {
            // Test driver - create a Wedge, convert to Bitmap, save to file
            //
            int width = 4095;
            int height = 1200;
            int bits = 12;
    
            byte[] wedge = Wedge( width, height, bits );
    
            Bitmap bmp = Convert( wedge, width, height, bits );
    
            string file = "wedge.png";
    
            bmp.Save( file );
    
            Process.Start( file );
        }
    
        static Bitmap Convert( byte[] input, int width, int height, int bits )
        {
            // Convert byte buffer (2 bytes per pixel) to 32-bit ARGB bitmap
    
            var bitmap = new Bitmap( width, height, PixelFormat.Format32bppArgb );
    
            var rect = new Rectangle( 0, 0, width, height );
    
            var lut = CreateLut( bits );
    
            var bitmap_data = bitmap.LockBits( rect, ImageLockMode.WriteOnly, bitmap.PixelFormat );
    
            ConvertCore( width, height, bits, input, bitmap_data, lut );
    
            bitmap.UnlockBits( bitmap_data );
    
            return bitmap;
        }
    
        static unsafe void ConvertCore( int width, int height, int bits, byte[] input, BitmapData output, uint[] lut )
        {
            // Copy pixels from input to output, applying LUT
    
            ushort mask = (ushort)( ( 1 << bits ) - 1 );
    
            int in_stride = output.Stride;
            int out_stride = width * 2;
    
            byte* out_data = (byte*)output.Scan0;
    
            fixed ( byte* in_data = input )
            {
                for ( int y = 0; y < height; y++ )
                {
                    uint* out_row = (uint*)( out_data + ( y * in_stride ) );
    
                    ushort* in_row = (ushort*)( in_data + ( y * out_stride ) );
    
                    for ( int x = 0; x < width; x++ )
                    {
                        ushort in_pixel = (ushort)( in_row[ x ] & mask );
    
                        out_row[ x ] = lut[ in_pixel ];
                    }
                }
            }
        }
    
        static uint[] CreateLut( int bits )
        {
            // Create a linear LUT to convert from grayscale to ARGB
    
            int max_input = 1 << bits;
    
            uint[] lut = new uint[ max_input ];
    
            for ( int i = 0; i < max_input; i++ )
            {
                // map input value to 8-bit range
                //
                byte intensity = (byte)( ( i * 0xFF ) / max_input );
    
                // create ARGB output value A=255, R=G=B=intensity
                //
                lut[ i ] = (uint)( 0xFF000000L | ( intensity * 0x00010101L ) );
            }
    
            return lut;
        }
    
        static byte[] Wedge( int width, int height, int bits )
        {
            // horizontal wedge
    
            int max = 1 << bits;
    
            byte[] pixels = new byte[ width * height * 2 ];
    
            for ( int y = 0; y < height; y++ )
            {
                for ( int x = 0; x < width; x++ )
                {
                    int pixel = x % max;
    
                    int addr = ( ( y * width ) + x ) * 2;
    
                    pixels[ addr + 1 ] = (byte)( ( pixel & 0xFF00 ) >> 8 );
                    pixels[ addr + 0 ] = (byte)( ( pixel & 0x00FF ) );
                }
            }
    
            return pixels;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to convert a string to a hierarchyid in c#.net
I have a 2D array that I need to be able to convert to
Hi I need to be able to convert a ascii character into its decimal
I need to be able to turn a flat xml data sets into html
I need to be able to quickly convert an image (inside a rails controller)
I need to be able to load the entire contents of a text file
I need to be able to GZip compress a file in an Excel VBA
I need to be able to launch a process and read the output into
I need to take a 16 bit .tiff file and make it viewable on
I need to be able to convert all input fields in a DetailsView (Insert)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.