System.NotImplementedException: MetafileGraphics.FillRectangle(Brush brush, RectangleF rect)

System.NotImplementedException: MetafileGraphics.FillRectangle(Brush brush, RectangleF rect) error occurs with the code below:

using Aspose.Drawing;
using Aspose.Drawing.Imaging;

var stream = new MemoryStream();
using var dummyGfx = Graphics.FromHwnd(IntPtr.Zero);
var deviceContextHandle = dummyGfx.GetHdc();
using var metafile = new Metafile(
stream,
deviceContextHandle,
new Rectangle(0, 0, 40, 40),
MetafileFrameUnit.Pixel,
EmfType.EmfPlusDual);
dummyGfx.ReleaseHdc();
using var graphics = Graphics.FromImage(metafile);
var rectangle = new Rectangle(0, 0, 24, 24);
var rectangleBackgroundColor = Color.Aqua;
try
{
graphics.FillRectangle(new SolidBrush(rectangleBackgroundColor), rectangle);
}
catch (Exception e)
{
Console.WriteLine(e);
Console.ReadKey();
}

Note, FillRectangle is documented at: Graphics.FillRectangle | Aspose.Drawing for .NET API Reference

If this is not yet supported, is there an alternative to use FillRectangle to generate an EMF?

@mrcook,

Could you please share a standalone sample VS.NET project, zip the application and attach to reproduce the issue on our end. We will check your issue soon.

AsposeMetafileFill.zip (3.9 MB)

@mrcook,

We reproduced the issue in a .NET6.0 project as you mentioned. We found an exception “System.NotImplementedException: MetafileGraphics.FillRectangle(Brush brush, RectangleF rect)” when generating image of FillRectangle. It seems image using FillRectangle is not implemented for .NET Core in Aspose.Drawing.Common library. We need to evaluate your issue in detail.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): DRAWINGNET-1366

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@mrcook,

As a workaround, you can use Graphics.DrawImage instead of Graphics.FillRectangle:

                Bitmap bitmap = new Bitmap(24, 24);
                using (Graphics g = Graphics.FromImage(bitmap))
                    g.Clear(Color.Aqua);
                graphics.DrawImage(bitmap, 0, 0);

Full example:

            var stream = new MemoryStream();
            var dummyGfx = Graphics.FromHwnd(IntPtr.Zero);
            var deviceContextHandle = dummyGfx.GetHdc();
            using (var metafile = new Metafile(
            stream,
            deviceContextHandle,
            new Rectangle(0, 0, 40, 40),
            MetafileFrameUnit.Pixel,
            EmfType.EmfPlusDual))
            {
                dummyGfx.ReleaseHdc();
                var graphics = Graphics.FromImage(metafile);

                Bitmap bitmap = new Bitmap(24, 24);
                using (Graphics g = Graphics.FromImage(bitmap))
                    g.Clear(Color.Aqua);
                graphics.DrawImage(bitmap, 0, 0);
            }

            File.WriteAllBytes("out.emf", stream.ToArray());

Hope, this helps a bit.