HOW TO:將 UML 圖表匯出至影像檔案
您可以從 Visual Studio 將 UML 文件匯出到受程式控制的影像。例如,您可能想要在文件自動產生程序進行時執行此作業。
如果您想要手動將文件匯出到影像,您可以複製圖表中的圖案並貼到其他程式,例如 Word。您也可以將文件列印成 XPS 格式。如需詳細資訊,請參閱匯出圖表影像。
儲存影像
下列程式碼會定義捷徑功能表命令 (也稱為內容功能表命令),這些命令會將影像儲存到檔案。
注意事項 |
---|
若要將此程式碼做為功能表命令,您必須將它併入 MEF 元件。如需詳細資訊,請參閱HOW TO:在模型圖表上定義功能表命令。 |
程式碼會先使用 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;
}
}
}