I'm using this method to help achieve transparency in a bitmap/image:
I use this method to actually create the transparent bitmap: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; }
Finally, I use this method to save the transparent image to a directory.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; }
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 :PCode:/// <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)); }
I want to save the transparent image in a bitmap. I know it's got to be possible. Does anyone know how?
Thanks




Reply With Quote