私はユーザーが希望の背景色を持つテキスト以外の単純なイメージを作成できるようにするアプリを持っています。ユーザーはSystem.Windows.Forms.ColorDialog
から色を選択し、それを使用してテキストの色と背景の色を設定できます。透明な背景でgifを保存するにはどうすればよいですか?
背景色を透明に設定することができます(私は透明度の基準としてColor.Transparent
を使用します)。選択後、テキストと透明度を正しく表示するプレビュー画像を更新します。しかし、私が画像を保存するときには、画像をgifとして保存するための透過性を得ることができません。
私はメソッドを使用して、トランスペアレンシーの色を設定する必要があることを示すthis articleが見つかりました。
保存操作を呼び出す前に、メモリに保存されている画像を背景/透明色として黒で再描画してから、画像を保存する前にメモリ内の画像にMakeTransperent
メソッドを呼び出します。それでも、画像は背景として黒で保存されます。
何が間違っていますか?
EDIT:ここに関連コードがあります。
これはイメージを作成する方法です。 overrideBG
変数は、透明色を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();
}
ようになります。 – lincolnk