Click here to Skip to main content
15,885,914 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 
GeneralRe: My vote of 1 Pin
charles henington12-Apr-23 10:15
charles henington12-Apr-23 10:15 
You are correct that there's not a lot of documentation on this subject. I think one might be skeptical using code that's "unsafe", thinking that the unsafe keyword itself might be malicious. The reason the unsafe memory pointers are faster is because they directly access the memory address, where the Get Pixel/Set Pixel is extremely slow because it Marshals(System.Runtime.InteropServices.Marshal.Copy) each call to the memory address which results in very slow performance and eventually runs out of memory, especially with larger Bitmaps.
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.