首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> 關於VC++

MFC 應用程式中使用 GDI+ 實現圖片開啟/儲存對話框

     編輯:關於VC++

開發環境:Windows XP/2003/2000/NT4/98/Me,Visual Studio 6.0/2003,GDI+

核心類別架構

類別繼承關係

  • CGdiplusFileDialog:繼承 CFileDialog,為抽象基類,封裝編解碼器資訊陣列、實現過濾器轉換
  • CGdiplusFileOpenDialog:開啟檔案對話框,動態載入 GDI+ 解碼器資訊
  • CGdiplusFileSaveAsDialog:另存新檔對話框,動態載入 GDI+ 編碼器資訊

主要特色

  • 自動掃描系統支援的圖片編解碼器,動態建構檔案過濾清單
  • 取得編碼器 CLSID,直接傳遞給 Gdiplus::Image::Save
  • 自動驗證副檔名,缺失時補預設值,錯誤時提示重輸
  • 完整整合 MFC 文檔/視圖架構

CGdiplusFileOpenDialog 使用範例

#include "GdiplusFileOpenDialog.h"

bool CFoo::LoadImage(Image*& pImage) {
    bool bLoaded = false;
    CGdiplusFileOpenDialog dlgFile;
    if (IDOK == dlgFile.DoModal()) {
        CString strPath = dlgFile.GetPathName();
        pImage = Image::FromFile(strPath.AllocSysString());
        if (Ok == pImage->GetLastStatus()) bLoaded = true;
    }
    return bLoaded;
}

CGdiplusFileSaveAsDialog 使用範例

#include "GdiplusFileSaveAsDialog.h"

bool CFoo::SaveImage(Image* pImage) {
    bool bSaved = false;
    CGdiplusFileSaveAsDialog dlgFile;
    if (IDOK == dlgFile.DoModal()) {
        CLSID clsid = dlgFile.GetCodecCLSID(); // 取得選中格式的編碼器 CLSID
        CString strPath = dlgFile.GetPathName();
        if (Ok == pImage->Save(strPath.AllocSysString(), &clsid)) bSaved = true;
    }
    return bSaved;
}

Demo 應用程式架構

MDI 架構簡易圖片瀏覽器,示範完整開啟/儲存流程:

Demo 程式截圖

關鍵實作細節

// StdAfx.h
typedef unsigned __int32 ULONG_PTR; // VC6 必需,新版 SDK 可移除
#include <afxtempl.h>
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;

// 應用程式類別初始化/釋放 GDI+
class CDemoApp : public CWinApp {
    ULONG_PTR m_gdiplusToken;
    bool InitGdiplus() {
        GdiplusStartupInput input;
        return Ok == GdiplusStartup(&m_gdiplusToken, &input, NULL);
    }
    void TermGdiplus() { GdiplusShutdown(m_gdiplusToken); }
};

// 開啟檔案命令處理
void CDemoApp::OnFileOpen() {
    CGdiplusFileOpenDialog dlg;
    if (IDOK == dlg.DoModal()) OpenDocumentFile(dlg.GetPathName());
}

// Document 開啟檔案
BOOL CDemoDoc::OnOpenDocument(LPCTSTR pszPath) {
    SafeDeleteImage();
    m_pImage = Image::FromFile(CString(pszPath).AllocSysString());
    return Ok == m_pImage->GetLastStatus();
}

// 另存新檔命令處理
void CDemoDoc::OnFileSaveAs() {
    CGdiplusFileSaveAsDialog dlg;
    if (IDOK == dlg.DoModal()) {
        CLSID clsid = dlg.GetCodecCLSID();
        CString path = dlg.GetPathName();
        Status st = m_pImage->Save(path.AllocSysString(), &clsid);
        if (st != Ok) TRACE(_T("儲存失敗: %s, 錯誤碼: %u"), path, st);
    }
}

// View 繪圖
void CDemoView::OnDraw(CDC* pDC) {
    Image* img = GetDocument()->GetImage();
    if (img) {
        Graphics g(*pDC);
        if (Ok == g.GetLastStatus())
            g.DrawImage(img, 0, 0, img->GetWidth(), img->GetHeight());
    }
}

部署注意事項

  • Windows XP/Server 2003 內建 gdiplus.dll
  • 舊版系統 (NT4/2000/98/Me) 需隨應用程式佈署 gdiplus.dll,建議放置於應用程式目錄
  • 下載來源:Platform SDK Update (含標頭檔/匯入庫);GDI+ 可轉散發套件 (含 DLL)

結語

這組類別完美解決 MFC 專案中 GDI+ 圖片格式開啟/儲存的介面整合問題,自動處理編解碼器枚舉、過濾器建構、CLSID 取得與副檔名驗證,大幅簡化開發工作量。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved