|
-
Dec 16th, 2007, 09:40 PM
#1
Thread Starter
Frenzied Member
[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
-
Dec 17th, 2007, 07:40 AM
#2
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.
-
Dec 17th, 2007, 07:46 AM
#3
Thread Starter
Frenzied Member
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.
-
Dec 17th, 2007, 11:46 AM
#4
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.
-
Dec 17th, 2007, 03:02 PM
#5
Thread Starter
Frenzied Member
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.
=(
-
Dec 18th, 2007, 04:13 AM
#6
-
Dec 18th, 2007, 06:07 AM
#7
Thread Starter
Frenzied Member
Re: [2.0] Saving transparent images.
Haha! Exactly what I need and exactly what I'm doing. Thanks.
-
Dec 18th, 2007, 09:30 AM
#8
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|