BMP files not working with Graphics object?

Posted on 2004-11-24
Medium Priority
Last Modified: 2010-04-15
I have uploaded the problem source code at:


It would be nice if I could get it to work with BMP files instead of having to go back to using JPG.

Whenever I try and run the application I get the following error:

Additional information: A Graphics object cannot be created from an image that has an indexed pixel format.


'DefaultDomain': Loaded 'c:\winnt\microsoft.net\framework\v1.0.3705\mscorlib.dll', No symbols loaded.
'WindowsApplication1': Loaded 'C:\Documents and Settings\tom\Desktop\RobotzBest\bin\Debug\WindowsApplication1.exe', Symbols loaded.
'WindowsApplication1.exe': Loaded 'c:\winnt\assembly\gac\system.windows.forms\1.0.3300.0__b77a5c561934e089\system.windows.forms.dll', No symbols loaded.
'WindowsApplication1.exe': Loaded 'c:\winnt\assembly\gac\system\1.0.3300.0__b77a5c561934e089\system.dll', No symbols loaded.
'WindowsApplication1.exe': Loaded 'c:\winnt\assembly\gac\system.drawing\1.0.3300.0__b03f5f7f11d50a3a\system.drawing.dll', No symbols loaded.
'WindowsApplication1.exe': Loaded 'c:\winnt\assembly\gac\accessibility\1.0.3300.0__b03f5f7f11d50a3a\accessibility.dll', No symbols loaded.
An unhandled exception of type 'System.Exception' occurred in system.drawing.dll

Additional information: A Graphics object cannot be created from an image that has an indexed pixel format.

Unhandled Exception: System.Exception: A Graphics object cannot be created from an image that has an indexed pixel format.
   at System.Drawing.Graphics.FromImage(Image image)
   at Robotz.GameBoard.DrawPicture() in c:\documents and settings\tom\desktop\robotzbest\gameboard.cs:line 70
   at Robotz.GameBoard..ctor(PictureBox pBox, String resBoardName, ImageList iList) in c:\documents and settings\tom\desktop\robotzbest\gameboard.cs:line 60
   at Robotz.FormMainScreen.Form1_Activated(Object sender, EventArgs e) in c:\documents and settings\tom\desktop\robotzbest\formmainscreen.cs:line 103
   at System.Windows.Forms.Form.OnActivated(EventArgs e)
   at System.Windows.Forms.Form.set_Active(Boolean value)
   at System.Windows.Forms.Form.WmActivate(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.SafeNativeMethods.ShowWindow(IntPtr hWnd, Int32 nCmdShow)
   at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.set_Visible(Boolean value)
   at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at Robotz.FormMainScreen.Main() in c:\documents and settings\tom\desktop\robotzbest\formmainscreen.cs:line 96The program '[504] WindowsApplication1.exe' has exited with code 0 (0x0).

Question by:Tom Knowlton
LVL 22

Accepted Solution

Mohammed Nasman earned 2000 total points
ID: 12670163

  Try to add new bitmap that has more colors (indexed colors are 8bit or less)

      Bitmap b = new Bitmap("board.bmp");
      //this will gave you the same error
      //Graphics g = Graphics.FromImage(b);
      // use this to solve the problem
      Bitmap b2 = new Bitmap(b.Width,b.Height,System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
      Graphics g = Graphics.FromImage(b2);



Author Comment

by:Tom Knowlton
ID: 12670177

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question