Results 1 to 8 of 8

Thread: [2.0] Saving transparent images.

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    [2.0] Saving transparent images.

    I'm using this method to help achieve transparency in a bitmap/image:

    Code:
            /// <summary>
            /// Enables semi-transparency in an image.
            /// </summary>
            /// <param name="transparency">Transparency value between 0.0 and 1.0.</param>
            /// <returns>ImageAttributes.</returns>
            private static ImageAttributes GetTransparentAttributes(float transparency)
            {
                if (transparency < 0 || transparency > 1)
                {
                    throw new ArgumentOutOfRangeException("Value must be between 0.0 and 1.0.");
                }
    
                ColorMatrix colorMatrix = new ColorMatrix(
                new float[][] { 
                new float[] {1, 0, 0, 0, 0},
                new float[] {0, 1, 0, 0, 0},
                new float[] {0, 0, 1, 0, 0},
                new float[] {0, 0, 0, transparency, 0},
                new float[] {0, 0, 0, 0, 1}});
    
                ImageAttributes imageAttributes = new ImageAttributes();
                imageAttributes.SetColorMatrix(colorMatrix);
    
                return imageAttributes;
            }
    I use this method to actually create the transparent bitmap:

    Code:
            /// <summary>
            /// Creates an image based on the image and transparency given.
            /// </summary>
            /// <param name="directory">The directory of the bitmap to be made into a cursor.</param>
            /// <param name="transparency">Transparency value between 0.0 and 1.0.</param>
            /// <returns>Image.</returns>
            private static Image Create(string directory, float transparency)
            {
                Bitmap sourceBitmap = new Bitmap(directory);
                Bitmap tempBitmap = new Bitmap(sourceBitmap.Width, sourceBitmap.Height);
                Graphics myGraphics = Graphics.FromImage(tempBitmap);
                ImageAttributes imageAttributes = GetTransparentAttributes(transparency);
    
                myGraphics.DrawImage(sourceBitmap, new Rectangle(0, 0, tempBitmap.Width, tempBitmap.Height), 0, 0, tempBitmap.Width, tempBitmap.Height, GraphicsUnit.Pixel, imageAttributes);
    
                return tempBitmap;
            }
    Finally, I use this method to save the transparent image to a directory.

    Code:
            /// <summary>
            /// Saves an image with a different file format.
            /// </summary>
            /// <param name="directory">The directory of the image to save as a different file format.</param>
            /// <param name="extension">The new extension (with a leading period).</param>
            public static void Save(string inputDirectory, string outputDirectory, string newExtension, float newTransparency)
            {
                Image image = Create(inputDirectory, newTransparency);
                image.Save(Path.ChangeExtension(outputDirectory, newExtension));
            }
    However, when opening the image in Microsoft Paint or any other photo editing programs, the image is either not transparent at all, corrupt, or just normal. I think this is happening because BMP's cannot be saved as transparent, so I am asking if there is a way to save the pixels of the transparent image so that it will look transparent even if the image isn't transprent. It's a bit hard to word that :P

    I want to save the transparent image in a bitmap. I know it's got to be possible. Does anyone know how?

    Thanks

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [2.0] Saving transparent images.

    MS paint is no indication of transparency. Don't rely on it. Use Gimp or something else.

    Why can't you save it as a PNG instead?
    I don't live here any more.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Saving transparent images.

    Because the third parameter for SystemParametersInfo takes only filenames of bitmap files. If I supply some random bitmap I made in paint as the parameter, it works fine, but if I supply the file made via my program, it doesn't do anything. Apparently the image is either corrupt, unreadable, or BMP's do not support transparency or something.

  4. #4
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    Re: [2.0] Saving transparent images.

    That's right, bitmaps don't support transparency.

    I mean, yes, as you said it's "possible" the format technically *could*, but I've never seen a transparent bitmap, have you? You should save as PNG as the cat has suggested.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Saving transparent images.

    Wouldn't really work =/ The API call I'm trying to use requires the filename of a bitmap file.

    =(

  6. #6
    Lively Member Zolomon's Avatar
    Join Date
    Oct 2007
    Location
    Sweden
    Posts
    104

    Talking Re: [2.0] Saving transparent images.

    Hello!

    I know how to convert images to BMP. I don't have the functions at my current place but I'll gladly post them when I get home! I'm currently writing a WallPaper changer application and I found a solution to this problem (that is, how to convert an image to BMP so you can use it with SystemParametersInfo())

    Cheers!
    //Zolomon

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] Saving transparent images.

    Haha! Exactly what I need and exactly what I'm doing. Thanks.

  8. #8
    Lively Member Zolomon's Avatar
    Join Date
    Oct 2007
    Location
    Sweden
    Posts
    104

    Smile Re: [2.0] Saving transparent images.

    I'll post this link for you, you'll learn a lot more from there!

    Cheers!
    //Zolomon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width