Nathan
Fri Aug 12 19:42:13 CDT 2005
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.
--
Nathan Sokalski
njsokalski@hotmail.com
http://www.nathansokalski.com/
"Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message
news:OlQAv83nFHA.3448@TK2MSFTNGP12.phx.gbl...
> Hi Nathan,
>
> Okay, basically you have a palette to work with. The article tells you how
> to define the colors in the palette. Also note that the palette can
> certainly have LESS than 256 colors if you don't need them all. So,
> assuming you're drawing with GDI+, you just clear the bitmap to the
> transparent color, and draw with the others. All of the pixels that have
> the transparent color will be transparent. It doesn't matter what the
> transparent color is; it will be defined as "the transparent color" in the
> palette.
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> .Net Developer
> Expect the unaccepted.
>
> "Nathan Sokalski" <njsokalski@hotmail.com> wrote in message
> news:%23daSLW3nFHA.1996@TK2MSFTNGP10.phx.gbl...
>>I looked at the article you mentioned, and it definitely cleared up for me
>>why I was never getting any transparency, but I am still having trouble
>>figuring out how to fix the problem. I think what I really need is just a
>>basic example that just draws a simple shape or two and then adds a
>>transparent area. (Preferably in VB.NET, but I can usually get enough from
>>C# that it helps enough to solve my problem) Thanks.
>> --
>> Nathan Sokalski
>> njsokalski@hotmail.com
>>
http://www.nathansokalski.com/
>>
>> "Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message
>> news:%2335It9ynFHA.3380@TK2MSFTNGP12.phx.gbl...
>>> Hi Nathan,
>>>
>>> Sorry, I missed the "gif" reference in your OP.
>>>
>>> A GIF image palette can define one color as transparent. I'm not sure
>>> how you're defining the transparent color in your palette, but the
>>> following Microsoft KB article explains how to do it. Note that the
>>> article is not specifically about making a transparent gif, but it
>>> covers creating a palette with a single transparent color, which is
>>> encoded as the transparent color for the GIF:
>>>
>>>
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q319061
>>>
>>> --
>>> HTH,
>>>
>>> Kevin Spencer
>>> Microsoft MVP
>>> .Net Developer
>>> Everybody picks their nose,
>>> But some people are better at hiding it.
>>>
>>> "Nathan Sokalski" <njsokalski@hotmail.com> wrote in message
>>> news:On0xnMtnFHA.2916@TK2MSFTNGP14.phx.gbl...
>>>> Like I said in my original message, I am using GIF. The line that I use
>>>> to save my image is as follows (testbitmap is the name of my
>>>> System.Drawing.Bitmap object):
>>>>
>>>> testbitmap.Save(Server.MapPath("GDItest.gif"), Imaging.ImageFormat.Gif)
>>>>
>>>> --
>>>> Nathan Sokalski
>>>> njsokalski@hotmail.com
>>>>
http://www.nathansokalski.com/
>>>>
>>>> "Kevin Spencer" <kevin@DIESPAMMERSDIEtakempis.com> wrote in message
>>>> news:eq$BVdrnFHA.3936@TK2MSFTNGP10.phx.gbl...
>>>>> The only graphics formats that have transparency in HTML are GIF and
>>>>> PNG. What format are you using?
>>>>>
>>>>> --
>>>>> HTH,
>>>>>
>>>>> Kevin Spencer
>>>>> Microsoft MVP
>>>>> .Net Developer
>>>>> Everybody picks their nose,
>>>>> But some people are better at hiding it.
>>>>>
>>>>> "Nathan Sokalski" <njsokalski@hotmail.com> wrote in message
>>>>> news:OtgRhOqnFHA.3936@TK2MSFTNGP10.phx.gbl...
>>>>>>I am trying to create graphics with GDI+ that include transparency.
>>>>>>However, the transparency never seems to show up, even though my
>>>>>>colors have an alpha value of 0. How can I generate a graphic that is
>>>>>>completely transparent in certain areas (so that the web page
>>>>>>background shows through)? I save my graphics as gif files. I would
>>>>>>appreciate, if possible, a simple example so that I can see the code.
>>>>>>Thanks.
>>>>>> --
>>>>>> Nathan Sokalski
>>>>>> njsokalski@hotmail.com
>>>>>>
http://www.nathansokalski.com/
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>