Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6077645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:44:39+00:00 2026-05-23T10:44:39+00:00

I have an app that allows users to create simple images that are no

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:44:40+00:00Added an answer on May 23, 2026 at 10:44 am

    Apparently there are issues with MakeTransparent and gifs. I found this post which provides and alternative by way of modifying the image bytes in memory. Here’s the code sample from the post with syntax errors fixed.

    /// <summary>  
    /// Returns a transparent background GIF image from the specified Bitmap.  
    /// </summary>  
    /// <param name="bitmap">The Bitmap to make transparent.</param>  
    /// <param name="color">The Color to make transparent.</param>  
    /// <returns>New Bitmap containing a transparent background gif.</returns>  
    public static Bitmap MakeTransparentGif(Bitmap bitmap, Color color)
    {
        byte R = color.R;
        byte G = color.G;
        byte B = color.B;
        MemoryStream fin = new MemoryStream();
        bitmap.Save(fin, System.Drawing.Imaging.ImageFormat.Gif);
        MemoryStream fout = new MemoryStream((int)fin.Length);
        int count = 0;
        byte[] buf = new byte[256];
        byte transparentIdx = 0;
        fin.Seek(0, SeekOrigin.Begin);
        //header  
        count = fin.Read(buf, 0, 13);
        if ((buf[0] != 71) || (buf[1] != 73) || (buf[2] != 70)) return null; //GIF  
        fout.Write(buf, 0, 13);
        int i = 0;
        if ((buf[10] & 0x80) > 0)
        {
            i = 1 << ((buf[10] & 7) + 1) == 256 ? 256 : 0;
        }
        for (; i != 0; i--)
        {
            fin.Read(buf, 0, 3);
            if ((buf[0] == R) && (buf[1] == G) && (buf[2] == B))
            {
                transparentIdx = (byte)(256 - i);
            }
            fout.Write(buf, 0, 3);
        }
        bool gcePresent = false;
        while (true)
        {
            fin.Read(buf, 0, 1);
            fout.Write(buf, 0, 1);
            if (buf[0] != 0x21) break;
            fin.Read(buf, 0, 1);
            fout.Write(buf, 0, 1);
            gcePresent = (buf[0] == 0xf9);
            while (true)
            {
                fin.Read(buf, 0, 1);
                fout.Write(buf, 0, 1);
                if (buf[0] == 0) break;
                count = buf[0];
                if (fin.Read(buf, 0, count) != count) return null;
                if (gcePresent)
                {
                    if (count == 4)
                    {
                        buf[0] |= 0x01;
                        buf[3] = transparentIdx;
                    }
                }
                fout.Write(buf, 0, count);
            }
        }
        while (count > 0)
        {
            count = fin.Read(buf, 0, 1);
            fout.Write(buf, 0, 1);
        }
        fin.Close();
        fout.Flush();
        return new Bitmap(fout);
    }
    

    I tried it with Hans’ sample (replacing gif parts) and it works for me.

    Your code would look like

    img = MakeTransparent(img, TransparentColor); 
    img.Save(SaveFile.FileName, ImageFormat.Gif);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a simple django app that allows users to create posts
I have an existing web app that allows users to rate items based on
We have a WPF app that allows our users to download encrypted content and
I have an app that allows you to create Home shortcuts to a specific
Background: I am trying to create a really simple iPhone app that will allow
I am building a video streaming app that allows users to create playlists and
I have a blog-style app that allows users to tag each post with topics.
I have an app that allows the user to choose an image, at design
I have a Flex App that allows you to dynamically build an animation. It
I have a SQL interface page in my classic ASP web app that allows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.