public byte[] OnHTTPGetTextureImage(string path, Stream request, OSHttpRequest httpRequest, OSHttpResponse httpResponse)
{
if (httpRequest.QueryString.Get("method") != "GridTexture")
return MainServer.BlankResponse;
MainConsole.Instance.Debug("[WebUI]: Sending image jpeg");
byte[] jpeg = new byte[0];
IAssetService m_AssetService = m_registry.RequestModuleInterface<IAssetService>();
IJ2KDecoder m_j2kDecoder = m_registry.RequestModuleInterface<IJ2KDecoder>();
MemoryStream imgstream = new MemoryStream();
Image mapTexture = null;
imgstream = new MemoryStream();
try
{
// Taking our jpeg2000 data, decoding it, then saving it to a byte array with regular jpeg data
AssetBase mapasset = m_AssetService.Get(httpRequest.QueryString.Get("uuid"));
// Decode image to System.Drawing.Image
mapTexture = m_j2kDecoder.DecodeToImage(mapasset.Data);
if (mapTexture == null)
return jpeg;
// Save to bitmap
mapTexture = ResizeBitmap(mapTexture, 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("[WebUI]: 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 (imgstream != null)
{
imgstream.Close();
imgstream.Dispose();
}
}
httpResponse.ContentType = "image/jpeg";
return jpeg;
}