HOW TO:使用 .NET Framework 顯示影像

下列程式碼範例會修改 OnPaint 事件處理常式,以擷取指向主要表單 Graphics 物件的指標。OnPaint 函式主要用於 Windows Form 應用程式 (通常是使用 Visual Studio 應用程式精靈所建立)。

影像由 Image 類別所表示。範例會使用 Image.FromFile 方法從 .jpg 檔載入影像資料。在影像描繪至表單之前,會調整表單大小以容納影像。影像的描繪是使用 Graphics.DrawImage 方法來進行。

GraphicsImage 類別都位於 System.Drawing 命名空間中。

注意事項注意事項

GDI+ 內含在 Windows XP 中,而在 Windows NT 4.0 SP 6、Windows 2000、Windows 98 和 Windows Me 上則是以可轉散發檔案的方式提供。若要下載最新的可轉散發套件,請參閱 https://go.microsoft.com/fwlink/?linkid=11232。如需詳細資訊,請參閱 GDI+ 的 GDI+ SDK 說明文件。

範例

#using <system.drawing.dll>

using namespace System;
using namespace System::Drawing;

protected:
virtual Void Form1::OnPaint(PaintEventArgs^ pe) override
{
    Graphics^ g = pe->Graphics;
    Image^ image = Image::FromFile("SampleImage.jpg");
    Form::ClientSize = image->Size;
    g->DrawImage( image, 0, 0, image->Size.Width, image->Size.Height );
}

請參閱

參考

System.Drawing

其他資源

.NET 程式設計的 Visual C++