Why am I getting this message? I get it on both BMP's and GIF's. Do I need to
convert before I create my BitMap? See code below.
A Graphics object cannot be created from an image that has an indexed pixel
format.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.
Exception Details: System.Exception: A Graphics object cannot be created
from an image that has an indexed pixel format.
Source Error:
Line 26: Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Line 27: Dim bmp As Bitmap = New
Bitmap("C:\DinasourEye\image_map.gif")
Line 28: Dim canvas As Graphics = Graphics.FromImage(bmp)
Line 29: Dim strWatermark As String = "Property of Chris"
Line 30: canvas.DrawString(strWatermark, New Font("Verdana", 14,
FontStyle.Bold), _
Source File: c:\inetpub\wwwroot\Unleashed\Chapter27\WaterMarkOnImage.aspx.vb
Line: 28
Stack Trace:
[Exception: A Graphics object cannot be created from an image that has an
indexed pixel format.]
System.Drawing.Graphics.FromImage(Image image) +206
Unleashed.WaterMarkOnImage.Page_Load(Object sender, EventArgs e) in
c:\inetpub\wwwroot\Unleashed\Chapter27\WaterMarkOnImage.aspx.vb:28
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +739
The exception is concise and explicit. You cannot create a Graphics object
for any image with an indexed pixel format. This means GIFs etc.
The only way you can mess with one is to create a non-indexed image of the
same size, obtain a Graphics object for it, draw the original indexed image
to it and then manipulate the new image. Later, if you wish, you can save
the image in an indexed format.

Signature
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
> Why am I getting this message? I get it on both BMP's and GIF's. Do I need
> to
[quoted text clipped - 37 lines]
> System.Web.UI.Control.LoadRecursive() +35
> System.Web.UI.Page.ProcessRequestMain() +739
Chris Davoli - 29 Apr 2005 01:20 GMT
Bob,
Do you have some sample code that does this?
> The exception is concise and explicit. You cannot create a Graphics object
> for any image with an indexed pixel format. This means GIFs etc.
[quoted text clipped - 45 lines]
> > System.Web.UI.Control.LoadRecursive() +35
> > System.Web.UI.Page.ProcessRequestMain() +739
Chris Davoli - 29 Apr 2005 01:26 GMT
Is there a way to get the size if the non-indexed image so you can create a
non-idexed image of the same size?
> The exception is concise and explicit. You cannot create a Graphics object
> for any image with an indexed pixel format. This means GIFs etc.
[quoted text clipped - 45 lines]
> > System.Web.UI.Control.LoadRecursive() +35
> > System.Web.UI.Page.ProcessRequestMain() +739
Bob Powell [MVP] - 29 Apr 2005 08:43 GMT
Bitmap org=(Bitmap)Image.FromFile(<original image of gif type>);
Bitmap bm=new Bitmap(org.Width, Org.Height); << Can specify optional pixel
format but defaults to 32bppArgb
Graphics g=Graphics.FromImage(bm);
g.DrawImage(org,0,0);
//Draw other stuff on g here...
g.Dispose();
bm.Save(<filename>,<format>);

Signature
Bob Powell [MVP]
Visual C#, System.Drawing
Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
> Is there a way to get the size if the non-indexed image so you can create
> a
[quoted text clipped - 58 lines]
>> > System.Web.UI.Control.LoadRecursive() +35
>> > System.Web.UI.Page.ProcessRequestMain() +739