有没有人知道一个平滑/快速的方法来删除透明度,例如pngs/tiffs等,并用白色背景替换它?
基本上我需要的是我需要创建PDF/A兼容图像,根据规范,它可能具有-no-透明度(因此固定的白色背景很好).
有什么想法/建议吗?
干杯和谢谢,-Jörg
您可以创建一个与png大小相同的位图,绘制一个白色矩形,然后在其上绘制图像.
void RemTransp(string file) { Bitmap src = new Bitmap(file); Bitmap target = new Bitmap(src.Size.Width,src.Size.Height); Graphics g = Graphics.FromImage(target); g.DrawRectangle(new Pen(new SolidBrush(Color.White)), 0, 0, target.Width, target.Height); g.DrawImage(src, 0, 0); target.Save("Your target path"); }
您必须删除Alpha通道.否则你仍然会有一个透明的图像 - 只是没有透明的区域.
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; } }