简体   繁体   中英

How can I convert image on the hard disk png to be transparent?

private void ConvertImagestoTransparent(string filename)
{
    var image = new Bitmap(filename, PixelFormat.Format32bppArgb);
    using (var g = Graphics.FromImage(image))
    {
        g.DrawLine(Pens.Red, 0, 0, 135, 135);
    }
}

This give me error the new Bitmap is not getting filename:

Error 2 Argument 2: cannot convert from 'System.Drawing.Imaging.PixelFormat' to 'bool'

Also not getting Bitmap.

In the constructor:

DirectoryInfo d = new DirectoryInfo(@"C:\temp\images\");
Files = d.GetFiles("*.png");

Is it possible to convert the png images to transparent ? Will it lost quality ?

The Bitmap constructor you're using is only for loading existing images. If you want to create one, then you need to use this one like this:

var image = new Bitmap(theWidth, theHeight, PixelFormat.Format32bppArgb);

Then, you can save your image to a file like this:

image.Save(filename);

For your 2nd question, what do you mean by "convert the png images to transparent"? Do you want to remove the background color from the images?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM