Following the example to create a Raster Mask from png file always results in an error (positioning results in negative numbers for the mask rectangle. Is there another way to use PNG as raster mask layer?
void AssertAreEqual(object actual, object expected)
{
if (!object.Equals(actual, expected))
{
throw new FormatException(
string.Format("Actual value {0} are not equal to expected {1}.", actual, expected));
}
}
int FromBigEndianToInt32(byte[] bytes, int index)
{
if (bytes == null)
{
throw new ArgumentNullException("bytes");
}
if (index < 0 || index + 4 > bytes.Length)
{
throw new ArgumentOutOfRangeException("index", "The index falls outside the bytes array.");
}
return (bytes[index] << 24) | (bytes[index + 1] << 16) | (bytes[index + 2] << 8) | bytes[index + 3];
}
void AddRasterMask(Layer layer, string maskSourcePath)
{
var maskData = new LayerMaskDataShort();
// maskSourcePath = "somefile.png"
using (FileStreamContainer container = FileStreamContainer.OpenFileStream(maskSourcePath))
{
byte[] bytes = new byte[22];
AssertAreEqual(container.Read(bytes), 22);
maskData.Top = FromBigEndianToInt32(bytes, 0); // Produces negative number
maskData.Left = FromBigEndianToInt32(bytes, 4);
maskData.Bottom = FromBigEndianToInt32(bytes, 8);
maskData.Right = FromBigEndianToInt32(bytes, 12);
maskData.DefaultColor = bytes[16];
maskData.Flags = (LayerMaskFlags)bytes[17];
int imageDataLength = FromBigEndianToInt32(bytes, 18);
byte[] data = new byte[imageDataLength];
// ---- Assertion Fails ------
AssertAreEqual(maskData.MaskRectangle.Width * maskData.MaskRectangle.Height,
imageDataLength);
AssertAreEqual(container.Read(data), imageDataLength);
maskData.ImageData = data;
}
layer.AddLayerMask(maskData);
}