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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

17 Experts available now in Live!

Get 1:1 Help Now