I have an app that allows users to create simple images that are no more than text with a desired background color. The users can select a color from a System.Windows.Forms.ColorDialog
and use it to set the text color and the background color.
The background color can be set to transparent (I use Color.Transparent
as the reference for the transparency) and after selection, I update the preview image which displays the text and the transparency correctly. However, when I go to save the image, I can not get the transparency to save with the image as a gif.
I found this article which states that I should use the MakeTransparent
method to set the transaprency color.
Before I call the Save operation, I take the image that is in memory and redraw it using black as the background/transparent color and then before I save the image call the MakeTransperent
method on the in-memory image. Still, the image saves out with black as the background.
What might I be doing wrong?
EDIT: Here is the relevant code.
This is the method that creates the image. The overrideBG
variable is used to specify if we should set the transparency color to a non-alpha color for the gif.
void ReDrawImage(bool overrideBG = false) //My method that draws the image in memory.
{
//My In Memory Image creation
img = new Bitmap(sz.Width, sz.Height);
Graphics gfx = Graphics.FromImage(img);
...
//This portion of code sets the BG color to what should be the transparency color, if the BG is transparent
if (overrideBG)
{
gfx.Clear(TransparentColor); //TransparentColor = Black, unless Text Color is Black. If so, it equals White.
}
else
{
gfx.Clear(BackColorPreview.BackColor);
}
//Followed by code that writes the text.
}
//This is the save method (Assume we are always drawing a transparent background.)
Save()
{
ReDrawImage(true);
img.MakeTransparent(TransparentColor); //I've also tried moving this line before the ReDrawImage call
img.Save(SaveFile.FileName, ImageFormat.Gif);
ReDrawImage();
}