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