如何:将 UML 关系图导出至图像文件
您可以将 UML 文档从 Visual Studio 导出为程序控制下的图像。 例如,您可能希望作为自动文档生成的一部分执行此操作。
如果您希望手动将文档导出为图像,则可以将一个关系图中的形状复制并粘贴到诸如 Word 这样的其他程序中。 您还可以将文档打印为 XPS 格式。 有关更多信息,请参见导出关系图的图像。
保存图像
下面的代码定义了一个用于将图像保存到文件的快捷菜单命令(也称为上下文菜单命令)。
备注
若要使此代码以菜单命令方式工作,必须将其合并到 MEF 组件。有关更多信息,请参见如何:在建模图上定义菜单命令。
此代码首先使用 GetObject<T> 获取基础实现的 Diagram。 此类型具有 CreateBitmap 方法。
namespace SaveToImage
{
using System.ComponentModel.Composition; // for [Import], [Export]
using System.Drawing; // for Bitmap
using System.Drawing.Imaging; // for ImageFormat
using System.Linq; // for collection extensions
using System.Windows.Forms; // for SaveFileDialog
using Microsoft.VisualStudio.Modeling.Diagrams;
// for Diagram
using Microsoft.VisualStudio.Modeling.ExtensionEnablement;
// for IGestureExtension, ICommandExtension, ILinkedUndoContext
using Microsoft.VisualStudio.ArchitectureTools.Extensibility.Presentation;
// for IDiagramContext
using Microsoft.VisualStudio.ArchitectureTools.Extensibility.Uml;
// for designer extension attributes
/// <summary>
/// Called when the user clicks the menu item.
/// </summary>
// Context menu command applicable to any UML diagram
[Export(typeof(ICommandExtension))]
[ClassDesignerExtension]
[UseCaseDesignerExtension]
[SequenceDesignerExtension]
[ComponentDesignerExtension]
[ActivityDesignerExtension]
class CommandExtension : ICommandExtension
{
[Import]
IDiagramContext Context { get; set; }
public void Execute(IMenuCommand command)
{
// Get the diagram of the underlying implementation.
Diagram dslDiagram = Context.CurrentDiagram.GetObject<Diagram>();
if (dslDiagram != null)
{
string imageFileName = FileNameFromUser();
if (!string.IsNullOrEmpty(imageFileName))
{
Bitmap bitmap = dslDiagram.CreateBitmap(
dslDiagram.NestedChildShapes,
Diagram.CreateBitmapPreference.FavorClarityOverSmallSize);
bitmap.Save(imageFileName, GetImageType(imageFileName));
}
}
}
/// <summary>
/// Called when the user right-clicks the diagram.
/// Set Enabled and Visible to specify the menu item status.
/// </summary>
/// <param name="command"></param>
public void QueryStatus(IMenuCommand command)
{
command.Enabled = Context.CurrentDiagram != null
&& Context.CurrentDiagram.ChildShapes.Count() > 0;
}
/// <summary>
/// Menu text.
/// </summary>
public string Text
{
get { return "Save To Image..."; }
}
/// <summary>
/// Ask the user for the path of an image file.
/// </summary>
/// <returns>image file path, or null</returns>
private string FileNameFromUser()
{
SaveFileDialog dialog = new SaveFileDialog();
dialog.AddExtension = true;
dialog.DefaultExt = "image.bmp";
dialog.Filter = "Bitmap ( *.bmp )|*.bmp|JPEG File ( *.jpg )|*.jpg|Enhanced Metafile (*.emf )|*.emf|Portable Network Graphic ( *.png )|*.png";
dialog.FilterIndex = 1;
dialog.Title = "Save Diagram to Image";
return dialog.ShowDialog() == DialogResult.OK ? dialog.FileName : null;
}
/// <summary>
/// Return the appropriate image type for a file extension.
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private ImageFormat GetImageType(string fileName)
{
string extension = System.IO.Path.GetExtension(fileName).ToLowerInvariant();
ImageFormat result = ImageFormat.Bmp;
switch (extension)
{
case ".jpg":
result = ImageFormat.Jpeg;
break;
case ".emf":
result = ImageFormat.Emf;
break;
case ".png":
result = ImageFormat.Png;
break;
}
return result;
}
}
}