Tech Off Thread

6 posts

Bitmap.MakeTransparent

Back to Forum: Tech Off
  • User profile image
    qwert231

    System.Drawing.Bitmap outBmp;
    string FilePath = MapPath(Request.QueryString["File"]);
    sigImg = System.Drawing.Image.FromFile(FilePath);
    outBmp =
    new System.Drawing.Bitmap(sigImg);

    // Sending Response GIF type to the browser.
    Response.ContentType = "image/gif";
    outBmp.MakeTransparent(System.Drawing.Color.White);
    //(outBmp.GetPixel(1,1));

    outBmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

    // Disposing the objects.
    outBmp.Dispose();

    Why would the resulting image have black wherever white appears in the image?

  • User profile image
    ScanIAm

    qwert231 wrote:
    

    System.Drawing.Bitmap outBmp;
    string FilePath = MapPath(Request.QueryString["File"]);
    sigImg = System.Drawing.Image.FromFile(FilePath);
    outBmp =
    new System.Drawing.Bitmap(sigImg);

    // Sending Response GIF type to the browser.
    Response.ContentType = "image/gif";
    outBmp.MakeTransparent(System.Drawing.Color.White);
    //(outBmp.GetPixel(1,1));

    outBmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

    // Disposing the objects.
    outBmp.Dispose();

    Why would the resulting image have black wherever white appears in the image?


    Just a guess (seriously, a guess), but does


    MakeTransparent(System.Drawing.Color.White);
     
    make everything that matches System.Drawing.Color.White in the image transparent?

    And if so, is your background (behind the image) black Smiley

  • User profile image
    qwert231

    The background is gray. The image is Red and White. All the white in the image turns to black.

  • User profile image
    W3bbo

    qwert231 wrote:
    The background is gray. The image is Red and White. All the white in the image turns to black.


    Is the Transparent key color part of the GIF's color pallette? If not, it isn't going to work and GDI+ will just use the closest color. Are you using any form of quantization on the image either?

    Save the image to disk, then open it in Photoshop (in Indexed Color mode) and take a look at the color table. If the Transparency Key color doesn't turn up, you're not quantizing the image properly.

  • User profile image
    cheong

    This is a limitation of GDI+ library. You have to manually change a particular color in the palette to make it transparent.

    See this entry in the GDIPlus FAQ.

    Recent Achievement unlocked: Code Avenger Tier 4/6: You see dead program. A lot!
    Last modified
  • User profile image
    mjda

    See
    http://www.nathansokalski.com/code/TransparencyClass.aspx
    It has a class used to add transparency to a System.Drawing.Bitmap object

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.