public Hashtable OnHTTPGetTextureImage(Hashtable keysvals)
{
Hashtable reply = new Hashtable();
if (keysvals["method"].ToString() != "GridTexture")
return reply;
MainConsole.Instance.Debug("[WeAPI]: Sending image jpeg");
int statuscode = 200;
byte[] jpeg = new byte[0];
IAssetService m_AssetService = m_registry.RequestModuleInterface<IAssetService>();
MemoryStream imgstream = new MemoryStream();
Bitmap mapTexture = new Bitmap(1, 1);
ManagedImage managedImage;
Image image = (Image)mapTexture;
try
{
// Taking our jpeg2000 data, decoding it, then saving it to a byte array with regular jpeg data
imgstream = new MemoryStream();
// non-async because we know we have the asset immediately.
AssetBase mapasset = m_AssetService.Get(keysvals["uuid"].ToString());
// Decode image to System.Drawing.Image
if (OpenJPEG.DecodeToImage(mapasset.Data, out managedImage, out image))
{
// Save to bitmap
mapTexture = ResizeBitmap(image, 128, 128);
EncoderParameters myEncoderParameters = new EncoderParameters();
myEncoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 75L);
// Save bitmap to stream
mapTexture.Save(imgstream, GetEncoderInfo("image/jpeg"), myEncoderParameters);
// Write the stream to a byte array for output
jpeg = imgstream.ToArray();
}
}
catch (Exception)
{
// Dummy!
MainConsole.Instance.Warn("[WebAPI]: Unable to post image.");
}
finally
{
// Reclaim memory, these are unmanaged resources
// If we encountered an exception, one or more of these will be null
if (mapTexture != null)
mapTexture.Dispose();
if (image != null)
image.Dispose();
if (imgstream != null)
{
imgstream.Close();
imgstream.Dispose();
}
}
reply["str_response_string"] = Convert.ToBase64String(jpeg);
reply["int_response_code"] = statuscode;
reply["content_type"] = "image/jpeg";
return reply;
}