Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#
Tip/Trick

Very simple conversion of bitmap to indexed bitmap

Rate me:
Please Sign up or sign in to vote.
2.47/5 (7 votes)
9 Apr 2023CPOL 7.6K   2   22
Very simple conversion of bitmap to indexed bitmap
C#
private static unsafe Bitmap ToIndexed(Bitmap source, PixelFormat format)
        {
            Rectangle rectangle = new Rectangle(0, 0, source.Width, source.Height);
            Bitmap result = new Bitmap(source.Width, source.Height, format);

            BitmapData sourceData = source.LockBits(rectangle, 
                ImageLockMode.ReadOnly, source.PixelFormat),
                resultData = result.LockBits(rectangle, 
                ImageLockMode.WriteOnly, format);

            int sourceFlags = sourceData.Stride * sourceData.Height,
                resultFlags = resultData.Stride * resultData.Height;

            byte* sourceBuffer = (byte*)sourceData.Scan0.ToPointer(), 
                resultBuffer = (byte*)resultData.Scan0.ToPointer();

            int offset = sourceFlags / resultFlags;
            for (int i = 0; i < sourceFlags; i++)
            {
                resultBuffer[i / offset] = sourceBuffer[i];
            }

            result.UnlockBits(resultData);
            source.UnlockBits(sourceData);
            return result;
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I do not claim to be wrong! I just rarely ever write.

Comments and Discussions

 
GeneralMy vote of 1 Pin
Stacy Dudovitz11-Apr-23 18:37
professionalStacy Dudovitz11-Apr-23 18:37 
GeneralRe: My vote of 1 Pin
Lucky Vdb11-Apr-23 20:40
professionalLucky Vdb11-Apr-23 20:40 
I understand why this looks like no good code, conform to today's architectural rules.
Yes, this is only bare metal, the base necessity.
But for me, just what it should be, when tagged with a label "tip/trick".
Code like this is just made for speed, and saving resources like intermediate memory streams or files.
It just brings together some not well documented stuff.
Even the used unsafe memory pointers just showed how simple it is doing high speed parsing over the bits in the bitmap.

Sometimes there are big commented source projects, complete with test classes and over documented. But then I have to strip down to what I need.
This is only a building block, which can be decorated any way.

So in its own category, it is a very useable tip/trick.
GeneralRe: My vote of 1 Pin
charles henington12-Apr-23 10:15
charles henington12-Apr-23 10:15 
GeneralRe: My vote of 1 Pin
charles henington12-Apr-23 16:22
charles henington12-Apr-23 16:22 
GeneralRe: My vote of 1 Pin
Stacy Dudovitz12-Apr-23 20:30
professionalStacy Dudovitz12-Apr-23 20:30 
GeneralRe: My vote of 1 Pin
charles henington13-Apr-23 9:55
charles henington13-Apr-23 9:55 
GeneralRe: My vote of 1 Pin
charles henington13-Apr-23 11:20
charles henington13-Apr-23 11:20 
GeneralRe: My vote of 1 Pin
Stacy Dudovitz14-Apr-23 7:38
professionalStacy Dudovitz14-Apr-23 7:38 
GeneralMy vote of 2 Pin
dmjm-h11-Apr-23 12:01
dmjm-h11-Apr-23 12:01 
GeneralRe: My vote of 2 Pin
charles henington11-Apr-23 16:36
charles henington11-Apr-23 16:36 
GeneralRe: My vote of 2 Pin
charles henington11-Apr-23 16:55
charles henington11-Apr-23 16:55 
GeneralMy vote of 5 Pin
Lucky Vdb11-Apr-23 1:35
professionalLucky Vdb11-Apr-23 1:35 
GeneralRe: My vote of 5 Pin
charles henington11-Apr-23 11:17
charles henington11-Apr-23 11:17 
GeneralMy vote of 2 Pin
YDaoust10-Apr-23 23:20
YDaoust10-Apr-23 23:20 
GeneralRe: My vote of 2 Pin
charles henington11-Apr-23 11:14
charles henington11-Apr-23 11:14 
GeneralRe: My vote of 2 Pin
YDaoust11-Apr-23 20:17
YDaoust11-Apr-23 20:17 
GeneralRe: My vote of 2 Pin
charles henington12-Apr-23 3:36
charles henington12-Apr-23 3:36 
GeneralRe: My vote of 2 Pin
YDaoust12-Apr-23 4:36
YDaoust12-Apr-23 4:36 
GeneralRe: My vote of 2 Pin
charles henington12-Apr-23 9:55
charles henington12-Apr-23 9:55 
GeneralRe: My vote of 2 Pin
YDaoust12-Apr-23 10:27
YDaoust12-Apr-23 10:27 
GeneralRe: My vote of 2 Pin
charles henington12-Apr-23 10:37
charles henington12-Apr-23 10:37 
GeneralRe: My vote of 2 Pin
charles henington12-Apr-23 15:07
charles henington12-Apr-23 15:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.