I am trying to flatten a png with transparency over a bitmap with a solid colour.
so far i have this
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);
}
However this turns removes the transparency on the top layer. How do i retain a transparent png on the top?
change using (Image IMG1 = new Bitmap(251, 400))
to using (Image IMG1 = new Bitmap(251, 400, PixelFormat.Format32bppArgb))
.
For MSDN references see:
EDIT - as per comment:
To draw the PNG you want you need to use a different CompositingMode
- SourceOver
instead of SourceCopy
.