Transparent Key Color Isssue

I have a simple windows form with an image on it; I have set the transparent key of the form to match the form color rendering it transparent when the program is run.  However sometimes when the color depth is set to 32 bit on any given computer the transparent key color shows up.

Is there any way around this?

schenkpAsked:
Who is Participating?
 
Bob LearnedCommented:
Here is a method that will convert pixel format for an image:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

public class ImageConverter
{

    /// <summary>
    /// Convert an image to the desired pixel format.
    /// </summary>
    /// <returns>Converted image</returns>
    /// <remarks>Create a Bitmap with the desired pixel format, and draw the image using
    /// a temporary Graphics object.
    /// </remarks>
    public static Image ConvertPixelFormat(Bitmap img, PixelFormat format)
    {
        Bitmap bmp = new Bitmap(img.Width, img.Height, format);
        using (Graphics gr = Graphics.FromImage(bmp))
        {
            gr.DrawImage(img, 0, 0);
        }
        return bmp;
    }
}

Example:
            Bitmap img = (Bitmap)Image.FromFile(@"C:\WINDOWS\Web\Wallpaper\Autumn.jpg");
            this.BackgroundImage = ImageConverter.ConvertPixelFormat(img, PixelFormat.Format32bppArgb);

Bob
0
 
p_davisCommented:
what is the color value that you are using argb? can you just convert to a win32 using the color converter?
0
 
schenkpAuthor Commented:
Im just using blue 0,0,255.  
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
schenkpAuthor Commented:
How do i convert it to a win32 using the color converter?
0
 
Bob LearnedCommented:
What is the color depth (pixel format) for the image?  Format32bppArgb?

Bob
0
 
schenkpAuthor Commented:
Not sure what you mean its just a .tif out of Photoshop....  
0
 
Bob LearnedCommented:
Most images are 24-bit Format24bppArgb, and the colors are slightly different between 24 bit and 32 bit images.

Bob
0
 
schenkpAuthor Commented:
Is there anyway to correct this with code?
0
 
WilliamCommented:
As well as what Bob has shown, to make sure you always have the "Actual" Color for your transparency:

        So using his example

  Bitmap img = (Bitmap)Image.FromFile(@"C:\WINDOWS\Web\Wallpaper\Autumn.jpg");
  this.BackgroundImage = ImageConverter.ConvertPixelFormat(img, PixelFormat.Format32bppArgb);
//Set the color using the actual color from the pixel....
  this.TransparencyKey = img.GetPixel( 0 , 0 );
0
 
schenkpAuthor Commented:
When using this method the background image is about four times large than the actual background, thus ony showing part of it.  How do i properly align it so it shows up the proper size?
0
 
WilliamCommented:
try setting the imagelayout prop...


            this.BackgroundImage = yourimage;
            this.BackgroundImageLayout = ImageLayout.Stretch;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.