System.Drawing.Image.Save() on Transparent GIF

G

Guest

I am creating GIF images with transparent backgrounds on-the-fly for a web
app and rendering them by using
System.Drawing.Image.Save(Response.OutputStream, ImageType.GIF).

I am confident that the transparency is working properly because if I save
the created image to the local hard disk and then view it in a web page or an
image editor, the transparency is correct. I can also view the transparency
on-the-fly in a Windows.Forms application.

Yet, when I render the image on-the-fly to the Response.OutputStream, I lose
the transparency and the background color is displayed in the page.

Has anyone got any ideas or experience in rendering transparent GIFs this way?

Thanks,
 
S

Steven Cheng[MSFT]

Hello Dale,

From your description, you've programmatically create an transparent gif
image and flush it into ASP.NET page's response stream. However, you found
the output image lose the transparency, correct?

Based on my understanding, it is likely that the bits save into the
response stream is different from the expected transparent gif's binary
bits. I suggest you test through the following means:

1. first use your code to generate the gif and save it into a disk file(gif
file), make sure that the gif file is working as transparent one.

2. use ASP.NET code to write the gif file into response stream as below:

===============
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Response.ClearHeaders()
Response.ClearContent()
Response.ContentType = "image/gif"


Response.WriteFile("D:\temp\web_temp\images\duglogobigtextcolor.gif")

Response.End()



End Sub
===============

based on my test, the above code can correctly flush a correct transparent
gif out to client-side. If the above test also return a non-transparent
one, it seems the generated gif has some bits get corrupted.

Please feel free to let me know if you have any other finding or anything I
missed.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Thanks, Steven. I was afraid you were going to say that. Our web
infrastructure policies don't generally allow web apps to write to the local
disk so that's going to be a problem. I may have to look for another
alternative.

The goal was to present an error message in a text bubble with the bubble
hook pointing to the form field with the improper value. The transparent GIF
seemed like just the ticket.

Do you have any other suggestions or know of articles or examples of
alternative methods for displaying text in a bubble like that on-the-fly?

I built my code following the examples in KB #319061 and
http://www.bobpowell.net/giftransparency.htm. Do you know if I would have
the same problem with a transparent PNG? If a PNG is a possibility, do you
know of any documentation on how to set the transparency in a PNG using .Net?
 
G

Guest

Just FYI, Steven.

I was able to resolve my problem and, hopefully make my solution more robust
than it may have otherwise been.

The problem was that the Bitmap object I was drawing on using the Graphics
class seems to corrupt the palette by duplicating a single palette entry
which was, coincidentally, the transparent color.

While I was aware of the duplication, I was skipping the first occurrence
because no pixels pointed to it. The second occurrence was where all the
transparent pixels pointed and that is the entry I set the alpha to 0 on.
This issue did not cause any problems in Windows.Forms, in Paint Shop Pro, in
Microsoft Image Composer, or if I saved to disk and then opened the saved
file in a web page.

For some reason, though, it did create a problem when saving to the
Response.OutputStream because the method requires re-assigning the ImageType.
There should be an overload of the Save method that writes to a Stream
without having to pass the ImageType just as there is an overload that saves
to a file.

The bottom line is, I re-wrote my code to detect duplicate entries in the
palette, then modify all pixels that pointed to any of the duplicates so that
they point to the first occurrence of the color, and then modify all of the
remaining duplicate palette entries so that they are unique. With this done,
I was able to save my image to the Response.OutputStream with the
transparency working as expected.

Thanks again for your help.

Dale
--
Dale Preston
MCAD C#
MCSE, MCDBA
 
S

Steven Cheng[MSFT]

Hi Dale,

Thanks for your followup.

I'm glad that you've figured out the problem and found a workable solution.

As always, welcome to post here when you need any help from us.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top