473,378 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Noway to create a transparent gif with gdi !

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);
May 19 '07 #1
5 4153
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:eQ*************@TK2MSFTNGP06.phx.gbl...
>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);


May 20 '07 #2
On May 20, 1:05 am, "WT" <W...@newsgroups.nospamwrote:
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.

May 20 '07 #3
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:eQ*************@TK2MSFTNGP06.phx.gbl...
>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);


May 20 '07 #4
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(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" <al************@gmail.coma écrit dans le message de news:
11**********************@k79g2000hse.googlegroups. com...
On May 20, 1:05 am, "WT" <W...@newsgroups.nospamwrote:
>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.

May 20 '07 #5
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.

May 21 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: 2003et | last post by:
How To Create Transparent Windows Controls? Thanks
3
by: °Ë´óɽÈË | last post by:
HI, I create a custom transparent control, set the style is ControlStyles.DoubleBuffer, It display black background. How to create a double buffer transparent control?
2
by: Peter Steele | last post by:
Visual Studio provides an icon editor but I can't figure out how to create a transparent icon. My app always shows the full 32x32 square, with a wide background instead of a transparent one. How do...
1
by: Efkas | last post by:
My application have some level : 1. MyButton class with Label inheritance 2. MyComponent as User Control loading and positionning some of MyButtons 3. MyApp loading and positionning MyComponent ...
2
by: Willie jan | last post by:
Hi, i try to create a bitmap with transparent regions, but it does not work. the transparent region turns out to be black. anybody some ideas?? Dim bmap As...
4
by: ray well | last post by:
in my app i need to make a RichTextbox control transparent. i need it to be a like a pane of glass lying on a sheet of paper, where u can see everything on the sheet of paper not covered by text...
3
by: Ananas | last post by:
Hi, I have created a custom control. It's a rectangle with transparent background. It's possible to see the container through the control. But when controls intersect one another then it's not...
9
by: pek | last post by:
Here is what I want: I have an image, let's say a JPEG 800x600... I want to create a transparent 128x128 PNG image and then resize the 800x600 to fit in the center of my transparent 128x128 PNG.....
2
by: neovantage | last post by:
Hey all, I have created transparent PNG images from text dynamically. But it edges are pixel-ate or we can say edges are distorted. Here is my LINK which shows my generated transparent PNG image....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.