Create an image with text and transparent background RRS feed

  • Question

  • User-224477884 posted
    Hey everyone,
    I'm trying to figure out how to create (with C#) an image with text and transparent background.
    Create an image using GDI+ w/C# that writes text to a .gif image, that has transparent background.
    Any tips of where to look, or a simple example to achieve this effect are most welcome.
    Thanks in advance,
    Monday, April 24, 2006 6:10 PM

All replies

  • User1169324036 posted
    Jaques, Please check out the following link and tell me if this is what you are looking for.


    Tuesday, April 25, 2006 12:39 AM
  • User314661392 posted

    Hey, <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    Since it took me so long to find the solution for a transparent background, <o:p></o:p>

    (For some reason I couldn’t find it in this respectable forum)<o:p></o:p>

    I figured that my time won’t to be more productive if I share with you my findings <o:p></o:p>

    So here there are:<o:p> </o:p>

    The problem: A Dithery, grainy image is produced from the myGifImage.Save, and the transparent background  is actually black<o:p></o:p>

    The Solution: There is a free code that implements a OctreeQuantizer function. This solves the above problem.<o:p></o:p>

    (You’ll need to add the OctreeQuantizer.cs and Quantizer.cs to your App_Code folder in your project )<o:p></o:p>

    And than call it :<o:p></o:p>

            OctreeQuantizer quantizer = new OctreeQuantizer ( 255 , 8 ) ;<o:p></o:p>

    <o:p> </o:p>

            using ( Bitmap quantized = quantizer.Quantize(objBitmap) )<o:p></o:p>


                quantized.Save(FileName, ImageFormat.Gif);<o:p></o:p>


    You can find this code here: http://codebetter.com/blogs/brendan.tompkins/archive/2004/01/26/6103.aspx<o:p></o:p>

    <o:p> </o:p>

    Another problem I had was: using Graphics.DrawString on transparent background you get the ugly font while if the background is not transparent it looks fine.<o:p></o:p>

    The solution: Add a rendering hint just prior to the DrawString statement <o:p></o:p>

    g.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;  // <— This line solved it !
    g.DrawString(text, font, Brushes.Black, new Point(x, 0));<o:p></o:p>

    This is where I find this wonderful solution: http://weblogs.asp.net/israelio/archive/2006/05/06/445428.aspx<o:p></o:p>

    <o:p> </o:p>

    <o:p> </o:p>

    <o:p> </o:p>

    <o:p> </o:p>

    <o:p> </o:p>

    <o:p> </o:p>

    Sunday, June 11, 2006 12:09 PM