Reducing color depth of an image in c#
-
Is there a way to reduce the color depth (in bits) of an image (whether it is saved or in a Bitmap form) in c#? If possible, please include a sample code.
There might be a simpler way, but this way works:
//assume the original bitmap is named bmp, and the
//new pixel format flag is in a variable named format.
Bitmap newbmp=new Bitmap(format,bmp.Width,bm.Height);
Graphics newbmpgr=Graphics.FromImage((Image)newbmp);
newbmpgr.DrawImageUnscaled((Image)bmp,0,0);
newbmpgr.Dispose();
//newbmp now contains old bitmap's data,
//but in the new format.Remember that if you convert the image from another format into 8-bit indexed color, you will have to set the palette entries yourself before doing the DrawImageUnscaled() call.
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi -
There might be a simpler way, but this way works:
//assume the original bitmap is named bmp, and the
//new pixel format flag is in a variable named format.
Bitmap newbmp=new Bitmap(format,bmp.Width,bm.Height);
Graphics newbmpgr=Graphics.FromImage((Image)newbmp);
newbmpgr.DrawImageUnscaled((Image)bmp,0,0);
newbmpgr.Dispose();
//newbmp now contains old bitmap's data,
//but in the new format.Remember that if you convert the image from another format into 8-bit indexed color, you will have to set the palette entries yourself before doing the DrawImageUnscaled() call.
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi -
I was trying to convert a 1024x768 image and i got an out of memory error (i have around 200mb free out of 512mb so that's not possible). Is there a workaround?
CyberKewl wrote: I was trying to convert a 1024x768 image and i got an out of memory error :rolleyes: :confused: :~ I don't know why that happens. CyberKewl wrote: Is there a workaround? Not that I know of - any way you do it, you're going to have to create 2 different bitmaps. But it really should work. What's your OS?
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi -
CyberKewl wrote: I was trying to convert a 1024x768 image and i got an out of memory error :rolleyes: :confused: :~ I don't know why that happens. CyberKewl wrote: Is there a workaround? Not that I know of - any way you do it, you're going to have to create 2 different bitmaps. But it really should work. What's your OS?
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi -
Ok i've just tried again but changed the pixelformat to PixelFormat.Format24bppRgb but the filesize and the color depth did not change at all (i saved newbmp as png).
-
So what's your starting pixel format and what's your new pixel format?
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi -
Ok i've just tried again but changed the pixelformat to PixelFormat.Format24bppRgb but the filesize and the color depth did not change at all (i saved newbmp as png).
Internally, GDI+ represents all bitmap/drawing data as 32bpp. As far as I am aware, none of the standard Microsoft image codecs (bmp,jpg,png,etc...) will do any sort of color conversion for you. -- Russell Morris "So, broccoli, mother says you're good for me... but I'm afraid I'm no good for you!" - Stewy