.NETGURU
Unable to create Transparent background on Gif
Messages   Related Types
This message was discovered on ASPFriends.com 'ngfx-drawing' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Jeffrey Widmer
I cannot create a transparent background for a gif. Either setting the
color to transparent or using the MakeTransparent method of the Bitmap
class, the background always comes out black.

Here's the code I am using, anyone have any ideas??

Dim context As System.Web.HttpContext = System.Web.HttpContext.Current
Dim strText As String = Request.Params("Text")

Dim imgComposite As System.Drawing.Bitmap

Dim pf As System.Drawing.Imaging.PixelFormat
pf = Drawing.Imaging.PixelFormat.Format32bppArgb

imgComposite = New System.Drawing.Bitmap(400, 400, pf)
Dim grphComposite As System.Drawing.Graphics
grphComposite = System.Drawing.Graphics.FromImage(imgComposite)

Dim objBrush As System.Drawing.SolidBrush = New SolidBrush(Color.Navy)
Dim objFont As Font = New Font("Verdana", 10)

'>>> Color.Transparent is not transparent but black
grphComposite.Clear(imgComposite.GetPixel(1, 1))
grphComposite.FillRectangle(New SolidBrush(Color.Transparent), _
0, 0, intWidth, intHeight)
grphComposite.DrawString("This is the text:", objFont, objBrush, 0, 0)
grphComposite.DrawString(strText, objFont, objBrush, 0, 20)

grphComposite.Flush()

'>>> This does not do anything but make the specified color black
'imgComposite.MakeTransparent(imgComposite.GetPixel(1, 1))

context.Response.ContentType = "image/gif"
imgComposite.Save(context.Response.OutputStream, _
System.Drawing.Imaging.ImageFormat.Gif)

context.Response.Flush()

Thanks in advance,
-Jeff

Reply to this message...
 
    
Peter Brunone (VIP)
Jeff,

    After exhausting every apparent possibility and then some -- as I'm sure
you have done -- I can only conclude that this is not possible in the
current framework (Public Beta 2 here). It seems that the image *always*
has a black background (notice that if you don't specify a color, it's
black), and anything you do merely lays on top of that background.

    I wish it weren't so, but perhaps this is something they'll fix before
RTM...?

Regards,

Peter

----- Original Message -----
From: "Jeffrey Widmer" <Click here to reveal e-mail address>
To: "ngfx-drawing" <Click here to reveal e-mail address>
Sent: Friday, January 11, 2002 3:27 PM
Subject: [ngfx-drawing] Unable to create Transparent background on Gif

| I cannot create a transparent background for a gif. Either setting the
| color to transparent or using the MakeTransparent method of the Bitmap
| class, the background always comes out black.
|
| Here's the code I am using, anyone have any ideas??
|
| Dim context As System.Web.HttpContext = System.Web.HttpContext.Current
| Dim strText As String = Request.Params("Text")
|
| Dim imgComposite As System.Drawing.Bitmap
|
| Dim pf As System.Drawing.Imaging.PixelFormat
| pf = Drawing.Imaging.PixelFormat.Format32bppArgb
|
| imgComposite = New System.Drawing.Bitmap(400, 400, pf)
| Dim grphComposite As System.Drawing.Graphics
| grphComposite = System.Drawing.Graphics.FromImage(imgComposite)
|
| Dim objBrush As System.Drawing.SolidBrush = New SolidBrush(Color.Navy)
| Dim objFont As Font = New Font("Verdana", 10)
|
| '>>> Color.Transparent is not transparent but black
| grphComposite.Clear(imgComposite.GetPixel(1, 1))
| grphComposite.FillRectangle(New SolidBrush(Color.Transparent), _
| 0, 0, intWidth, intHeight)
| grphComposite.DrawString("This is the text:", objFont, objBrush, 0, 0)
| grphComposite.DrawString(strText, objFont, objBrush, 0, 20)
|
| grphComposite.Flush()
|
| '>>> This does not do anything but make the specified color black
| 'imgComposite.MakeTransparent(imgComposite.GetPixel(1, 1))
|
| context.Response.ContentType = "image/gif"
| imgComposite.Save(context.Response.OutputStream, _
| System.Drawing.Imaging.ImageFormat.Gif)
|
| context.Response.Flush()
|
| Thanks in advance,
| -Jeff

Reply to this message...
 
    
Jeff Widmer
Peter,
Thanks for your help. It's always good to have a second set of eyes looking
at the problem. Fortunately, for the project I am currently working on, I
do not actually have to export a gif with a transparent background. All I
have to do is import two bitmaps, one is the base and the other lays on top,
with white being the transparent color for the top image. I can combine
these two images using something like:

imgOverlay.MakeTransparent(Color.White)
imgBase.DrawImage(imgOverlay)

The code above actually does set the Color.White in imgOverlay to
transparent and the imgBase shows through. imgBase just happens to be a
solid image without any transparent areas, so it seems that I have a
solution.

But I still would like an answer to why a gif, when exported either as a
stream or a file, cannot have a transparent color. Is this a bug?? or are
we missing something??

Thanks,
-Jeff

-----Original Message-----
From: Peter Brunone [mailto:Click here to reveal e-mail address]
Sent: Friday, January 11, 2002 7:58 PM
To: ngfx-drawing
Subject: [ngfx-drawing] RE: Unable to create Transparent background on
Gif

Jeff,

    After exhausting every apparent possibility and then some -- as I'm sure
you have done -- I can only conclude that this is not possible in the
current framework (Public Beta 2 here). It seems that the image *always*
has a black background (notice that if you don't specify a color, it's
black), and anything you do merely lays on top of that background.

    I wish it weren't so, but perhaps this is something they'll fix before
RTM...?

Regards,

Peter

----- Original Message -----
From: "Jeffrey Widmer" <Click here to reveal e-mail address>
To: "ngfx-drawing" <Click here to reveal e-mail address>
Sent: Friday, January 11, 2002 3:27 PM
Subject: [ngfx-drawing] Unable to create Transparent background on Gif

| I cannot create a transparent background for a gif. Either setting the
| color to transparent or using the MakeTransparent method of the Bitmap
| class, the background always comes out black.
|
| Here's the code I am using, anyone have any ideas??
|
| Dim context As System.Web.HttpContext = System.Web.HttpContext.Current
| Dim strText As String = Request.Params("Text")
|
| Dim imgComposite As System.Drawing.Bitmap
|
| Dim pf As System.Drawing.Imaging.PixelFormat
| pf = Drawing.Imaging.PixelFormat.Format32bppArgb
|
| imgComposite = New System.Drawing.Bitmap(400, 400, pf)
| Dim grphComposite As System.Drawing.Graphics
| grphComposite = System.Drawing.Graphics.FromImage(imgComposite)
|
| Dim objBrush As System.Drawing.SolidBrush = New SolidBrush(Color.Navy)
| Dim objFont As Font = New Font("Verdana", 10)
|
| '>>> Color.Transparent is not transparent but black
| grphComposite.Clear(imgComposite.GetPixel(1, 1))
| grphComposite.FillRectangle(New SolidBrush(Color.Transparent), _
| 0, 0, intWidth, intHeight)
| grphComposite.DrawString("This is the text:", objFont, objBrush, 0, 0)
| grphComposite.DrawString(strText, objFont, objBrush, 0, 20)
|
| grphComposite.Flush()
|
| '>>> This does not do anything but make the specified color black
| 'imgComposite.MakeTransparent(imgComposite.GetPixel(1, 1))
|
| context.Response.ContentType = "image/gif"
| imgComposite.Save(context.Response.OutputStream, _
| System.Drawing.Imaging.ImageFormat.Gif)
|
| context.Response.Flush()
|
| Thanks in advance,
| -Jeff

| [ngfx-drawing] member Click here to reveal e-mail address = YOUR ID
| http://www.aspfriends.com/aspfriends/ngfx-drawing.asp = JOIN/QUIT

Reply to this message...
 
    
Peter Brunone (VIP)
Jeff,

    That's pretty much the only solution I could think of. It's possible that
there's a way to correct this; maybe with more documentation we can find the
answer.
    I've sent your letter on to the ASP.net team with the assumption that it's
a bug, or at least an oversight. If it's not been addressed yet, I doubt
the problem will be corrected before RTM, so let's hope that it has. Of
course, I'm sure some brilliant soul will come up with a workaround in the
meantime. Is it perhaps possible to open a previously created gif and make
part of it transparent? Let me know if you try that out, and I'll do the
same.

Cheers,

Peter

|-----Original Message-----
|From: Jeff Widmer [mailto:Click here to reveal e-mail address]
|Sent: Friday, January 11, 2002 8:56 PM
|To: ngfx-drawing
|Subject: [ngfx-drawing] RE: Unable to create Transparent background on
|Gif
|
|
|Peter,
|Thanks for your help. It's always good to have a second set of
|eyes looking
|at the problem. Fortunately, for the project I am currently working on, I
|do not actually have to export a gif with a transparent background. All I
|have to do is import two bitmaps, one is the base and the other
|lays on top,
|with white being the transparent color for the top image. I can combine
|these two images using something like:
|
|imgOverlay.MakeTransparent(Color.White)
|imgBase.DrawImage(imgOverlay)
|
|The code above actually does set the Color.White in imgOverlay to
|transparent and the imgBase shows through. imgBase just happens to be a
|solid image without any transparent areas, so it seems that I have a
|solution.
|
|But I still would like an answer to why a gif, when exported either as a
|stream or a file, cannot have a transparent color. Is this a bug?? or are
|we missing something??
|
|Thanks,
|-Jeff

Reply to this message...
 
    
Chris Garrett
Hi all,

[Original message clipped]

I have taken a good long look and unless something is escaping me (perfectly
possible, me being a bit dim) I cant find any way other than generating the
image with a funky-ass colour and using a command-line utility or old com
component to rig the transparent background. :O/

> I've sent your letter on to the ASP.net team

There you go, flaunting your connections ;O)

I would be interested in hearing the reply

Cheers

Chris

--

NEW:

Simple Web Service with Notepad
http://www.RealWorldASP.net

Don't miss the news! - Sign up for ASP/.NET Newsletter:
http://www.aspalliance.com/chrisg/subscribe.asp

Reply to this message...
 
    
Chris Garrett
Jeffrey Widmer said:

> I cannot create a transparent background for a gif.

After doing some research it looks to me like the encoding does not exist
yet in GDI+ version 1 to do progressive JPEG or transparent GIFs (although
if someone can prove this to be false I would love to hear it).

Check out
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDrawingImaging
EncoderValueClassTopic.asp?

And note where it says not implemented.

Cheers

Chris

--

NEW:

Simple Web Service with Notepad
http://www.RealWorldASP.net

Don't miss the news! - Sign up for ASP/.NET Newsletter:
http://www.aspalliance.com/chrisg/subscribe.asp

Reply to this message...
 
    
John F. Doran
For what its worth, I have tired to play with transparent gifs as
well...

The problem as I see it is this... There are two GIF standards.. GIF and
GIF89A. When you work in photoshop, for example, you need to 'export'
to GIF89A for transparent GIFs... There are also two standards for PNG
.. One will allow transparencies as well.. However the framework only
has output specifications for regular GIF (not GIF89A) and the same with
png. Microsoft needs to implement the GIF89A standard as a separate
output inorder to be able to stream that type of image.

Not sure if that helps clear things up at all...

John Doran

-----Original Message-----
From: Peter Brunone [mailto:Click here to reveal e-mail address]
Sent: Friday, January 11, 2002 10:54 PM
To: ngfx-drawing
Subject: [ngfx-drawing] RE: Unable to create Transparent background on
Gif

Jeff,

    That's pretty much the only solution I could think of. It's
possible that there's a way to correct this; maybe with more
documentation we can find the answer.
    I've sent your letter on to the ASP.net team with the assumption
that it's a bug, or at least an oversight. If it's not been addressed
yet, I doubt the problem will be corrected before RTM, so let's hope
that it has. Of course, I'm sure some brilliant soul will come up with
a workaround in the meantime. Is it perhaps possible to open a
previously created gif and make part of it transparent? Let me know if
you try that out, and I'll do the same.

Cheers,

Peter

|-----Original Message-----
|From: Jeff Widmer [mailto:Click here to reveal e-mail address]
|Sent: Friday, January 11, 2002 8:56 PM
|To: ngfx-drawing
|Subject: [ngfx-drawing] RE: Unable to create Transparent background on
|Gif
|
|
|Peter,
|Thanks for your help. It's always good to have a second set of eyes
|looking at the problem. Fortunately, for the project I am currently
|working on, I do not actually have to export a gif with a transparent
|background. All I have to do is import two bitmaps, one is the base
|and the other lays on top,
|with white being the transparent color for the top image. I can
combine
|these two images using something like:
|
|imgOverlay.MakeTransparent(Color.White)
|imgBase.DrawImage(imgOverlay)
|
|The code above actually does set the Color.White in imgOverlay to
|transparent and the imgBase shows through. imgBase just happens to be
|a solid image without any transparent areas, so it seems that I have a
|solution.
|
|But I still would like an answer to why a gif, when exported either as
|a stream or a file, cannot have a transparent color. Is this a bug??
|or are we missing something??
|
|Thanks,
|-Jeff

| [ngfx-drawing] member Click here to reveal e-mail address = YOUR ID
| http://www.aspfriends.com/aspfriends/ngfx-drawing.asp = JOIN/QUIT

Reply to this message...
 
 
System.Drawing.Bitmap
System.Drawing.Color
System.Drawing.Graphics
System.Drawing.Imaging.ImageFormat
System.Drawing.Imaging.PixelFormat
System.Drawing.SolidBrush
System.Web.HttpContext




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification