有什么办法插入一个JPEG图像上使用C#或VBA将Visio? 插入后是有办法对其进行格式化像宽度,高度,位置属性放置在图纸上?
Answer 1:
当图像被导入到Visio中,他们得到缠绕成标准形状(与visTypeForeignObject的类型属性)。
从那里,你只是说话的ShapeSheet单元格。 (请参阅这篇文章,详细了解在ShapeSheet http://visualsignals.typepad.co.uk/vislog/2007/10/just-for-starte.html )
所以,你可以通过使用Visio中的宏录制了这样的事情开始。 图片拖动到页面上,会产生输出是这样的:
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
宏录制在当前选择上操作,但你并不需要。 同时,它采用SRC(科,行,列)语法,而不是简单的单元名称语法。 所以上面的翻译可能是这样的:
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
这个C#版本是这样的:
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";
}
}
需要注意的是GetRunningVisio
是我与LinqPad使用扩展方法:
http://visualsignals.typepad.co.uk/vislog/2015/12/getting-started-with-c-in-linqpad-with-visio.html
...但它取决于你如何让应用程序对象的保持。
文章来源: Inserting Jpeg image onto Visio drawing programmatically in C# or VBA