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,072 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now