用c#在位图的顶部平坦PNG

本文关键字:PNG 顶部 位图 | 更新日期: 2023-09-27 18:13:53

我试图在一个纯色位图上平铺一个透明的png。

到目前为止我有这个

    using (System.Drawing.Image backImage = System.Drawing.Image.FromFile(layer1imagename))
    using (System.Drawing.Image frontImage = System.Drawing.Image.FromFile(HttpContext.Current.Server.MapPath("~/") + layer2))
    using (Image IMG1 = new Bitmap(251, 400))
    using (Graphics compositeGraphics = Graphics.FromImage(IMG1))
    {
        compositeGraphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
        compositeGraphics.DrawImageUnscaled(backImage, 0, 0);
        compositeGraphics.DrawImageUnscaled(frontImage, 0, 0);
        compositeGraphics.Dispose();
        frontImage.Dispose();
        backImage.Dispose();
        IMG1.Save(layer1imagename, System.Drawing.Imaging.ImageFormat.Png);
    }

然而,这将删除顶部图层的透明度。我如何保留一个透明的png在顶部?

用c#在位图的顶部平坦PNG

change using (Image IMG1 = new Bitmap(251, 400)) to using (Image IMG1 = new Bitmap(251, 400, PixelFormat.Format32bppArgb)) .

有关MSDN参考,请参阅:

  • http://msdn.microsoft.com/en-us/library/3z132tat.aspx
  • http://msdn.microsoft.com/en-us/library/system.drawing.imaging.pixelformat.aspx

编辑-按注释:

要绘制你想要的PNG,你需要使用不同的CompositingMode - SourceOver而不是SourceCopy