Re: make image transparent



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.

"Steve Bugden" <SteveBugden@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:4746317E-BE11-447E-B6A5-E2D43F64EB1C@xxxxxxxxxxxxxxxx
> 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
>
> "Michael Phillips, Jr." wrote:
>
>> 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.
>>
>>
>> "Steve Bugden" <SteveBugden@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
>> news:1F260D2B-3CFD-44D4-B8DC-73A04B3F7897@xxxxxxxxxxxxxxxx
>> > 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
>> >
>> > "Bob Powell [MVP]" wrote:
>> >
>> >> 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.
>> >>
>> >>
>> >>
>> >>
>> >>
>> >> "jeff" <jeff@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
>> >> news:1F8202EB-4BB2-4B05-9A1E-B3B0A64AF01B@xxxxxxxxxxxxxxxx
>> >> >
>> >> > hi, All
>> >> >
>> >> > I have a control with a pic(gif),
>> >> > I want to make the pic is transparent.
>> >> > How to do it?
>> >> >
>> >> > Thanks
>> >> >
>> >>
>> >>
>> >>
>>
>>
>>


.



Relevant Pages

  • Re: make image transparent
    ... All I want to do in this case, is make white the transparent colour. ... Dim intwidth As Integer = 100 ... > 1) For a .gif file with a transparent color, ... >> Dim imageAttr As New ImageAttributes ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Table & Cell borders and spacing
    ... the cell spacing with the problem with the border-like lines ... Thanks for the great idea of the transparent gif, ... I actually created a gif of the color that I wanted and placed it into ... border size and color 'nothing' and selected collapse table border. ...
    (microsoft.public.frontpage.client)
  • Re: transparent colour
    ... Did you try to load your gif and draw it? ... the transparent color in you gif. ... > Dim intwidth As Integer = 100 ... intHeight) ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: make image transparent
    ... Dim imageAttr As New ImageAttributes ... ' Draw the .gif file blending with the background ... This will allow you to draw against any background. ... The transparent portion of the .gif file will be replaced ...
    (microsoft.public.dotnet.framework.drawing)
  • RE: System.Drawing.Image.Save() on Transparent GIF
    ... infrastructure policies don't generally allow web apps to write to the local ... disk so that's going to be a problem. ... The transparent GIF ... the same problem with a transparent PNG? ...
    (microsoft.public.dotnet.framework.aspnet)

Loading