How to add background picture to a grouped shape

Dear support,

I’m using the Aspose.Diagram for .NET version 24.8.0

I’m not able to set the background picture of a grouped shape. The background picture is an enhanced metafile (emf).

This is a part of the code I’m using:

   SVGSaveOptions svgSaveOptions = new()
   {
      ExportHiddenPage = false,
      SVGFitToViewPort = true,
      SaveFormat = SaveFileFormat.Svg
   };

   // Remove the 'whitespace'
   List<Shape> shapesForGroup = new();
   foreach (Shape shape in page.Shapes)
   {
      shapesForGroup.Add(shape);
   }

   Shape groupShape = page.Shapes.Group(shapesForGroup.ToArray());
   if (File.Exists(backgroundPath))
   {
      groupShape.ForeignData.ForeignType = ForeignType.EnhMetafile;
      groupShape.ForeignData.Value = File.ReadAllBytes(backgroundPath);
      groupShape.ForeignData.ExtentX = groupShape.XForm.Width.Value;
      groupShape.ForeignData.ExtentY = groupShape.XForm.Height.Value;
    }

    groupShape.ToSvg(svgPath, svgSaveOptions);

Do I miss something, or is this an issue of Aspose.Diagram ?
I hope to hear soon from you.

Kind regards,

Thierry Knijff

@tkn1967

To add a background picture to a grouped shape in Aspose.Diagram for .NET, you need to ensure that the foreign data is correctly set for the grouped shape. Based on your provided code, it looks like you are on the right track, but there are a few things to consider.

  1. Setting Foreign Data: When you set the ForeignData for the grouped shape, ensure that the dimensions (ExtentX and ExtentY) are correctly set to match the size of the group. This is important for the background image to fit properly.

  2. Grouping Shapes: When you group shapes, each shape retains its properties. If you want the background image to be visible behind all shapes, you may need to adjust the z-order of the shapes after grouping.

  3. Exporting to SVG: When you call ToSvg, ensure that the SVGSaveOptions are configured correctly to include the background image.

Here’s a refined version of your code snippet that ensures the background image is set correctly:

SVGSaveOptions svgSaveOptions = new SVGSaveOptions
{
    ExportHiddenPage = false,
    SVGFitToViewPort = true,
    SaveFormat = SaveFileFormat.Svg
};

// Remove the 'whitespace'
List<Shape> shapesForGroup = new List<Shape>();
foreach (Shape shape in page.Shapes)
{
    shapesForGroup.Add(shape);
}

Shape groupShape = page.Shapes.Group(shapesForGroup.ToArray());
if (File.Exists(backgroundPath))
{
    groupShape.ForeignData.ForeignType = ForeignType.EnhMetafile;
    groupShape.ForeignData.Value = File.ReadAllBytes(backgroundPath);
    groupShape.ForeignData.ExtentX = groupShape.XForm.Width.Value;
    groupShape.ForeignData.ExtentY = groupShape.XForm.Height.Value;
}

// Ensure the background is behind other shapes
groupShape.ZOrder = ZOrder.Behind;

groupShape.ToSvg(svgPath, svgSaveOptions);

Make sure to check if the backgroundPath is valid and that the EMF file is accessible. If the background image still does not appear as expected, consider verifying the grouping and z-ordering of the shapes to ensure that the background is rendered correctly behind the other shapes.

If you continue to experience issues, it may be beneficial to check the Aspose documentation or forums for any specific limitations or additional settings related to background images in grouped shapes.

Feel free to reach out if you have further questions or need additional assistance!