We are in the process of migrating our .NET application from System.Drawing.Common
to Aspose.Drawing
(v25.7) for cross-platform support.
System details:
Aspose .Drawing version: 25.7.0
.NET Framework: 4.7.2
OS: Windows 11
During this process, we’ve encountered a compatibility issue. Our existing code creates a Bitmap for a mask using PixelFormat.Format16bppRgb555
. However, it appears that Aspose.Drawing
(we are using v25.7) does not support this pixel format.
One of the use cases for example is:
-
Prepare a GraphicsPath
by integer-aligning and flattening it.
-
32bpp ARGB bitmap (originally 16bpp) is created with bounds encompassing the entire path
-
Graphics.FillPath()
renders the polygon interior, creating a binary mask.
-
Access the raw pixel data via BitmapData.Scan0
for statistical calculations.
In this case we are forced to use PixelFormat.Format32bppPArgb
, which consumes significantly more memory (4 bytes per pixel).
To optimize this, we investigated using PixelFormat.Format1bppIndexed
. This would be ideal, offering a 32x memory reduction.
However, we found that we cannot use Format1bppIndexed
directly because Graphics.FillPath()
does not support drawing onto indexed-format bitmaps. This appears to be a limitation shared by both GDI+ and Aspose.Drawing.
Given this context, we have two questions:
-
Are there any plans to add support for PixelFormat.Format16bppRgb555
in a future release? This would provide a direct, memory-efficient solution for our original implementation.
-
Is there an alternative method you would recommend for creating a memory-efficient binary mask from a GraphicsPath
, considering the limitations of FillPath()
with indexed formats?
To summarize, we need a memory-efficient way to create a binary mask from a GraphicsPath. Support for PixelFormat.Format16bppRgb555 would be a direct solution, but we are open to use Format1bppIndexed for maximum optimization.
Regards,
Rejitha Raj
@Rejitha
Hi Rejitha,
Thank you for reaching out with your questions regarding pixel format support in Aspose.Drawing.
-
Support for PixelFormat.Format16bppRgb555
: Currently, there is no official announcement regarding the addition of support for PixelFormat.Format16bppRgb555
in future releases of Aspose.Drawing. I recommend regularly checking the release notes and updates from Aspose, as they often include new features and enhancements based on user feedback.
-
Creating a Memory-Efficient Binary Mask: Given the limitations with FillPath()
and indexed formats, a potential solution is to create a 32bpp ARGB bitmap and then manually convert it to a binary mask. You can fill the path using FillPath()
on the 32bpp bitmap, and then iterate through the pixel data to create a binary mask based on your criteria (e.g., setting pixels to 1 for filled areas and 0 for transparent areas). This approach allows you to maintain the use of FillPath()
while still achieving a binary mask.
Here’s a minimal code snippet to illustrate this approach:
using Aspose.Drawing;
using Aspose.Drawing.Imaging;
// Create a 32bpp ARGB bitmap
Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bitmap))
{
// Fill the path
g.FillPath(Brushes.Black, graphicsPath);
}
// Create a binary mask
byte[] binaryMask = new byte[width * height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Color pixelColor = bitmap.GetPixel(x, y);
binaryMask[y * width + x] = (pixelColor.A > 0) ? (byte)1 : (byte)0; // 1 for filled, 0 for transparent
}
}
This method effectively allows you to create a binary mask while utilizing the capabilities of Aspose.Drawing
.
Please let me know if this solution meets your needs or if you have any further questions. I am here to help!
Best regards!
@Rejitha,
We require thorough evaluation on supporting PixelFormat.Format16bppRgb555 (if possible) for your requirements. Alternatively, we need to check if there is some better solution or other way around to cope with your needs. We have opened the following new ticket(s) in our internal issue tracking system and will provide updates or deliver the fix (if possible) according to the terms mentioned in Free Support Policies.
Issue ID(s): DRAWINGNET-1651
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.
@Rejitha,
We have evaluated your requirements in details.
Aspose.Drawing currently does not support Format16bppRgb555 and to fill a path you need to use a 32-bit bitmap.
You can create a binary mask using Bitmap.LockBits with Format1bppIndexed. After that you can directly use the mask as byte[] or you can create a Format1bppIndexed bitmap from byte[] data.
Please see the following example code:
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
internal class Program
{
static void Main(string[] args)
{
const int width = 30;
const int height = 30;
byte[] pixels = CreateMask(width, height);
Bitmap bitmap = CreateBitmap(width, height, pixels);
}
private static byte[] CreateMask(int width, int height)
{
Bitmap bitmap = new(width, height);
Graphics graphics = Graphics.FromImage(bitmap);
graphics.FillEllipse(Brushes.White, new Rectangle(0, 0, bitmap.Width, bitmap.Height));
var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format1bppIndexed);
byte[] pixels = new byte[data.Stride * data.Height];
Marshal.Copy(data.Scan0, pixels, 0, pixels.Length);
bitmap.UnlockBits(data);
return pixels;
}
private static Bitmap CreateBitmap(int width, int height, byte[] pixels)
{
Bitmap bitmap = new(width, height, PixelFormat.Format1bppIndexed);
var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed);
Marshal.Copy(pixels, 0, data.Scan0, pixels.Length);
bitmap.UnlockBits(data);
return bitmap;
}
}
}
Hope, this helps a bit.