Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
1,130 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 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