none
Background Picture

Answers

  • [Obsolete answer - see the new version following.]

     ...

    Monday, October 24, 2005 6:32 PM
  • You can set a background picture in the whole diagram as shown here. 

     

    Add your picture to the Dsl Project resources:

    • Copy the image file to the Dsl\Resources folder and include it in the Dsl project.
    • Open the DSL Project properties by right clicking on the DSL project
    • Go to the Resources tab and click to add the resources file, if necessary.
    • Drag the image from the solution explorer into the resources file

    Add a custom code file containing this: 

    [code]

    using System;

    using Microsoft.VisualStudio.Modeling.Diagrams;

    // Fix the namespace

    namespace Fabrikam.MyProject.Language1

    {

      // Fix the Diagram Class name - get it from GeneratedCode\Diagram.cs

      public partial class Language1Diagram 

      {

        protected override void InitializeInstanceResources()

        {

           base.InitializeInstanceResources();

           // Fix the Resources namespace and the Image resource name.

           ImageField backgroundField = new ImageField("background",

                                     Fabrikam.MyProject.Language1.Properties.Resources.kathRowing);

          backgroundField.DefaultFocusable = false;

          backgroundField.DefaultSelectable = false;

          backgroundField.DefaultVisibility = true;

          backgroundField.DefaultUnscaled = false;   // or true to use the natural size of the image without weird stretching

          shapeFields.Add(backgroundField);

          backgroundField.AnchoringBehavior.SetTopAnchor(AnchoringBehavior.Edge.Top, 0.01);

          backgroundField.AnchoringBehavior.SetLeftAnchor(AnchoringBehavior.Edge.Left, 0.01);

          backgroundField.AnchoringBehavior.SetRightAnchor(AnchoringBehavior.Edge.Right, 0.01);

          backgroundField.AnchoringBehavior.SetBottomAnchor(AnchoringBehavior.Edge.Bottom, 0.01);

        }

      }

    }

    [/code]


    The AnchoringBehavior stuff stretches the picture to fill the diagram. You might like to try variants for different effects.

     

     (You can apply the same technique to individual shapes, but it's generally better to use an ImageShape, which is designed for the purpose.)

    Tuesday, October 10, 2006 12:03 AM

All replies

  • [Obsolete answer - see the new version following.]

     ...

    Monday, October 24, 2005 6:32 PM
  • You can set a background picture in the whole diagram as shown here. 

     

    Add your picture to the Dsl Project resources:

    • Copy the image file to the Dsl\Resources folder and include it in the Dsl project.
    • Open the DSL Project properties by right clicking on the DSL project
    • Go to the Resources tab and click to add the resources file, if necessary.
    • Drag the image from the solution explorer into the resources file

    Add a custom code file containing this: 

    [code]

    using System;

    using Microsoft.VisualStudio.Modeling.Diagrams;

    // Fix the namespace

    namespace Fabrikam.MyProject.Language1

    {

      // Fix the Diagram Class name - get it from GeneratedCode\Diagram.cs

      public partial class Language1Diagram 

      {

        protected override void InitializeInstanceResources()

        {

           base.InitializeInstanceResources();

           // Fix the Resources namespace and the Image resource name.

           ImageField backgroundField = new ImageField("background",

                                     Fabrikam.MyProject.Language1.Properties.Resources.kathRowing);

          backgroundField.DefaultFocusable = false;

          backgroundField.DefaultSelectable = false;

          backgroundField.DefaultVisibility = true;

          backgroundField.DefaultUnscaled = false;   // or true to use the natural size of the image without weird stretching

          shapeFields.Add(backgroundField);

          backgroundField.AnchoringBehavior.SetTopAnchor(AnchoringBehavior.Edge.Top, 0.01);

          backgroundField.AnchoringBehavior.SetLeftAnchor(AnchoringBehavior.Edge.Left, 0.01);

          backgroundField.AnchoringBehavior.SetRightAnchor(AnchoringBehavior.Edge.Right, 0.01);

          backgroundField.AnchoringBehavior.SetBottomAnchor(AnchoringBehavior.Edge.Bottom, 0.01);

        }

      }

    }

    [/code]


    The AnchoringBehavior stuff stretches the picture to fill the diagram. You might like to try variants for different effects.

     

     (You can apply the same technique to individual shapes, but it's generally better to use an ImageShape, which is designed for the purpose.)

    Tuesday, October 10, 2006 12:03 AM
  • How to repeat the background image?

    (link in CSS: background-repeat: repeat; )

     

    Thursday, December 13, 2007 1:32 PM
  • A different way to draw a background is to create a custom ShapeGeometry and override the DoPaint method. With the DoPaint method you can draw anything you like at the background of the diagram.

    Code Snippet

    using System;

    using System.Drawing;

    using System.Drawing.Drawing2D;

    using Microsoft.VisualStudio.Modeling.Diagrams;

    using Fabrikam.MyProject.Language1.Properties;

    namespace Fabrikam.MyProject.Language1

    {

    public partial class Language1Diagram : Language1DiagramBase

    {

    public override ShapeGeometry ShapeGeometry

    {

    get

    {

    return new DiagramShapeGeometry();

    }

    }

    }

     

    public class DiagramShapeGeometry : RectangleShapeGeometry

    {

    private volatile static Image ImageLogo;

    public DiagramShapeGeometry()

    {

    if (ImageLogo == null)

    {

    lock (this)

    {

    if (ImageLogo == null)

    {

    System.Resources.ResourceManager resourceManager = ResourceManagers.GetResourceManager(typeof(Resources));

    ImageLogo = (Image)resourceManager.GetObject("logo");

    ImageLogo.RotateFlip(RotateFlipType.Rotate270FlipNone);

    }

    }

    }

    }

    public override void DoPaint(DiagramPaintEventArgs e, IGeometryHost geometryHost)

    {

    base.DoPaint(e, geometryHost);

    if (e != null && ImageLogo != null)

    {

    e.Graphics.DrawImage(ImageLogo, 0.0f, 0.5f, 1.15f, 3.5f);

    }

    }

    }

    }

     

     

    Tuesday, May 20, 2008 8:13 AM