BMP files not working with Graphics object?

Posted on 2004-11-24
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\\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\\1.0.3300.0__b77a5c561934e089\', 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:knowlton
    LVL 22

    Accepted Solution


      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);


    LVL 5

    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now