Inserting Jpeg image onto Visio drawing programmatically in C# or VBA

﹥>﹥吖頭↗ 提交于 2019-12-11 06:54:45

问题


Is there any way to insert an jpeg image onto the visio using C# or VBA? After inserting is there a way to format it properties like width, height, position to place on the drawing?


回答1:


When images are imported into Visio they get wrapped up into a standard shape (with a Type property of visTypeForeignObject).

From there you're just talking to cells in the ShapeSheet. (See this post for more details on the ShapeSheet http://visualsignals.typepad.co.uk/vislog/2007/10/just-for-starte.html)

So, you can start by using the macro recorder on Visio for this sort of thing. Dragging an image onto the page will produce output something like this:

Sub Macro1()

    'Enable diagram services
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150

    Dim UndoScopeID2 As Long
    UndoScopeID2 = Application.BeginUndoScope("Auto Size Page")
    Application.ActiveWindow.Page.AutoSize = False
    Application.EndUndoScope UndoScopeID2, True

    Dim UndoScopeID3 As Long
    UndoScopeID3 = Application.BeginUndoScope("Insert")
    Application.ActiveWindow.Page.Import "C:\SomeImage.jpg"
    Application.EndUndoScope UndoScopeID3, True

    ActiveWindow.DeselectAll
    ActiveWindow.Select Application.ActiveWindow.Page.Shapes.ItemFromID(1), visSelect
    Application.ActiveWindow.Selection.Move 2.129396, -0.904364

    Dim UndoScopeID4 As Long
    UndoScopeID4 = Application.BeginUndoScope("Size Object")
    Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(visSectionObject, visRowXFormOut, visXFormPinX).FormulaU = "46.261665987369 mm"
    Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(visSectionObject, visRowXFormOut, visXFormPinY).FormulaU = "212.02916285428 mm"
    Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(visSectionObject, visRowXFormOut, visXFormWidth).FormulaU = "103.47666530807 mm"
    Application.EndUndoScope UndoScopeID4, True

    Dim UndoScopeID5 As Long
    UndoScopeID5 = Application.BeginUndoScope("Size Object")
    Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(visSectionObject, visRowXFormOut, visXFormPinX).FormulaU = "46.261665987369 mm"
    Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(visSectionObject, visRowXFormOut, visXFormPinY).FormulaU = "185.77916321819 mm"
    Application.ActiveWindow.Page.Shapes.ItemFromID(1).CellsSRC(visSectionObject, visRowXFormOut, visXFormHeight).FormulaU = "73.441667394486 mm"
    Application.EndUndoScope UndoScopeID5, True

    'Restore diagram services
    ActiveDocument.DiagramServicesEnabled = DiagramServices

End Sub

The macro recorder operates in terms of the current selection, but you don't need to. Also, it uses SRC (Section, Row, Column) syntax instead of the simpler cell name syntax. So a translation of the above might be something like this:

Sub TestAddImage()
    Call DropImage(ActivePage, "C:\SomeImage.jpg")
End Sub

Private Sub DropImage(ByRef vPag As Visio.Page, imageFile As String)

If Not vPag Is Nothing Then
    Dim newShp As Visio.Shape
    Set shpNew = vPag.Import(imageFile)
    'Set position
    shpNew.CellsU("PinX").FormulaU = "75mm"
    shpNew.CellsU("PinY").FormulaU = "175mm"
    'Set size
    shpNew.CellsU("Width").FormulaU = "100mm"
    shpNew.CellsU("Height").FormulaU = "80mm"
End If

End Sub

A C# version of this would look like this:

void Main()
{
    var vApp = MyExtensions.GetRunningVisio();
    DropImage(vApp.ActivePage, @"C:\SomeImage.jpg");
}

private void DropImage(Visio.Page vPag, string imageFile)
{
    if (vPag != null)
    {
        var shpNew = vPag.Import(imageFile);
        //Set position
        shpNew.CellsU["PinX"].FormulaU = "75mm";
        shpNew.CellsU["PinY"].FormulaU = "175mm";
        //Set size
        shpNew.CellsU["Width"].FormulaU = "100mm";
        shpNew.CellsU["Height"].FormulaU = "80mm";
    }
}

Note that GetRunningVisio is my extension method for using with LinqPad:

http://visualsignals.typepad.co.uk/vislog/2015/12/getting-started-with-c-in-linqpad-with-visio.html

...but it's up to you how you get hold of the application object.



来源:https://stackoverflow.com/questions/43405368/inserting-jpeg-image-onto-visio-drawing-programmatically-in-c-sharp-or-vba

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!