473,806 Members | 2,790 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(strin g Text, SizeF Sf)

{

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

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

Bitmap imageObj = new Bitmap(width, height);

Graphics graphicsObj = Graphics.FromIm age(imageObj);

// Create a border in the color of the font

//graphicsObj.Fil lRectangle(new SolidBrush(_Fon tColor), 0, 0, width,
height);

// Create a LightBlue background

graphicsObj.Fil lRectangle(new SolidBrush(_Bac kgroundColor), 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.Al ignment = StringAlignment .Center;

stringFormat.Li neAlignment = StringAlignment .Center;

// Draw the adverising pitch

graphicsObj.Dra wString(Text, fontBanner, new SolidBrush(_Fon tColor), new
Rectangle(0, 0, width, height), stringFormat);

return imageObj;

}

Then save it as gif

imMail.Save(imF ileName, System.Drawing. Imaging.ImageFo rmat.Gif);

imMail.Dispose( );

Then reload and convert it to transparent background

imMail= new Bitmap(imFileNa me);

Bitmap bm = img.Convert2Gif (imMail);

if (bm != null)

{

imMail.Dispose( );

System.IO.File. Delete(imFileNa me);

bm.Save(imFileN ame, System.Drawing. Imaging.ImageFo rmat.Gif);

bm.Dispose();

}

Here is the convert

imMail= new Bitmap(imFileNa me);
May 19 '07 #1
5 4177
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******** *****@TK2MSFTNG P06.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(strin g Text, SizeF Sf)

{

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

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

Bitmap imageObj = new Bitmap(width, height);

Graphics graphicsObj = Graphics.FromIm age(imageObj);

// Create a border in the color of the font

//graphicsObj.Fil lRectangle(new SolidBrush(_Fon tColor), 0, 0, width,
height);

// Create a LightBlue background

graphicsObj.Fil lRectangle(new SolidBrush(_Bac kgroundColor), 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.Al ignment = StringAlignment .Center;

stringFormat.Li neAlignment = StringAlignment .Center;

// Draw the adverising pitch

graphicsObj.Dra wString(Text, fontBanner, new SolidBrush(_Fon tColor), new
Rectangle(0, 0, width, height), stringFormat);

return imageObj;

}

Then save it as gif

imMail.Save(imF ileName, System.Drawing. Imaging.ImageFo rmat.Gif);

imMail.Dispose( );

Then reload and convert it to transparent background

imMail= new Bitmap(imFileNa me);

Bitmap bm = img.Convert2Gif (imMail);

if (bm != null)

{

imMail.Dispose( );

System.IO.File. Delete(imFileNa me);

bm.Save(imFileN ame, System.Drawing. Imaging.ImageFo rmat.Gif);

bm.Dispose();

}

Here is the convert

imMail= new Bitmap(imFileNa me);


May 20 '07 #2
On May 20, 1:05 am, "WT" <W...@newsgroup s.nospamwrote:
Then reload and convert it to transparent background

imMail= new Bitmap(imFileNa me);

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******** *****@TK2MSFTNG P06.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(strin g Text, SizeF Sf)

{

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

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

Bitmap imageObj = new Bitmap(width, height);

Graphics graphicsObj = Graphics.FromIm age(imageObj);

// Create a border in the color of the font

//graphicsObj.Fil lRectangle(new SolidBrush(_Fon tColor), 0, 0, width,
height);

// Create a LightBlue background

graphicsObj.Fil lRectangle(new SolidBrush(_Bac kgroundColor), 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.Al ignment = StringAlignment .Center;

stringFormat.Li neAlignment = StringAlignment .Center;

// Draw the adverising pitch

graphicsObj.Dra wString(Text, fontBanner, new SolidBrush(_Fon tColor), new
Rectangle(0, 0, width, height), stringFormat);

return imageObj;

}

Then save it as gif

imMail.Save(imF ileName, System.Drawing. Imaging.ImageFo rmat.Gif);

imMail.Dispose( );

Then reload and convert it to transparent background

imMail= new Bitmap(imFileNa me);

Bitmap bm = img.Convert2Gif (imMail);

if (bm != null)

{

imMail.Dispose( );

System.IO.File. Delete(imFileNa me);

bm.Save(imFileN ame, System.Drawing. Imaging.ImageFo rmat.Gif);

bm.Dispose();

}

Here is the convert

imMail= new Bitmap(imFileNa me);


May 20 '07 #4
WT
Here it is, so any sugestion ?

public Bitmap Convert2Gif(Bit map _gifImage)

{

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

ColorPalette cp = _gifImage.Palet te;

if (cp == null)

{

System.Diagnost ics.Trace.Write LineIf(CoreTrac eSwitch.Sw.Trac eVerbose,
"Text2Image Convert2Gif ColorPalette null");

return null;

}

//Create a new 8 bit per pixel image

Bitmap bm = new Bitmap(_gifImag e.Width,
_gifImage.Heigh t,PixelFormat.F ormat8bppIndexe d);

//get it's palette

ColorPalette ncp = bm.Palette;

Trace.WriteLine If(CoreTraceSwi tch.Sw.TraceErr or, string.Format(" Text2Image
Convert2Gif cp: {0}, ncp: {1}",
cp.Entries.GetL ength(0),ncp.En tries.GetLength (0)));

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

for (int i = 0; i < cp.Entries.GetL ength(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.WriteLine If(CoreTraceSwi tch.Sw.TraceVer bose, "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.R eadOnly, SrcImg.PixelFor mat);

BitmapData dstData = DstImg.LockBits (new Rectangle(0, 0, DstImg.Width,
DstImg.Height), ImageLockMode.W riteOnly, DstImg.PixelFor mat);

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.ReadByt e(srcData.Scan0 , srcOffset);

// Write the byte to the destination bitmap.

Marshal.WriteBy te(dstData.Scan 0, dstOffset, b);

}

ret = true;

}

catch (Exception ex)

{

Trace.WriteLine If(CoreTraceSwi tch.Sw.TraceErr or, string.Format(" Text2Image
TransfertBytes ex: {0}", ex));

}

finally

{

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

SrcImg.UnlockBi ts(srcData);

DstImg.UnlockBi ts(dstData);

}

return ret;

}
"Alexey Smirnov" <al************ @gmail.coma écrit dans le message de news:
11************* *********@k79g2 00...legr oups.com...
On May 20, 1:05 am, "WT" <W...@newsgroup s.nospamwrote:
>Then reload and convert it to transparent background

imMail= new Bitmap(imFileNa me);

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
6675
by: 2003et | last post by:
How To Create Transparent Windows Controls? Thanks
3
8070
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
19489
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 I get it to look transparent?
1
20185
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 I prefer don't insert a background in MyComponent, and using the MyApp one as general background with MyComponent positionned, but I am not able to have MyComponent background transparent.
2
2076
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 Bitmap(100,100,Imaging.PixelFormat.Format32bppPArgb) Dim g As Graphics = Graphics.FromImage(bmap) g.Clear(Color.Transparent)
4
9378
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 written on the glass pane. i need to be able to see the control or form that is underneath the RichTextbox, and at the same time to be able to write and erase text to the RichTextbox. i'm assuming that it is the backcolor that hides what is...
3
4570
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 possible to see the control lying under another. Please, give me an idea how to create a transparent controls? Is anywhere any useful examples located?
9
4461
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.. Can you help me/point me to the right direction..? Thank you very much in advance..
2
3485
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. Can someone help me out to sort out my problem i will be very grateful to him as i am stuck bad with this issue and i am not figuring out how to fix this. Here is my Code which generate transparent PNG images <img...
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9599
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.