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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

622 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