Images for your Revit Elements

Here is some code and a video showing how you can associate images with elements in your Revit model and display them when the elements are selected.

public void showElementImage()
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;

    // dictionary stores element id values and their corresponding image file URLs
    Dictionary<int,string> dictionary = new Dictionary<int, string>();

    // prompt user to select elements
    ElementId id = uidoc.Selection.PickObject(ObjectType.Element).ElementId;

    // give message if no dictionary entry exists for this id value
    if (!dictionary.ContainsKey(id.IntegerValue))
        TaskDialog.Show("Error","No image exists for element id = " + id.IntegerValue);

    // create a Windows Form
    using (System.Windows.Forms.Form myForm = new System.Windows.Forms.Form())
        // Create a label to indicate the element id
        System.Windows.Forms.Label label = new System.Windows.Forms.Label();
        label.Text = "Image for element id " + id.IntegerValue;
        label.AutoSize = true;
        label.Location = new System.Drawing.Point(5, 5);

        // create a picture box to show the image
        System.Windows.Forms.PictureBox picture = new System.Windows.Forms.PictureBox();
        // the location of the image is the dictionary entry for this element id value
        picture.ImageLocation = dictionary[id.IntegerValue];
        picture.Location = new System.Drawing.Point(5, 25);
        picture.Width = 500;
        picture.Height = 500;
        // stretch or shrink to fit the PictureBox
        picture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;

        // add controls to the form

        // set the size & location of the form and display it 
        myForm.Height = picture.Top + picture.Height + 50;
        myForm.Width = picture.Left + picture.Width + 50;
        myForm.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;

12 thoughts on “Images for your Revit Elements

  1. How can I do this with shared parameters via a windows.form? So that the user can save or load the pictures via an User Interface of the created windows.form?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s