I am having trouble defining the colors in the palette. The first method I
tried gave me the error Object reference not set to an instance of an
object. (Which didn't surprise me, but it would not let me use the keyword
New when declaring the Imaging.ColorPalette). Here is that code:
Dim transbitmap As New Bitmap(400, 400)
Dim transgraphics As Graphics = Graphics.FromImage(transbitmap)
Dim transpen As New Pen(Color.FromArgb(255, 0, 255, 0), 20)
Dim transbrush As New SolidBrush(Color.FromArgb(255, 255, 0, 0))
Dim transpalette As Imaging.ColorPalette
transpalette.Entries(0) = Color.FromArgb(0, 255, 255, 255)
transpalette.Entries(1) = Color.FromArgb(255, 0, 255, 0)
transpalette.Entries(2) = Color.FromArgb(255, 255, 0, 0)
transgraphics.Clear(Color.FromArgb(0, 255, 255, 255))
transbitmap.Palette = transpalette
transgraphics.FillRectangle(transbrush, 100, 100, 200, 200)
transgraphics.DrawRectangle(transpen, 50, 50, 150, 150)
transbitmap.Save(Server.MapPath("GDItest.gif"), Imaging.ImageFormat.Gif)
The second method I tried gave me the error Index was outside the bounds of
the array. Here is that code:
Dim transbitmap As New Bitmap(400, 400)
Dim transgraphics As Graphics = Graphics.FromImage(transbitmap)
Dim transpen As New Pen(Color.FromArgb(255, 0, 255, 0), 20)
Dim transbrush As New SolidBrush(Color.FromArgb(255, 255, 0, 0))
transbitmap.Palette.Entries(0) = Color.FromArgb(0, 255, 255, 255)
transbitmap.Palette.Entries(1) = Color.FromArgb(255, 0, 255, 0)
transbitmap.Palette.Entries(2) = Color.FromArgb(255, 255, 0, 0)
transgraphics.Clear(Color.FromArgb(0, 255, 255, 255))
transgraphics.FillRectangle(transbrush, 100, 100, 200, 200)
transgraphics.DrawRectangle(transpen, 50, 50, 150, 150)
transbitmap.Save(Server.MapPath("GDItest.gif"), Imaging.ImageFormat.Gif)
What I expected from the code above was to create a GIF file with a red
filled rectangle, a green unfilled rectangle, and a transparent background.
If I remove the lines where I attempt to define the palette, this is what I
got except instead of a transparent background I ended up with a black
background. What am I supposed to do to create/edit the palette? The article
you mentioned took the palette from another Bitmap, which is not something I
am planning to do. Thanks.