Graphics.AddMetafileComment(Byte[]) 方法

定義

將批註新增至目前的 Metafile

C#
public void AddMetafileComment(byte[] data);

參數

data
Byte[]

包含批注的位元組陣列。

範例

下列程式代碼範例的設計目的是要與 Windows Forms 搭配使用,而且它需要 PaintEventArgse,這是 Paint 事件處理程式的參數,以及 thisForm,此範例 Form。 程式代碼會執行下列動作:

  • 建立暫時 Graphics 來建立元檔,並取得其裝置內容的句柄 hdc

  • 使用 hdc建立新的元檔。

  • Metafile建立 Graphics 以顯示元檔。

  • 將矩形繪製至元檔。

  • 將批註加入元檔。

  • 處置元檔 Graphics,這會關閉元檔。

  • 處置元檔。

  • 釋放暫時 hdc

  • 處置暫時 Graphics

  • 從先前建立的檔案建立第二個元檔。

  • 將元檔繪製到畫面。

  • 處置元檔。

C#
private void AddMetafileCommentBytes(PaintEventArgs e)
{
    // Create temporary Graphics object for metafile
    //  creation and get handle to its device context.
    Graphics newGraphics = this.CreateGraphics();
    IntPtr hdc = newGraphics.GetHdc();
             
    // Create metafile object to record.
    Metafile metaFile1 = new Metafile("SampMeta.emf", hdc);
             
    // Create graphics object to record metaFile.
    Graphics metaGraphics = Graphics.FromImage(metaFile1);
             
    // Draw rectangle in metaFile.
    metaGraphics.DrawRectangle(new Pen(Color.Black, 5), 0, 0, 100, 100);
             
    // Create comment and add to metaFile.
    byte[] metaComment = {(byte)'T', (byte)'e', (byte)'s', (byte)'t'};
    metaGraphics.AddMetafileComment(metaComment);
             
    // Dispose of graphics object.
    metaGraphics.Dispose();
             
    // Dispose of metafile.
    metaFile1.Dispose();
             
    // Release handle to temporary device context.
    newGraphics.ReleaseHdc(hdc);
             
    // Dispose of scratch graphics object.
    newGraphics.Dispose();
             
    // Create existing metafile object to draw.
    Metafile metaFile2 = new Metafile("SampMeta.emf");
             
    // Draw metaFile to screen.
    e.Graphics.DrawImage(metaFile2, new Point(0, 0));
             
    // Dispose of metafile.
    metaFile2.Dispose();
}

備註

只有當這個 GraphicsMetafile相關聯時,這個方法才有效。

適用於

產品 版本
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10