Skip to main content

History: Convert a System.Image to a Mogre.Image

Source of version: 5 (current)

Copy to clipboard
            This snippet shows, how to convert a [http://System.Drawing.Image|System.Drawing.Image] to a Mogre.Image in C#

{CODE(wrap="1", colors="c#")}Stream oStream = new MemoryStream();
 System.Drawing.Image image= new System.Drawing.Image(); //Here you load your image from anywhere...
 image.Save(oStream, ImageFormat.Png);
 
 /* Back to the start of the stream */
 oStream.Position = 0;
 
 /* read all the stream in a buffer */
 BinaryReader oBinaryReader = new BinaryReader(oStream);
 byte[] pBuffer = oBinaryReader.ReadBytes((int)oBinaryReader.BaseStream.Length);
 oStream.Close(); /*No more needed */
 
 Mogre.Image oMogreImage = new Mogre.Image();
 
 unsafe
   {
    GCHandle handle = GCHandle.Alloc(pBuffer, GCHandleType.Pinned);
    byte* pUnsafeByte = (byte*)handle.AddrOfPinnedObject();
    void* pUnsafeBuffer = (void*)handle.AddrOfPinnedObject();
  
    MemoryDataStream oMemoryStream = new MemoryDataStream(pUnsafeBuffer, (uint)pBuffer.Length);
    DataStreamPtr oPtrDataStream = new DataStreamPtr(oMemoryStream);
    oMogreImage = oMogreImage.Load(oPtrDataStream, "png");
    handle.Free();
   }
{CODE}