2014-10-19 166 views
-3

我在MSDN上編寫代碼作爲方向,但不起作用。它無法加載圖像並將圖像保存爲bmp。CImage :: Load無法加載圖像

#include "stdafx.h" 
#include "atlimage.h" 
#include "cstdio" 
#include "fstream" 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
    CImage m_image1; 
    CImage m_image2; 

    char *srcFile = "C:\\Users\\TYZRPVX\\Desktop\\test.jpg"; 
    const char *tarFile = "C:\\Users\\TYZRPVX\\Desktop\\testBmp.bmp"; 
    FILE *tar; 
    fopen_s(&tar, tarFile, "w"); 
    m_image1.Load((LPCTSTR)(srcFile)); 
    //m_image1.Save(_T("C:\\Users\\TYZRPVX\\Desktop\\testBmp.bmp")); 
    m_image1.Save(_T("C:\\Users\\TYZRPVX\\Desktop"),Gdiplus::ImageFormatBMP); 

    return 0; 

} 
+2

你是什麼意思「它不工作」?你有錯誤信息嗎? – DanM7 2014-10-19 13:49:51

+0

試試這個:'m_image1.Save(_T(「C:\\ Users \\ TYZRPVX \\ Desktop \\ test.bmp」),Gdiplus :: ImageFormatBMP);'你的代碼中的輸出文件名是不正確的。順便說一句,爲什麼你需要'FILE *'變量?似乎沒有關係。 – 2014-10-19 13:53:38

+0

什麼是C或C++,請做出決定。目前它似乎不是。 – 2014-10-19 16:35:59

回答

5

您可能正在編譯啓用Unicode,這是在Visual Studio中創建新項目時的默認值。這會讓你的演員變爲LPCTSTR不正確。爲您的文件名使用寬字符字符串並放下演員表。

const wchar_t *srcFile = L"C:\\Users\\TYZRPVX\\Desktop\\test.jpg"; 
const wchar_t *bmpFile = L"C:\\Users\\TYZRPVX\\Desktop\\testBmp.bmp"; 


m_image1.Load(srcFile); 
m_image1.Save(bmpFile, Gdiplus::ImageFormatBMP); 
+0

謝謝你,你已經解決了我的問題。 – TYZRPVX 2014-10-20 07:26:56