Here is a sample code to demonstrate the creation of an instance of "ImageBGRA32" class from a bitmap and to use it in a "WorldDraw" method. The code to copy the raw bytes between two arrays is not optimized and I think there are more efficient way to do it. But I hope this sample code helps you get started.
Circle c = (Circle)drawable;
Bitmap bitmap = new Bitmap(@"c:\Work\Test.bmp");
BitmapData raw = bitmap.LockBits
(
new Rectangle(0, 0, (int)bitmap.Width, (int)bitmap.Height),
ImageLockMode.ReadOnly,
bitmap.PixelFormat
);
int size = raw.Height * raw.Stride;
byte[] rawImageRGB24 = new Byte[size];
System.Runtime.InteropServices.Marshal.Copy(
raw.Scan0, rawImageRGB24, 0, size);
byte[] rawImageBGRA32 = new Byte[bitmap.Width * bitmap.Height * 4];
int rgbIndex = 0;
int rgbaIndex = 0;
for (int row = 0; row < bitmap.Height; row++)
{
for (int col = 0; col < bitmap.Width; col++)
{
rgbIndex = col * 3 + row * raw.Stride;
rgbaIndex = col * 4 + row * bitmap.Width * 4;
rawImageBGRA32[rgbaIndex]
= rawImageRGB24[rgbIndex]; // Blue
rawImageBGRA32[rgbaIndex + 1]
= rawImageRGB24[rgbIndex + 1]; // Green
rawImageBGRA32[rgbaIndex + 2]
= rawImageRGB24[rgbIndex + 2]; // Red
rawImageBGRA32[rgbaIndex + 3]
= 255; // Alpha
}
}
if (raw != null)
{
bitmap.UnlockBits(raw);
}
IntPtr unmanagedPointer
= Marshal.AllocHGlobal(rawImageBGRA32.Length);
Marshal.Copy(
rawImageBGRA32,
0,
unmanagedPointer,
rawImageBGRA32.Length);
ImageBGRA32 imgBGRA32
= new ImageBGRA32
(
(uint)bitmap.Width,
(uint)bitmap.Height,
unmanagedPointer
);
bool result
= wd.Geometry.Image
(
imgBGRA32,
c.Center,
Vector3d.XAxis * (bitmap.Width / bitmap.Height),
Vector3d.YAxis
);
Marshal.FreeHGlobal(unmanagedPointer);
return result;