Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

saving transparent images in ASP.net 2.0 C#

Status
Not open for further replies.

toptomato

Programmer
Jul 22, 2005
62
US
Hi folks,
Thanks in advance to all of you geniouses.
Here is what I am doing:
This is an ASP.net 2.0 with C# problem.
I am reading an image file (either: gif, jpg or png of any dimension) as such:
Image rImg = Image.FromFile(RawFilePath)
Next I create a Bitmap and extract graphic as such:
Bitmap wImage = new Bitmap(200, 200, PixelFormat.Format24bppRgb)
Graphics g = Graphics.FromImage(wImage)
Next I draw the Image in the graphic using only 50x50 square from the topleft corner of the rImg as such:
g.DrawImage(rImg, new Rectangle(0, 0, 200, 200), new Rectangle(0,0,50,50), GraphicsUnit.Pixel);
Finally I save the bitmap to disk as such:
wImg.Save(filePath, ImageFormat.Png);

Now here is the problem:
By design I can accept any of the three types of images (gif, png, jpg) and of any dimension. I must also by design only select the 50x50 square from the topleft corner and save as 200x200. Now here is the issue: My objective is to preserve transperencey, especially in case of png files. Right now when I try to read a PNG that has transperency it save the transparent part of the image as black. How can I have it save the transaparent part as transparent? Thanks guys
 
Please don't bump threads (especially as the post has only been up two hours). Tek-Tips isn't a help desk and people will reply when and if they want to.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
thanks, ca8msm, your links were helpful however the issue remains the same. I was able to make the images transparent however when saved as png the black background remain. I was also able to change the black background to any other color by g.Clear(Color.Blue); before drawing the image (in this case blue background). However trying this g.Clear(Color.Transparent) doesn't take effect and the black background returns.

Apparently the transparncy works when drawing the image (it sets the desired portion transparent by following the instruction in the links from previous posts). However, when you save it the background comes back.
wImage.Save(filePath,WebImageFormat.Png);
 
From reading this:


I came across this article which shows a demo in a VB.NET app. You should be able to go through this and pick out the relevant info:



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
thanks again ca8msm,
i had minor success.
i was able to get the transperency however, it only seems to be processing only the first quarter of an image from left. I will keep working at it. in the mean time, any suggestion is welcome
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top