Connecting Tech Pros Worldwide Forums | Help | Site Map

Noway to create a transparent gif with gdi !

WT
Guest
 
Posts: n/a
#1: May 20 '07
I am trying to crete a gif file with transparent backcolor on the fly but it
seems that GDI refuses to do this.
I am using samples from Bob Powell but it doesn't seem to work.

First create a Bitmap
public Bitmap MakeImage(string Text, SizeF Sf)

{

int width = (int)(Sf.Width)+2;

int height = (int)(Sf.Height)+2;

Bitmap imageObj = new Bitmap(width, height);

Graphics graphicsObj = Graphics.FromImage(imageObj);

// Create a border in the color of the font

//graphicsObj.FillRectangle(new SolidBrush(_FontColor), 0, 0, width,
height);

// Create a LightBlue background

graphicsObj.FillRectangle(new SolidBrush(_BackgroundColor), 0, 0, width,
height);

// Specify the font and alignment

Font fontBanner = new Font(_FontFace, _FontSize, _FontStyle );

// center align the advertising pitch

StringFormat stringFormat = new StringFormat();

stringFormat.Alignment = StringAlignment.Center;

stringFormat.LineAlignment = StringAlignment.Center;

// Draw the adverising pitch

graphicsObj.DrawString(Text, fontBanner, new SolidBrush(_FontColor), new
Rectangle(0, 0, width, height), stringFormat);

return imageObj;

}



Then save it as gif

imMail.Save(imFileName, System.Drawing.Imaging.ImageFormat.Gif);

imMail.Dispose();

Then reload and convert it to transparent background

imMail= new Bitmap(imFileName);

Bitmap bm = img.Convert2Gif(imMail);

if (bm != null)

{

imMail.Dispose();

System.IO.File.Delete(imFileName);

bm.Save(imFileName, System.Drawing.Imaging.ImageFormat.Gif);

bm.Dispose();

}



Here is the convert

imMail= new Bitmap(imFileName);



Eliyahu Goldin
Guest
 
Posts: n/a
#2: May 20 '07

re: Noway to create a transparent gif with gdi !


You will likely get more responses from drawing or gdi newsgroups.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


"WT" <WT@newsgroups.nospamwrote in message
news:eQbLypmmHHA.596@TK2MSFTNGP06.phx.gbl...
Quote:
>I am trying to crete a gif file with transparent backcolor on the fly but
>it seems that GDI refuses to do this.
I am using samples from Bob Powell but it doesn't seem to work.
>
First create a Bitmap
public Bitmap MakeImage(string Text, SizeF Sf)
>
{
>
int width = (int)(Sf.Width)+2;
>
int height = (int)(Sf.Height)+2;
>
Bitmap imageObj = new Bitmap(width, height);
>
Graphics graphicsObj = Graphics.FromImage(imageObj);
>
// Create a border in the color of the font
>
//graphicsObj.FillRectangle(new SolidBrush(_FontColor), 0, 0, width,
height);
>
// Create a LightBlue background
>
graphicsObj.FillRectangle(new SolidBrush(_BackgroundColor), 0, 0, width,
height);
>
// Specify the font and alignment
>
Font fontBanner = new Font(_FontFace, _FontSize, _FontStyle );
>
// center align the advertising pitch
>
StringFormat stringFormat = new StringFormat();
>
stringFormat.Alignment = StringAlignment.Center;
>
stringFormat.LineAlignment = StringAlignment.Center;
>
// Draw the adverising pitch
>
graphicsObj.DrawString(Text, fontBanner, new SolidBrush(_FontColor), new
Rectangle(0, 0, width, height), stringFormat);
>
return imageObj;
>
}
>
>
>
Then save it as gif
>
imMail.Save(imFileName, System.Drawing.Imaging.ImageFormat.Gif);
>
imMail.Dispose();
>
Then reload and convert it to transparent background
>
imMail= new Bitmap(imFileName);
>
Bitmap bm = img.Convert2Gif(imMail);
>
if (bm != null)
>
{
>
imMail.Dispose();
>
System.IO.File.Delete(imFileName);
>
bm.Save(imFileName, System.Drawing.Imaging.ImageFormat.Gif);
>
bm.Dispose();
>
}
>
>
>
Here is the convert
>
imMail= new Bitmap(imFileName);
>
>

Alexey Smirnov
Guest
 
Posts: n/a
#3: May 20 '07

re: Noway to create a transparent gif with gdi !


On May 20, 1:05 am, "WT" <W...@newsgroups.nospamwrote:
Quote:
Then reload and convert it to transparent background
>
imMail= new Bitmap(imFileName);
>
Bitmap bm = img.Convert2Gif(imMail);
>
You didn't sent the code for transparent background.

Cowboy \(Gregory A. Beamer\)
Guest
 
Posts: n/a
#4: May 20 '07

re: Noway to create a transparent gif with gdi !


You can create a transparent GIF, from my understanding, by adding the code
for the value that is transparent. No matter what you do, however, GDI is
always 256 colors, which is largely a waste. There are third party controls
that do a better job of GIF.

There is an article on the support site for "hijacking" a color palette that
might be useful for you. It is the only way to get things more GIF like with
Microsoft's lightweight GIF version.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
Co-author: Microsoft Expression Web Bible (upcoming)

************************************************
Think outside the box!
************************************************
"WT" <WT@newsgroups.nospamwrote in message
news:eQbLypmmHHA.596@TK2MSFTNGP06.phx.gbl...
Quote:
>I am trying to crete a gif file with transparent backcolor on the fly but
>it seems that GDI refuses to do this.
I am using samples from Bob Powell but it doesn't seem to work.
>
First create a Bitmap
public Bitmap MakeImage(string Text, SizeF Sf)
>
{
>
int width = (int)(Sf.Width)+2;
>
int height = (int)(Sf.Height)+2;
>
Bitmap imageObj = new Bitmap(width, height);
>
Graphics graphicsObj = Graphics.FromImage(imageObj);
>
// Create a border in the color of the font
>
//graphicsObj.FillRectangle(new SolidBrush(_FontColor), 0, 0, width,
height);
>
// Create a LightBlue background
>
graphicsObj.FillRectangle(new SolidBrush(_BackgroundColor), 0, 0, width,
height);
>
// Specify the font and alignment
>
Font fontBanner = new Font(_FontFace, _FontSize, _FontStyle );
>
// center align the advertising pitch
>
StringFormat stringFormat = new StringFormat();
>
stringFormat.Alignment = StringAlignment.Center;
>
stringFormat.LineAlignment = StringAlignment.Center;
>
// Draw the adverising pitch
>
graphicsObj.DrawString(Text, fontBanner, new SolidBrush(_FontColor), new
Rectangle(0, 0, width, height), stringFormat);
>
return imageObj;
>
}
>
>
>
Then save it as gif
>
imMail.Save(imFileName, System.Drawing.Imaging.ImageFormat.Gif);
>
imMail.Dispose();
>
Then reload and convert it to transparent background
>
imMail= new Bitmap(imFileName);
>
Bitmap bm = img.Convert2Gif(imMail);
>
if (bm != null)
>
{
>
imMail.Dispose();
>
System.IO.File.Delete(imFileName);
>
bm.Save(imFileName, System.Drawing.Imaging.ImageFormat.Gif);
>
bm.Dispose();
>
}
>
>
>
Here is the convert
>
imMail= new Bitmap(imFileName);
>
>

WT
Guest
 
Posts: n/a
#5: May 20 '07

re: Noway to create a transparent gif with gdi !


Here it is, so any sugestion ?

public Bitmap Convert2Gif(Bitmap _gifImage)

{

//Creates a new GIF image with a modified colour palette

ColorPalette cp = _gifImage.Palette;

if (cp == null)

{

System.Diagnostics.Trace.WriteLineIf(CoreTraceSwit ch.Sw.TraceVerbose,
"Text2Image Convert2Gif ColorPalette null");

return null;

}

//Create a new 8 bit per pixel image

Bitmap bm = new Bitmap(_gifImage.Width,
_gifImage.Height,PixelFormat.Format8bppIndexed);

//get it's palette

ColorPalette ncp = bm.Palette;

Trace.WriteLineIf(CoreTraceSwitch.Sw.TraceError, string.Format("Text2Image
Convert2Gif cp: {0}, ncp: {1}",
cp.Entries.GetLength(0),ncp.Entries.GetLength(0))) ;

//copy all the entries from the old palette removing any transparency

for (int i = 0; i < cp.Entries.GetLength(0); i++)

{

int alpha = (cp.Entries[i] == BackGroundColor) ? 0 : 255;

ncp.Entries[i] = Color.FromArgb(alpha, cp.Entries[i]);

}



//re-insert the palette

bm.Palette = ncp;

if (!TransferBytes(_gifImage, bm))

{

Trace.WriteLineIf(CoreTraceSwitch.Sw.TraceVerbose, "Text2Image Convert2Gif
TransferBytes null");

bm.Dispose();

return null;

}

return bm;

}

bool TransferBytes( Bitmap SrcImg, Bitmap DstImg)

{

bool ret = false;

//lock the source and destination bits

BitmapData srcData = SrcImg.LockBits(new Rectangle(0, 0, SrcImg.Width,
SrcImg.Height), ImageLockMode.ReadOnly, SrcImg.PixelFormat);

BitmapData dstData = DstImg.LockBits(new Rectangle(0, 0, DstImg.Width,
DstImg.Height), ImageLockMode.WriteOnly, DstImg.PixelFormat);

try

{

for (int y = 0; y < SrcImg.Height; y++)

for (int x = 0; x < SrcImg.Width; x++)

{

// Calculate the offsets of the next byte to read/write.

int srcOffset = srcData.Stride * y + x;

int dstOffset = dstData.Stride * y + x;

// Read the next byte from the source bitmap.

Byte b = Marshal.ReadByte(srcData.Scan0, srcOffset);

// Write the byte to the destination bitmap.

Marshal.WriteByte(dstData.Scan0, dstOffset, b);

}

ret = true;

}

catch (Exception ex)

{

Trace.WriteLineIf(CoreTraceSwitch.Sw.TraceError, string.Format("Text2Image
TransfertBytes ex: {0}", ex));

}

finally

{

// Cleanup (unlock the bits of the bitmaps).

SrcImg.UnlockBits(srcData);

DstImg.UnlockBits(dstData);

}

return ret;

}


"Alexey Smirnov" <alexey.smirnov@gmail.coma écrit dans le message de news:
1179657673.291983.152080@k79g2000hse.googlegroups. com...
Quote:
On May 20, 1:05 am, "WT" <W...@newsgroups.nospamwrote:
Quote:
>Then reload and convert it to transparent background
>>
>imMail= new Bitmap(imFileName);
>>
>Bitmap bm = img.Convert2Gif(imMail);
>>
>
You didn't sent the code for transparent background.
>

Steven Cheng[MSFT]
Guest
 
Posts: n/a
#6: May 21 '07

re: Noway to create a transparent gif with gdi !


Hi CS,

Here are some web article demonstrate some code on creating transparent GIF
image, you can also have a look for reference:

http://www.bobpowell.net/giftransparency.htm

http://authors.aspalliance.com/chris...rentgif.vb.asp

http://www.thescripts.com/forum/thread551890.html

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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

Closed Thread