Noway to create a transparent gif with gdi !

W

WT

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);
 
G

Guest

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.
 
C

Cowboy \(Gregory A. Beamer\)

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.
 
W

WT

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(CoreTraceSwitch.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 == BackGroundColor) ? 0 : 255;

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

}



//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;

}
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top