Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove transparency in images with C#

does anyone know a smooth / fast way of removing transparency from e.g. pngs/tiffs etc and replacing it with a white background?

Basically what I need this for is I need to create PDF/A compatible images, which may, according to the spec, have -no- transparency (and therefore a fixed white background is fine).

Any ideas / suggestions?

Cheers & thanks, -Jörg

like image 401
Jörg Battermann Avatar asked Mar 06 '09 09:03

Jörg Battermann


People also ask

How do you make a transparent background in CSS?

The CSS for this is opacity:1;. When the mouse pointer moves away from the image, the image will be transparent again. An example of reversed hover effect: When using the opacity property to add transparency to the background of an element, all of its child elements inherit the same transparency.

How to change opacity/transparency of an element using CSS?

The opacity property specifies the opacity/transparency of an element. The opacity property can take a value from 0.0 - 1.0. The lower value, the more transparent: The opacity property is often used together with the :hover selector to change the opacity on mouse-over: The first CSS block is similar to the code in Example 1.

How to make an image transparent when the mouse pointer moves away?

The CSS for this is opacity:1;. When the mouse pointer moves away from the image, the image will be transparent again. An example of reversed hover effect:

What is trans transparency in CSS?

Transparency plays an important role in front end development. It lets you choose how transparent the elements on your web pages appear. You can adjust transparency in several ways – because of course, in CSS, there are multiple ways to do the same thing.


1 Answers

You have to remove the alpha channel. Otherwise you'll still have a transparent image - just without transparent areas.

class Program
{
    static void Main(string[] args)
    {
        //this also works for different file formats
        ReplaceTransparency(@"C:\Y\transparent.png", System.Drawing.Color.White).Save(@"C:\Y\no_transparency.png");
        ReplaceTransparency(@"C:\Y\transparent.gif", System.Drawing.Color.White).Save(@"C:\Y\no_transparency.gif");
    }

    public static System.Drawing.Bitmap ReplaceTransparency(string file, System.Drawing.Color background)
    {
        return ReplaceTransparency(System.Drawing.Image.FromFile(file), background);
    }

    public static System.Drawing.Bitmap ReplaceTransparency(System.Drawing.Image image, System.Drawing.Color background)
    {
        return ReplaceTransparency((System.Drawing.Bitmap)image, background);
    }

    public static System.Drawing.Bitmap ReplaceTransparency(System.Drawing.Bitmap bitmap, System.Drawing.Color background)
    {
        /* Important: you have to set the PixelFormat to remove the alpha channel.
         * Otherwise you'll still have a transparent image - just without transparent areas */
        var result = new System.Drawing.Bitmap(bitmap.Size.Width, bitmap.Size.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        var g = System.Drawing.Graphics.FromImage(result);

        g.Clear(background);
        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
        g.DrawImage(bitmap, 0, 0);

        return result;
    }
}
like image 87
pr0gg3r Avatar answered Oct 06 '22 23:10

pr0gg3r