Solved

Load a PNG (8-bit color depth) file without transparency and save it with transparency while preserving the color depth

Posted on 2004-04-22
7
1,091 Views
Last Modified: 2011-09-20
I am trying to add transparency to a PNG image (8-bit color depth) and save it with transparency set while preserving color depth. I could set the transparency and save it using the following code fragment:

      // Load the image from the file into a bitmap
      Bitmap bMap = new Bitmap("map.png");

      // Set white as the transparent color
      bMap.MakeTransparent(Color.White);

      // Save to the PNG format
      ImageFormat imgFormat = ImageFormat.Png;
      bMap.Save("maptst.png",imgFormat);

The problem with this code is that the resulting PNG file has a 24-bit color depth.

I tried it also using the following code fragment, but that resulted in a copy of the original image without transparency (even though the palette did change)

      // Load the image from the file into a bitmap
      Bitmap bMap = new Bitmap("map.png");

      // Modify the transparency in the color palette
      ColorPalette pal = bMap.Palette;
      for (int i = 0; i < pal.Entries.Length; i++)
      {
            // White should be transparent
            if (pal.Entries[i] == Color.FromArgb(255,255,255,255))
                  pal.Entries[i] = Color.FromArgb(0,255,255,255);
      }
      bMap.Palette = pal;

      // Save to the PNG format
      ImageFormat imgFormat = ImageFormat.Png;
      bMap.Save("maptst.png",imgFormat);


Thoughts?

Pjot ;-)
0
Comment
Question by:pvandermeulen
  • 3
7 Comments
 
LVL 4

Expert Comment

by:caner_elci
ID: 10894608
When you add transparency it should be 24-bit image.. Since ARGB (each 8-bit) will be 8x4 = 24-bit
There is no 8-bit transparent (at least with alpha channel) PNG as far as I know.. it can be GIF, but not PNG..
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10894613
hahaha.. sorry.. of course 8x4 is not equal to 24 :)
but it should be at least 24-bit anyway :)
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10895290
0
 

Author Comment

by:pvandermeulen
ID: 10901348
Caner,

You are right, there is no full alpha channel transparency for 8-bit PNG images. But, there is something tRNS chunk which you can add to specify simple transparency... But how is this accomplished using the .NET framework?

Pjot ;-)
0
 
LVL 4

Accepted Solution

by:
caner_elci earned 500 total points
ID: 10909807
Well, as far as I know there is no way to do that.. but maybe someone else knows a way?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
2 questions 10 29
MediaHelp 4 26
HTML - Color not displaying correctly in EMAIL. 6 49
length of the password hash sha1:64000 to set sql field property. 13 66
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question