Groups | Blog | Home
all groups > dotnet drawing api > june 2005 >

dotnet drawing api : make image transparent


jeff
6/8/2005 8:50:33 AM

hi, All

I have a control with a pic(gif),
I want to make the pic is transparent.
How to do it?

Thanks
Bob Powell [MVP]
6/9/2005 12:00:00 AM
To modify the GIF's transparent colour see the GDI+ FAQ article.

To make it transparent after you've loaded it in a Bitmap see
Bitmap.MakeTransparent.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





[quoted text, click to view]

Steve Bugden
8/13/2005 12:39:03 PM
Hi,

From your above suggestion I tried the following in the paint event of a form:

'******************************
Dim myBitmap As Bitmap
Dim strFIleName As String = "C:\temp\someimage.gif"

' Make the image transparent and save it
myBitmap = New Bitmap(strFIleName)
myBitmap.MakeTransparent(Color.White)
myBitmap.Save(strFIleName)

'Get the image again
myBitmap = New Bitmap(strFIleName)

' Draw the transparent bitmap to the screen.
e.Graphics.DrawImage(myBitmap, myBitmap.Width, 0, myBitmap.Width, _
myBitmap.Height)
'******************************

THis works fine on the windows form but when I insert the image into an html
page the background is black.

Hope no one minds me tagging on to this existing question/answer.

Can anyone tell me what I'm doing wrong?

Many thanks,

Steve

[quoted text, click to view]
Michael Phillips, Jr.
8/13/2005 4:44:48 PM
I suggest that you create an ImageAtributes object
and use the method SetColorKey to set the color-key
to the transparent color of the .gif file.

Then use DrawImage.

Example:
Dim imageAttr As New ImageAttributes()
Dim transColor As Color
myBitmap.GetPixel(0,0,transColor) ' Color.White
imageAttr.SetColorKey(transColor,transColor,ColorAdjustType.Default)
Dim rect as New Rectangle(0,0,myBitmap.Width,myBitmap.Height)
' Draw the .gif file blending with the background
e.Graphics.DrawImage(myBitmap, rect, 0, 0, myBitmap.Width,myBitmap.Height, _
GraphicsUnit.Pixel, imageAttr)

This will allow you to draw against any background.
The transparent portion of the .gif file will be replaced
with the background that you draw against.


[quoted text, click to view]

Steve Bugden
8/16/2005 6:42:04 AM
Hi Michael,

Thanks for your reply, from your code, I tried the following:

'Create the image
bmpImage = New Bitmap(50, 50)
grfgraphics = Graphics.FromImage(bmpImage)

Dim imageAttr As New ImageAttributes
Dim transColor As New Color
transColor = Color.White
imageAttr.SetColorKey(transColor, transColor, ColorAdjustType.Default)
Dim objrectangle As New Rectangle(0, 0, bmpImage.Width, bmpImage.Height)
'Draw the .gif file blending with the background
grfgraphics.DrawImage(bmpImage, objrectangle, 0, 0, bmpImage.Width,
bmpImage.Height, GraphicsUnit.Pixel, imageAttr)
bmpImage.Save("C:\\temp\temp.gif")

But the image is black when displayed in an html page. Have I missed
something?

Best Regards,

Steve

[quoted text, click to view]
Michael Phillips, Jr.
8/16/2005 11:04:22 AM
I believe that I am a little confused as to what you wish
to accomplish.

1) For a .gif file with a transparent color,
MakeTransparent is used to let the system know
what color is transparent in the .gif for purposes of
drawing the .gif to a background with some other
color. The transparent portion of the .gif will be
replaced with the color of the background.
2) In this case, SetColorKey does the same thing.
It sets the color-key to the transparent portion of the
.gif. When the image is drawn the transparent portion
of the image is replaced with the background that
you draw to.

In either case, you either hard code the color or you
use GetPixel to get the color at a point such as x=0,y=0
for the transparent color.

If the .gif background shows up as black in a web page,
then the .gif's color table may not have been encoded
correctly by gdiplus's .gif image encoder.

I have had problems in this regard using the .gif encoder
that comes with gdiplus. I don't believe it follows the
..gif specification correctly!

It creates a local color table with the transparency index set
in the .gif header but fails to read and use this index when
the image is reloaded. I believe it should create a Global
Color Table with the transparency index and background
color set per the specification.

If you wish to change the transparent color, you must remap
the colors in the palette to account for the new transparent color.

Bob Powell has an excellent article on how to accomplish this.
You may read it here:
http://www.bobpowell.net/giftransparency.htm

His method works and the image will be correctly displayed
either in a form or a web page.

[quoted text, click to view]

Steve Bugden
8/25/2005 7:19:04 AM
Hi Michael,

Sorry for the delay in replying, I have been away.

Thankyou for your reply.

To explain what I am trying to do, I have created a simple function
(attached). The function simply creates a white image with some green text.

All I want to do in this case, is make white the transparent colour.

If I comment out the MakeTransparent line, the function works in the sense
that it creates a label with text on a white background.

If I leave 'MakeTransparent' call in, the image comes out black on my system
at least.

So my question is, what do I have to do to make the white the transparent
colour?

Best Regards,

Steve Bugden.

'**************************************************
Function CreateLabel()

Dim intwidth As Integer = 100
Dim intHeight As Integer = 20


Dim bmpImage As Bitmap
'Get a graphics object to use for all the drawing
Dim grfgraphics As Drawing.Graphics

'Create the image
bmpImage = New Bitmap(intwidth, intHeight)
grfgraphics = Graphics.FromImage(bmpImage)

Dim transColor As New Color
transColor = Color.White
Dim objrectangle As New Rectangle(0, 0, intwidth, intHeight)
grfgraphics.DrawImage(bmpImage, objrectangle, 0, 0, intwidth, intHeight,
GraphicsUnit.Pixel)
bmpImage.MakeTransparent(transColor)

Dim mybrush As Brush

mybrush = New SolidBrush(transColor)
grfgraphics.FillRectangle(mybrush, 0, 0, intwidth, intHeight)

'Write the text
mybrush = New SolidBrush(Color.Green)
Dim objfont As Font
objfont = New Font("tahoma", 10, FontStyle.Regular)
grfgraphics.DrawString("Test", objfont, mybrush, 0, 0)

'Save the image
bmpImage.Save("C:\CustomerWebSites\astrid\images\temp.gif")

End Function
'*******************************************************

[quoted text, click to view]
AddThis Social Bookmark Button