Hello,
I'm trying to add run-time GDI+ images on layers, but I haven't been successful to this point. From the documentation, it appears that GdiPlusRasterSource and GdiPlusRasterLayer are the way to go. What I did to this point is created a derived class from the GdiPlusRasterLayer class to allow setting the ImageSource property. I would then set the ImageSource to a GdiPlusRasterSource instance that calls the StreamLoading event which creates a memorystream from my GDI+ Bitmap. However, I keep getting a "Parameter not valid" exception when the WinformsMap attempts to refresh.
Here is the code:
public partial class MainForm : Form
{
// ...
private void MainForm_Load(object sender, EventArgs e)
{
// ...
// Create the raster source and handle the stream loading
GdiPlusRasterSource gdiSource = new GdiPlusRasterSource("doesNotExist.bmp");
// Removed gator brackets in case it causes formatting issues
gdiSource.StreamLoading += new EventHandler[StreamLoadingEventArgs](gdiSource_StreamLoading);
// Create the custom GDI raster layer and set the
// image source
MyGdiRasterLayer gdiLayer = new MyGdiRasterLayer();
gdiLayer.ImageSource = gdiSource;
// Add the custom raster layer to the dynamic layers
this.winformsMap.DynamicOverlay.Layers.Add(gdiLayer);
// Refresh the WinformsMap
this.winformsMap.Refresh();
}
// ...
private void gdiSource_StreamLoading(object sender, StreamLoadingEventArgs e)
{
// Create a simple bitmap and color it purple
Bitmap bitmap = new Bitmap(100, 100);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.Clear(Color.Purple);
}
// Lock the bits of the bitmap
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format32bppArgb);
// Copy the bytes of the bitmap to a byte array
byte[] bitmapBytes = new byte[bitmapData.Stride * bitmapData.Height];
Marshal.Copy(bitmapData.Scan0, bitmapBytes, 0, bitmapBytes.Length);
// Unlock the bitmap bits
bitmap.UnlockBits(bitmapData);
// Create a memory stream of the bitmap bytes
MemoryStream bitmapStream = new MemoryStream(bitmapBytes);
// Set the alternate stream to the bitmap stream
e.AlternateStream = bitmapStream;
e.AlternateStreamName = "NewStream";
e.FileAccess = System.IO.FileAccess.Read;
e.FileMode = System.IO.FileMode.Open;
}
// ...
}
public class MyGdiRasterLayer : GdiPlusRasterLayer
{
public MyGdiRasterLayer()
: base()
{
}
public new RasterSource ImageSource
{
get
{
return (base.ImageSource);
}
set
{
base.ImageSource = value;
}
}
}
Any help would be appreciated, as this is the main purpose for my using this library.
Thanks for your time,
Phil