Skip to main content

This snippet shows, how to convert a System.Drawing.Image to a Mogre.Image in C#

Copy to clipboard
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(); }