GIFs are not the only way to create transparent images.  Nor are they the
best way - GIF only supports 8-bit indexed colour, which makes for pretty
ropey-looking images.

PNG is a much better format.  It supports full colour with transparency. And
unlike GIF, the transparency channel supports partial transparency. With GIF
a pixel is either transparent or opaque.  PNG supports partial transparency.
This means you can anti-alias the edges of your image and have it blend in
properly with whatever background you draw it over.

Also, lots of people seem to find that creating transparent GIFs doesn't
work in .NET, whereas I've never seen a problem with transparent PNGs.

So given all of that, I'd recommend trying PNG.

Doing pretty much what you've done there, but saving the stream as PNG
rather than GIF should work.


By the way, drawing with Color.Transparent *doesn't* work. This is because that just ends up drawing nothing! It does take the area you specify and make it transparent - it just draws the item completely transparently, i.e. it makes no difference to what was already there.

The way to draw a transparent bitmap is to understand that when you first
create a new bitmap, it starts off completely transparent.  By using the
Clear method to set the bitmap to white in your example below, you end up
turning off the transparency, and there's no way of getting it back.  (Apart
from MakeTransparent, but that's not ideal - it means you don't get the
anti-aliasing benefits offered by the default 8 bit transparency channel.)

Something like this works fine though:

   Bitmap bmp = new Bitmap(200, 200);
   using (Graphics g = Graphics.FromImage(bmp))
   {
       g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
       g.FillEllipse(Brushes.Green, 0, 0, 200, 200);
   }
   bmp.Save(@"c:\temp.png", System.Drawing.Imaging.ImageFormat.Png);


-- Ian Griffiths - http://www.interact-sw.co.uk/iangblog/ DevelopMentor - http://www.develop.com/

----- Original Message -----
From: "Bryan Clauss" <[EMAIL PROTECTED]>


I want to create a transparent image (GIF format) at run time.   From what
I have seen on the web, the only way to create a transparent image is to
use the GIF89a format.

At this time I am trying to create a circle to place around some text on
my
web page.

I have created a "helper" ASPX page with a VB Code page to create my image
in.  Here is the code snippet:

'my Page_Load Sub is not wrapped.
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load

       Dim m_Stream As New System.IO.MemoryStream

       Dim newImage As System.Drawing.Bitmap
       Dim newGraphic As System.Drawing.Graphics
       Dim tmpPen As System.Drawing.Pen

       ' Create Bitmap
       newImage = New Bitmap(200, 200)

       tmpPen = New System.Drawing.Pen(Brushes.Red, 10)

       ' Initialize Graphics Class
       newGraphic = Graphics.FromImage(newImage)
       newGraphic.Clear(Color.White)

       newGraphic.DrawEllipse(tmpPen, 10, 10, 180, 180)

       newImage.MakeTransparent(Color.White)
       newImage.Save(m_Stream, System.Drawing.Imaging.ImageFormat.Gif())

       ' Display Bitmap
       Response.ClearHeaders()
       Response.ClearContent()
       Response.ContentType = "image/gif"
       Response.AddHeader("Content-Type", "application/unknown")
       Response.AppendHeader("Content-Transfer-Encoding", "Binary")
       Response.BinaryWrite(m_Stream.ToArray())
       Response.End()

End Sub

and in my ASPX page, I load the image using the following:
<img src="PDFForm.aspx" style="Z-INDEX: 1100; LEFT: 100px; POSITION:
absolute; TOP: 100px">

When the image displays, I get a red circle with a black square
background.

Is there a way to actually create a transparent GIF at run time?

I have tried the following to create a circular "window" within the image
by replacing the DrawEllipse command.  No luck there.
  newGraphic.FillEllipse(Brushes.Transparent, 10, 10, 180, 180)

I have tried the following to create a square "window" in the middle of
the
image:

       For X = 50 To 150
           For Y = 50 To 150
               newImage.SetPixel(X, Y, Color.Transparent)
           Next
       Next

I have even tried to use the above code, and setting the color to Green,
and then used this command:

       newImage.MakeTransparent(Color.Green)

Help me!  I am pulling out what is left of my hair.

(Boy, if you look at all of my questions that I have sent to this forum,
they all seem to deal with images.  Is there a more appropriate "image"
forum out there?)

Thank you all,
 Bryan Clauss

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
Some .NET courses you may be interested in:

Essential .NET: building applications and components with C#
November 29 - December 3, in Los Angeles
http://www.develop.com/courses/edotnet

View archives and manage your subscription(s) at
http://discuss.develop.com


=================================== This list is hosted by DevelopMentor� http://www.develop.com Some .NET courses you may be interested in:

Essential .NET: building applications and components with C#
November 29 - December 3, in Los Angeles
http://www.develop.com/courses/edotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to