Hi,
We are evaluating the product for a customer and we need to figure this issue before to recommend the product.
As we saw that EMF images are not being draw in Azure, we are converting them to PNG, but converted images are not being shown as well.
In a local Desktop environment images are being shown.
Here is the code:
we store the pictureFrames that contains EMF images and then we transform the image and we add a new PictureFrame and remove the old one.
foreach (var pf in PictureFrameList)
{
IPPImage oldImage = pf.PictureFormat.Picture.Image;
Image newImage = Image.FromStream(ReplaceImage(oldImage));
IPPImage newImagetoAdd = presentation.Images.AddImage(newImage);
// Add Picture Frame with height and width equivalent of Picture
IPictureFrame picframe = slide.Shapes.AddPictureFrame(ShapeType.Rectangle, pf.X,
pf.Y, pf.Width, pf.Height, newImagetoAdd);
picframe.LineFormat.FillFormat.FillType = FillType.Solid;
picframe.LineFormat.FillFormat.SolidFillColor.Color = Color.Red;
picframe.LineFormat.Width = 2;
slide.Shapes.Remove(pf);
}
/* this is the code that replaces the image*/
public MemoryStream ReplaceImage(IPPImage pic)
{
byte[] bytes = pic.BinaryData;
Stream oldPicStream = new MemoryStream(bytes);
MemoryStream NewPictureStream = new MemoryStream();
ConvertMetafile(oldPicStream, NewPictureStream, 0, Color.Transparent, ImageFormat.Png,null);
Image newImage = Image.FromStream (NewPictureStream);
newImage.Save(NewPictureStream, ImageFormat.Png);
return NewPictureStream;
}
*/ THIS IS THE CODE OF ConvertMetafile */
public void ConvertMetafile(Stream source, Stream destination, float scale, Color? backgroundColor, ImageFormat format, EncoderParameters parameters)
{
scale = 4f;
backgroundColor = null;
format = null;
parameters = null;
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (destination == null)
{
throw new ArgumentNullException(nameof(destination));
}
using (var img = new Metafile(source))
{
var f = format ?? ImageFormat.Png;
//Determine default background color.
//Not all formats support transparency.
if (backgroundColor == null)
{
var transparentFormats = new ImageFormat[] { ImageFormat.Gif, ImageFormat.Png, ImageFormat.Wmf, ImageFormat.Emf };
var isTransparentFormat = transparentFormats.Contains(f);
backgroundColor = isTransparentFormat ? Color.Transparent : Color.White;
}
//header contains DPI information
var header = img.GetMetafileHeader();
//calculate the width and height based on the scale
//and the respective DPI
var width = (int)Math.Round((scale * img.Width / header.DpiX * 100), 0, MidpointRounding.ToEven);
var height = (int)Math.Round((scale * img.Height / header.DpiY * 100), 0, MidpointRounding.ToEven);
using (var bitmap = new Bitmap(width, height))
{
using (var g = System.Drawing.Graphics.FromImage(bitmap))
{
//fills the background
g.Clear(backgroundColor.Value);
//reuse the width and height to draw the image
//in 100% of the square of the bitmap
g.DrawImage(img, 0, 0, bitmap.Width, bitmap.Height);
}
//get codec based on GUID
var codec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(c => c.FormatID == f.Guid);
bitmap.Save(destination, codec, parameters);
}
}
}
Regards,
Pablo.