Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Create or Save bitmap PixelFormat.Format8bppIndexed

Bitmap bitmap = new Bitmap(100, 100, PixelFormat.Format8bppIndexed);
panel1.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, 100, 100));
bitmap.Save(@"c:\\test.bmp", ImageFormat.Bmp);


System.Exception: 'A Graphics object cannot be created from an image that has an indexed pixel format.'

What I have tried:

C#
Bitmap bitmap = new Bitmap(100, 100, PixelFormat.Format8bppIndexed);
panel1.DrawToBitmap(bitmap, new System.Drawing.Rectangle(0, 0, 100, 100));
bitmap.Save(@"c:\\test.bmp", ImageFormat.Bmp);
Posted
Updated 5-Dec-22 6:43am

Look at the error message, it is clearly telling you that you cannot use PixelFormat.Format8bppIndexed. So choose an alternate format from PixelFormat Enum (System.Drawing.Imaging) | Microsoft Learn[^].
 
Share this answer
 
Comments
BahramBahramy 5-Dec-22 10:46am    
Well done, I need to save the image in this format, this is a bug in C#
PixelFormat.Format8bppIndexed
Richard MacCutchan 5-Dec-22 10:49am    
How do you know this is a C# bug?
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900