錄製中繼檔

繼承自Image類別的Metafile類別可讓您記錄一連串的繪圖命令。 錄製的命令可以儲存在記憶體中、儲存至檔案或儲存至資料流程。 中繼檔可以包含向量圖形、點陣影像和文字。

下列範例會建立 中繼檔 物件。 程式碼會使用 Metafile 物件來記錄圖形命令序列,然後將錄製的命令儲存在名為 SampleMetafile.emf 的檔案中。 請注意, 中繼檔 建構函式會接收裝置內容控制碼, 而圖形 建構函式會接收 Metafile 物件的位址。 錄製會停止 (,而且當 Graphics 物件超出範圍時,錄製的命令會儲存到檔案) 。 最後兩行程式碼會藉由建立新的Graphics物件,並將Metafile物件的位址傳遞至該Graphics物件的DrawImage方法,以顯示中繼檔。 請注意,程式碼會使用相同的 中繼檔 物件來記錄和顯示 (播放) 中繼檔。

Metafile metafile(L"SampleMetafile.emf", hdc); 
{
   Graphics graphics(&metafile);
   Pen greenPen(Color(255, 0, 255, 0));
   SolidBrush solidBrush(Color(255, 0, 0, 255));

   // Add a rectangle and an ellipse to the metafile.
   graphics.DrawRectangle(&greenPen, Rect(50, 10, 25, 75));
   graphics.DrawEllipse(&greenPen, Rect(100, 10, 25, 75));

   // Add an ellipse (drawn with antialiasing) to the metafile.
   graphics.SetSmoothingMode(SmoothingModeHighQuality);
   graphics.DrawEllipse(&greenPen, Rect(150, 10, 25, 75));

   // Add some text (drawn with antialiasing) to the metafile.
   FontFamily fontFamily(L"Arial");
   Font font(&fontFamily, 24, FontStyleRegular, UnitPixel);
   
   graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
   graphics.RotateTransform(30.0f);
   graphics.DrawString(L"Smooth Text", 11, &font, 
      PointF(50.0f, 50.0f), &solidBrush);
} // End of recording metafile.

// Play back the metafile.
Graphics playbackGraphics(hdc);
playbackGraphics.DrawImage(&metafile, 200, 100);

注意

若要記錄中繼檔,您必須根據中繼檔物件建構Graphics物件。 中繼檔的錄製會在刪除 繪圖物件或 超出範圍時結束。

 

中繼檔包含自己的圖形狀態,由用來記錄中繼檔的 Graphics 物件所定義。 Graphics物件的任何屬性 (剪輯區域、世界轉換、平滑模式,以及您在錄製中繼檔時設定的類似) ,都會儲存在中繼檔中。 當您顯示中繼檔時,繪圖將會根據儲存的屬性來完成。

在下列範例中,假設在錄製中繼檔期間,smoothing 模式已設定為 SmoothingModeNormal。 即使用於播放的 Graphics 物件的平滑模式設定為 SmoothingModeHighQuality,中繼檔還是會根據 SmoothingModeNormal 設定來播放。 這是錄製期間所設定的平滑模式,而不是播放前所設定的平滑模式。

graphics.SetSmoothingMode(SmoothingModeHighQuality);
graphics.DrawImage(&meta, 0, 0);