Drawing images with transparency by using System.Drawing

VRahikkaVRahikka Member ✭✭
edited June 5 in Xamarin.Mac

Hi,

I'm having hard time combining two images with transparency. I use System.Drawing for the drawing on Xamarin.Mac. My dependencies are System.Drawing.Common (4.5.1) and Microsoft.NETCore.App (2.1.0).

The issue is that if I just read the file, draw it and then save it, it looks correct. But if I do any other modifications e.g. draw rectangle behind it or try to combine two images the end result is messed up. If I have rectangle on the background the transparent image looses the transparency (square is drawn). If I draw two transparent images, there is transparency but the first object is filled with the other image's color.

Any ideas what could be wrong?

Thanks!

using System;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Reflection;

namespace RenderingTest
{
    class Program
    {
        static Bitmap baseBitmap;
        static Bitmap buttonBitmap;

        static void Main(string[] args)
        {
            baseBitmap = new Bitmap(GetFileStream("ButtonBaseAction.png"));
            buttonBitmap = new Bitmap(GetFileStream("CollectionShow.png"));


            using (var bitmap = new Bitmap(90, 90, System.Drawing.Imaging.PixelFormat.Format32bppPArgb))
            {
                using (var graphics = Graphics.FromImage(bitmap))
                {
                    //graphics.FillRectangle(Brushes.Black, 0, 0, bitmap.Width, bitmap.Height);

                    graphics.DrawImage(baseBitmap, 0, 0);
                    graphics.DrawImage(buttonBitmap, 5, 5);

                    graphics.Flush();
                }

                bitmap.Save(GetFileName("Result2.png"));
            }
        }

        private static Stream GetFileStream(String fileName) => Assembly.GetExecutingAssembly().GetManifestResourceStream($"RenderingTest.{fileName}");
        private static String GetFileName(String fileName) => Path.Combine(Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath), fileName);
    }
}
Sign In or Register to comment.