• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

bVisual

  • Home
  • Services
    • How Visio smartness can help your business
    • Visio visual in Power BI
    • Visio Consulting Services
    • Visio Bureau Services
    • Visio Training and Support Services
  • Products
    • Visio Shape Report Converter
    • SS Plus
    • LayerManager
    • visViewer
    • Metro Icons
    • Rules Tools for Visio
    • The Visio 2010 Sessions App
    • Multi-Language Text for Visio
    • Document Imager for Visio
    • multiSelect for Visio
    • pdSelect for Visio
  • Case Studies
    • Case studies overview
    • Using Visio in Education for GIS
    • Visualizing Construction Project Schedules
    • Visio Online Business Process Mapping
    • Nexans Visio Template
    • CNEE Projects, WorldCom
    • Chase Manhattan Bank
  • News
    • Recent news
    • News archive
  • Resources
    • Articles➡
      • ShapeSheet Functions A-Z
      • Comparing Visio for the Web and Desktop
      • Customising Visio Shapes for the Web App
      • Key differences between the Visio desktop and web apps
      • Using the Visio Data Visualizer in Excel
      • Using Visio in Teams
      • Creating Visio Tabs and Apps for Teams with SharePoint Framework (SPFx)
      • Designing Power Automate Flows with Microsoft Visio
      • Innovative uses of Visio Lists
    • Webcasts ➡
      • Visio in Organizations
      • My session and other Visio sessions at MSIgnite 2019
      • Power up your Visio diagrams
      • Vision up your Visio diagrams
      • The Visio 2010 MVP Sessions
    • Visio Web Learning Resources
    • Books➡
      • Visualize Complex Processes with Microsoft Visio
      • Mastering Data Visualization with Microsoft Visio
      • Microsoft Visio Business Process Diagramming and Validation
      • Visualizing Information with Microsoft Visio
  • Blog
    • Browse blog articles
    • Visio Power BI articles
    • Visio for Web articles
    • A history of messaging and encryption
  • About us
    • About bVisual
    • Testimonials
    • Bio of David Parker
    • Contact Us
    • Website Privacy Policy
    • Website terms and conditions
    • Ariba Network
You are here: Home / Visio / Swapping Images in a Visio Shape

Published on April 17, 2018 by David Parker

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 wrote 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.

ChangePicture
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 =
 $"=IF((Width/Height)>={imgWHRatio.ToString()},Height*{imgWHRatio.ToString()},Width*1)";
 shp.CellsSRC[sect, row, (short)Visio.VisCellIndices.visFrgnImgHeight].FormulaForceU =
 $"=IF((Width/Height)>={imgWHRatio.ToString()},Height*1,Width/{imgWHRatio.ToString()})";
 //Set the Image Offsets
 shp.CellsSRC[sect,row,(short) Visio.VisCellIndices.visFrgnImgOffsetX].FormulaForceU=
 $"=0.5*(Width-ImgWidth)";
 shp.CellsSRC[sect,row,(short) Visio.VisCellIndices.visFrgnImgOffsetY].FormulaForceU=
 $"=0.5*(Height-ImgHeight)";
}
WorkingsForChangePicture

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);
  else
  {
    foreach (Visio.Shape s in shp.Shapes)
    {
     if (s.Type == (short)Visio.VisShapeTypes.visTypeForeignObject)
    {
     ChangeImage(s, dialog.FileName);
    }
   }
  }
 }

 
See Shape ChangePicture method

Related

Filed Under: C#, Images, ShapeSheet Formulas, Visio, Visio 2016 Tagged With: Automation, ShapeSheet

About David Parker

David Parker has 25 years' experience of providing data visualization solutions to companies around the globe. He is a Microsoft MVP and Visio expert.

Reader Interactions

Trackbacks

  1. Replacing Images in #Visio Shapes by Changing Data | bVisual - for people interested in Microsoft Visio says:
    January 10, 2019 at 3:54 pm

    […] 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 […]

    Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

  • LinkedIn
  • Twitter

Recent Posts

  • Co-authoring and Commenting with Visio Documents
  • Fixing dimensions of 2D shapes
  • Merging Linked Data from Similar Tables
  • Smart Radio Buttons and Check Boxes in Visio
  • Using Button Face Ids in Visio

Categories

Tags

Accessibility Add-Ins Connectors Containers Data Export Data Graphics Data Import Data Visualizer Educational Excel GraphDatabase Hyperlinks Icon Sets JavaScript LayerManager Layers Legend Link Data to Shapes Lists MSIgnite MVP Office365 Org Chart PowerApps PowerBI PowerQuery Processes Setup and Deployment Shape Data Shape Design ShapeSheet ShapeSheet Functions SharePoint 2013 SQL Teams Validation VBA Video Visio Visio 2007 Visio for the Web Visio Online Visio Services Visio Viewer Webinar

Footer

bVisual Profile

The UK-based independent Visio consultancy with a worldwide reach. We have over 25 years experience of providing data visualization solutions to companies around the globe.

Learn more about bVisual

  • Amazon
  • E-mail
  • Facebook
  • LinkedIn
  • Twitter
  • YouTube

Search this website

Recent posts

  • Co-authoring and Commenting with Visio Documents
  • Fixing dimensions of 2D shapes
  • Merging Linked Data from Similar Tables
  • Smart Radio Buttons and Check Boxes in Visio
  • Using Button Face Ids in Visio

Copyright © 2025 · Executive Pro on Genesis Framework · WordPress · Log in