How can I make an Image tranparent
-
Please help me out. I have an image and want to make it transparent. I didn't know how I will do this any idea will be helpful for me. Thanks in advance.
First create a buffer image of the same size and resolution as the source image. Then draw the source image on the buffer, specifying the transparent color using the ImageAttributes. Something like this:
Image image = Image.FromFile(@"c:\logo.png");
Bitmap buffer = new Bitmap(image.Width, image.Height);
buffer.SetResolution(image.HorizontalResolution,image.VerticalResolution);Rectangle destRect = new Rectangle(0, 0, image.Width, image.Height);
ImageAttributes attributes = new ImageAttributes();
// Set the transparent color
attributes.SetColorKey(Color.Red, Color.Red);using (Graphics gr = Graphics.FromImage(buffer)) {
gr.DrawImage(image, destRect, 0, 0, image.Width,
image.Height, GraphicsUnit.Pixel, attributes);
}buffer.Save(@"c:\logo2.png");
Regards, Lev
-
First create a buffer image of the same size and resolution as the source image. Then draw the source image on the buffer, specifying the transparent color using the ImageAttributes. Something like this:
Image image = Image.FromFile(@"c:\logo.png");
Bitmap buffer = new Bitmap(image.Width, image.Height);
buffer.SetResolution(image.HorizontalResolution,image.VerticalResolution);Rectangle destRect = new Rectangle(0, 0, image.Width, image.Height);
ImageAttributes attributes = new ImageAttributes();
// Set the transparent color
attributes.SetColorKey(Color.Red, Color.Red);using (Graphics gr = Graphics.FromImage(buffer)) {
gr.DrawImage(image, destRect, 0, 0, image.Width,
image.Height, GraphicsUnit.Pixel, attributes);
}buffer.Save(@"c:\logo2.png");
Regards, Lev
-
Thanks for your reply. This example very helpful but I want to make transparent whole image not just one or two colors. I hope you can give me a good idea. Regards
Naveed khan nido wrote:
I want to make transparent whole image
What do you mean? if you want the whole image to be transparent, just create a new image object and save it right away. That's not one or two colors, the SetColorKey accepts two color objects to specify the range of colors to make transparent.
Regards, Lev
-
Naveed khan nido wrote:
I want to make transparent whole image
What do you mean? if you want the whole image to be transparent, just create a new image object and save it right away. That's not one or two colors, the SetColorKey accepts two color objects to specify the range of colors to make transparent.
Regards, Lev
Yes I want to transparent whole Image. What do you mean to create an object of image and save it. But I am going to transparent the whole image. the example you given is just removing the desire color. I create an black image and give black color parameter in you example it remove the black color but not showing the other side control. If you can give me hit to properly transplant a single color then I will create a loop for all color. Regards
-
Yes I want to transparent whole Image. What do you mean to create an object of image and save it. But I am going to transparent the whole image. the example you given is just removing the desire color. I create an black image and give black color parameter in you example it remove the black color but not showing the other side control. If you can give me hit to properly transplant a single color then I will create a loop for all color. Regards
Well, let me say again. If you want to make the whole image transparent, this is equal to creating an empty image of the same size and resolution and saving it, i.e. dropping the content of the image. You could have modified the code I've sent pretty easy, but anyway, here it is:
Image img = Image.FromFile(@"c:\logo.png");
Bitmap buffer = new Bitmap(img.Width, img.Height);
buffer.SetResolution(img.HorizontalResolution, img.VerticalResolution);
buffer.Save(@"c:\logo2.png");This will give you the same source image, BUT without the content (I actually wonder what is this for ;))
Regards, Lev
-
Well, let me say again. If you want to make the whole image transparent, this is equal to creating an empty image of the same size and resolution and saving it, i.e. dropping the content of the image. You could have modified the code I've sent pretty easy, but anyway, here it is:
Image img = Image.FromFile(@"c:\logo.png");
Bitmap buffer = new Bitmap(img.Width, img.Height);
buffer.SetResolution(img.HorizontalResolution, img.VerticalResolution);
buffer.Save(@"c:\logo2.png");This will give you the same source image, BUT without the content (I actually wonder what is this for ;))
Regards, Lev
-
You're welcome
Regards, Lev
-
Well, let me say again. If you want to make the whole image transparent, this is equal to creating an empty image of the same size and resolution and saving it, i.e. dropping the content of the image. You could have modified the code I've sent pretty easy, but anyway, here it is:
Image img = Image.FromFile(@"c:\logo.png");
Bitmap buffer = new Bitmap(img.Width, img.Height);
buffer.SetResolution(img.HorizontalResolution, img.VerticalResolution);
buffer.Save(@"c:\logo2.png");This will give you the same source image, BUT without the content (I actually wonder what is this for ;))
Regards, Lev
Lev Danielyan wrote:
I actually wonder what is this for
you can use a glass plate like that to fix a broken window. :laugh:
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
Lev Danielyan wrote:
I actually wonder what is this for
you can use a glass plate like that to fix a broken window. :laugh:
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
Right :laugh: :laugh:
Regards, Lev