Swapping Images in a #Visio Shape

I often include images in Visio diagrams, and sometimes they are part of a group shape. There are times when I need to swap the image for another, and I want the correct width to height ratio for the new image to be applied, otherwise the image will be distorted. I recently realised that the Shape.ChangePicture() function in Visio actually returns this ratio which can then be applied to the shape cells so that the image is not distorted. Therefore I wrote some C# code (in LinqPad) to utilize this method to provide the option to change the image in a selected shape or sub-shape.


So, I write this function in C# to swap the image of the shape, and to apply the formulas to maintain its correct aspect ratio, even if the shape is subsequently resized.

private void ChangeImage(Visio.Shape shp, string fileName)
 //The width:height ratio of the original image is returned
 var imgWHRatio = shp.ChangePicture(fileName);
 //If imgWHRatio > 1 then Landscape else Portrait
 var sect =(short) Visio.VisSectionIndices.visSectionObject;
 var row = (short) Visio.VisRowIndices.visRowForeign;
 //Set the formulas for the Image Width & Height
 shp.CellsSRC[sect, row, (short)Visio.VisCellIndices.visFrgnImgWidth].FormulaForceU = 
 shp.CellsSRC[sect, row, (short)Visio.VisCellIndices.visFrgnImgHeight].FormulaForceU = 
 //Set the Image Offsets
 shp.CellsSRC[sect,row,(short) Visio.VisCellIndices.visFrgnImgOffsetX].FormulaForceU=
 shp.CellsSRC[sect,row,(short) Visio.VisCellIndices.visFrgnImgOffsetY].FormulaForceU=

The above routine applies the settings according to the following illustration:


I called this function with some code that already had the Visio.Application variable vWin set.

 var vWin = vApp.ActiveWindow;
 var vSel = vWin.Selection;
 if (vSel.Count == 0)
  //Check if a sub-selection was made
  vSel.IterationMode = (int)Visio.VisSelectMode.visSelModeSkipSuper;
 //Abort if nothing selected
 if (vSel.Count == 0) return;

 var dialog = new OpenFileDialog();
 dialog.Filter = @"png files (*.png)|*.png|
 jpg files (*.jpg)|*.jpg|
 gif files (*.gif)|*.gif|
 bmp files (*.bmp)|*.bmp|
 All files (*.*)|*.*";
 dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures);
 dialog.Title = "Select an image file";
 //Abort if no image selected
 if (dialog.ShowDialog() != DialogResult.OK) return;

foreach (Visio.Shape shp in vSel)
  if (shp.Type == (short)Visio.VisShapeTypes.visTypeForeignObject)
   ChangeImage(shp, dialog.FileName);
    foreach (Visio.Shape s in shp.Shapes)
     if (s.Type == (short)Visio.VisShapeTypes.visTypeForeignObject)
     ChangeImage(s, dialog.FileName);


See Shape ChangePicture method


One Response to “Swapping Images in a #Visio Shape”

  1. Replacing Images in #Visio Shapes by Changing Data | bVisual - for people interested in Microsoft Visio Says:

    […] wrote an article last year about Swapping Images in a #Visio Shape manually, but I want to be able to do this whenever a Shape Data value changes. This is because I […]

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.


O365 and SharePoint

Simplify Tasks

Want to learn the simple way?

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

John Goldsmith's visLog

be smart, be clear, be visual ...

Mo's blog

Personal views on Dynamics 365 for Operations and Technical Architecture.

Chris Webb's BI Blog

Microsoft Power BI, Analysis Services, MDX, DAX, M, Power Pivot and Power Query


Azure Solutions for Office 365, and more...

Rob Fahrni



Life with Visio and other Microsoft Toys!

Nilsandrey's Weblog

Just another WordPress.com weblog

Things that Should be Easy

Every so often (too often in the IT industry) I encounter things that should have been very easy to do but turned out to be far too complicated. My favorite topics include SharePoint, .Net development, and software architecture, especially distributed systems.

Visio Guy

Smart graphics for visual people

%d bloggers like this: