Hi, when I render a Powerpoint Slide into a Metafile Graphics Context, I obtain a byte[] array with proper EMF Format which I can successfully save into an *.emf File or embed into Word.
But when I try to “ungroup” the EMF Graphic in Word or Powerpoint I obtain an empty graphic.
Can I modify the Rendering so that the EMF is editable in Word or PPT?
This is my code:
public static byte[] PptxAsEmf(this ISlide slide, Drawing.SizeF slideSize)
{
using (var stream = new MemoryStream()) {
using (var graphics = CreateEmf(stream, bounds))
{
graphics.SetClip(new Drawing.RectangleF(0, 0, (int)bounds.Width, (int)bounds.Height));
// graphics.SetGraphicsQuality();
slide.RenderToGraphics(false, graphics, (int)bounds.Width, (int)bounds.Height);
//ImagePixelFormat pf = ImagePixelFormat.Format32bppArgb
}
stream.Flush();
return stream.ToArray();
}
}
public static Drawing.Graphics CreateEmf(this Stream stream, Rect bounds = new Rect()) {
if (bounds.Width <= 0 ||
bounds.Height <= 0) {
bounds = new Rect(0, 0, 1, 1);
} //use a dummy Image to retrieve a Graphics Context
return CreateMetaFile(stream, Math.Max(bounds.Width, 1), Math.Max(bounds.Height, 1)).CreateGraphics();
}
public static Imaging.Metafile CreateMetaFile(Stream stream, double width, double height) {
var offScreenBufferGraphics = Drawing.Graphics.FromHwndInternal(IntPtr.Zero);
//using ()
{
IntPtr deviceContextHandle = offScreenBufferGraphics.GetHdc();
var metaFile = new Imaging.Metafile(
stream,
deviceContextHandle,
new Drawing.RectangleF(0, 0, (float)width, (float)height),
Imaging.MetafileFrameUnit.Pixel,
Imaging.EmfType.EmfPlusOnly);
//TODO: offScreenBufferGraphics.ReleaseHdc();
return metaFile;
}
}