ThinkGeo.com    |     Documentation    |     Premium Support

GeoImage and Bitmaps

We have our own implementation of the native Proj.4 libraries, this is to handle the Canadian NTV2 grid conversions.

I know you have a new Version 12 implementation of the Unmanaged Projection tools (for the Raster image manipulation) but that implementation does not support the Canadian NTV2 tools.

Now my issue, we have our own bitmap warping tool, but it is not working and I have boiled it down to the GeoImage to/from Bitmap conversions. It gets called as part of the ProjectionConverter overrides of:
protected override RasterProjectionResult ConvertToExternalProjectionCore(GeoImage image, RectangleShape imageExtent, RectangleShape targetExtent, int width, int height)

If I just do a simple pass through of something like this:

private GeoImage DoSomething(GeoImage source)
{
GeoImage newImage = new GeoImage(source.GetImageStream());
return newImage;
}

I see the image on the map,

But, if I try to convert to a bitmap (as a bitmap is what I need to manipulate) then I see nothing. Example

private GeoImage DoSomething(GeoImage source)
{
GeoImage newImage;

// Get a Bitmap as this is what I need to work with
Bitmap bitmap= new Bitmap(source.GetImageStream());

// I now do something to the bitmap or whatever

// Convert the bitmap back to an image
using(MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.png);
newImage = new GeoImage(stream);
}

return newImage;
}

Using the above, I get Image Not Available

ps, if I save the bitmap to a file, and look at the file, I see the image, so it seems the issue is in the new GeoImage from a MemoryStream

Any insights that anyone can share?

I was able to figure out that you need to Flush and Reset the stream prior to using it as input.

// Convert the bitmap back to an image
using(MemoryStream stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.png);
stream.Flush();
stream.Seek(0,SeekOrigin.Begin);
newImage = new GeoImage(stream);
}

Thanks for the sharing, Chris!