EasyGIS.NET
EasyGIS.NET copied to clipboard
GetBitmap() from hidden process get black image
Hi! I have a support app that process some data and generate events with map images. This app run in background (windows task scheduler). The image is always black when the app run in background. When the app is running normal (with window) the map image show fine. Have a way to export a map image in a background process?
I am assuming you are referring to using the SFMap control. The reason for the black image is due to the control not being visible and how Windows handles paint events. Calling Refresh or Invalidate on a control that is not visible will not fire a paint event, even if the control.Visible property is true but the Window is off screen or minimized. Because of this the SFMap.GetBitmap method will return a "black" bitmap as internally the control's paint method is never called by Windows
To generate map images you will need to implement your own method to draw the layers to the image - this is essentially how images are generated in egis.web.controls.
If you look at the TiledMapHandler source code you will find this method.
private static void RenderMap(Graphics g, List<EGIS.ShapeFileLib.ShapeFile> layers, int w, int h, PointD centerPt, double zoom)
{
lock (EGIS.ShapeFileLib.ShapeFile.Sync)
{
var crs = EGIS.Projections.CoordinateReferenceSystemFactory.Default.GetCRSById(EGIS.Projections.CoordinateReferenceSystemFactory.Wgs84PseudoMercatorEpsgCode);
for (int n = 0; n < layers.Count; n++)
{
EGIS.ShapeFileLib.ShapeFile sf = layers[n];
sf.Render(g, new Size(w, h), centerPt, zoom, ProjectionType.None, crs);
}
}
}
The method accepts a list of ShapeFile objects, a Graphics object (create one from your image), w and h are the width and height of the image and centerPt and zoom are the center point and scale to use when drawing the layers to the image. Replace crs with the CRS of your map.
Example use:
Bitmap bm = new Bitmap(256, 256, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bm);
try
{
g.Clear(MapBackgroundColor);
RenderMap(g, mapLayers, 256, 256, centerPoint, zoom);
}
finally
{
g.Dispose();
}
// call bm.Dispose() when you have finished using it