如何在vc中保存图像++使用 DevIL

发布于 2024-11-27 09:13:17 字数 75 浏览 2 评论 0原文

我在我的 vc++ 应用程序中使用 DevIL。我需要保存图像。我有以编程方式生成的位图图像。如何将 png 格式的图像保存到特定位置?

I am using DevIL in my vc++ application. I need to save an image. I have the bitmap image which is generated programatically. How can I save the image in a png format to a specific location?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少年亿悲伤 2024-12-04 09:13:17
BOOL CCreateHtml::CreateImage(IHTMLDocument2 *pDoc,LPCTSTR szDestFilename,CSize srcSize)
{
    IHTMLDocument3* pDocument3 = NULL;
    IHTMLDocument2* pDocument  = NULL;
    IHTMLElement2* pElement2   = NULL;
    IHTMLElement* pElement     = NULL;
    IViewObject2* pViewObject  = NULL;
    IDispatch* pDispatch       = NULL;
    IViewObject* pViewObj = NULL;

    HRESULT hr;

    long bodyHeight;
    long bodyWidth;
    long rootHeight;
    long rootWidth;
    long height;
    long width;
    CImage img;
        if(FAILED(m_pBrowser->get_Document(&pDispatch)))
        return FALSE;
    if(FAILED(pDispatch->QueryInterface(IID_IHTMLDocument2,(void**)&pDocument)))
        return FALSE;
    if(FAILED(pDocument->get_body(&pElement)))
        return FALSE;
    if(FAILED(pElement->QueryInterface(IID_IHTMLElement2,(void**)&pElement2)))
        return FALSE;
    if(FAILED(pElement2->get_scrollHeight(&bodyHeight)))
        return FALSE;
    if(FAILED(pElement2->get_scrollWidth(&bodyWidth)))
        return FALSE;
    if(FAILED(pDispatch->QueryInterface(IID_IHTMLDocument3,(void**)&pDocument3)))
        return FALSE;
    if(FAILED(pDocument3->get_documentElement(&pElement)))
        return FALSE;
    if(FAILED(pElement->QueryInterface(IID_IHTMLElement2,(void**)&pElement2)))
        return FALSE;
    if(FAILED(pElement2->get_scrollHeight(&rootHeight)))
        return FALSE;   
    if(FAILED(pElement2->get_scrollWidth(&rootWidth)))
        return FALSE;

    HBITMAP m_hBmp;
    width = bodyWidth;
    height = rootHeight > bodyHeight ? rootHeight : bodyHeight;

    if(width > 2000)
        width = 2000;
    if(height > 2000)
        height = 2000;

    MoveWindow(0,0,width,height,TRUE);
    ::MoveWindow(m_hwndWebBrowser,0,0,width,height,TRUE);

    if(FAILED(m_pBrowser->QueryInterface(IID_IViewObject2,(void**)&pViewObject)))
        return FALSE;

    CDC *cdcMain = GetDC();
    HDC hdcMain = *cdcMain;
    HDC hdcMem = CreateCompatibleDC(hdcMain);
    m_hBmp = CreateCompatibleBitmap(hdcMain,width,height);
    SelectObject(hdcMem,m_hBmp);

    RECTL rcBounds = { 0, 0, width, height };

    hr = pViewObject->Draw(DVASPECT_CONTENT, -1, NULL, NULL,hdcMain,hdcMem, &rcBounds, NULL, NULL, 0);


        //Here is the code for saving an image as png

    CreateImage();
    ILHANDLE devHandle = m_hBmp;
    ilInit();
    ilBindImage(iluGenImage());
    ilLoadImage("123.bmp");

    ilSaveImage("abc1.png");

    pViewObject->Release();
    return TRUE;
}


//This method is used to save a  .bmp image as a bitmap stream

BOOL CCreateHtml::CreateImage()
{
    BITMAP bmp;
    HBITMAP hBmp = m_hBmp;
    int n= ::GetObject( hBmp,sizeof( BITMAP ),&bmp );
    BITMAPFILEHEADER   bmfHeader;    
    BITMAPINFOHEADER   bi;


    bi.biSize = sizeof(BITMAPINFOHEADER);    
    bi.biWidth = bmp.bmWidth;    
    bi.biHeight = bmp.bmHeight;  
    bi.biPlanes = 1;    
    bi.biBitCount = 32;    
    bi.biCompression = BI_RGB;    
    bi.biSizeImage = 0;  
    bi.biXPelsPerMeter = 0;    
    bi.biYPelsPerMeter = 0;    
    bi.biClrUsed = 0;    
    bi.biClrImportant = 0;

    DWORD dwBmpSize = ((bmp.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmp.bmHeight;
    HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize); 
    char *lpbitmap = (char *)GlobalLock(hDIB);  
    int m = GetDIBits((HDC)GetDC(), hBmp, 0,
        (UINT)bmp.bmHeight,
        lpbitmap,
        (BITMAPINFO *)&bi, DIB_RGB_COLORS);

    CString str = "captureqwsx.bmp";
    LPCSTR fNam  =(LPCSTR)(LPCTSTR) str;

    HANDLE hFile = CreateFile(fNam,
        GENERIC_WRITE,
        0,
        NULL,
        CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL);
    DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    //Offset to where the actual bitmap bits start.
    bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
    bmfHeader.bfSize = dwSizeofDIB;
    bmfHeader.bfType = 0x4D42; //BM 

    DWORD dwBytesWritten = 0;
    WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
    WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
    WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);

    //Unlock and Free the DIB from the heap
    GlobalUnlock(hDIB);    
    GlobalFree(hDIB);

    //Close the handle for the file that was created
    CloseHandle(hFile);

    //Clean up done:
    //DeleteObject(hBmp);
    return TRUE;
}
BOOL CCreateHtml::CreateImage(IHTMLDocument2 *pDoc,LPCTSTR szDestFilename,CSize srcSize)
{
    IHTMLDocument3* pDocument3 = NULL;
    IHTMLDocument2* pDocument  = NULL;
    IHTMLElement2* pElement2   = NULL;
    IHTMLElement* pElement     = NULL;
    IViewObject2* pViewObject  = NULL;
    IDispatch* pDispatch       = NULL;
    IViewObject* pViewObj = NULL;

    HRESULT hr;

    long bodyHeight;
    long bodyWidth;
    long rootHeight;
    long rootWidth;
    long height;
    long width;
    CImage img;
        if(FAILED(m_pBrowser->get_Document(&pDispatch)))
        return FALSE;
    if(FAILED(pDispatch->QueryInterface(IID_IHTMLDocument2,(void**)&pDocument)))
        return FALSE;
    if(FAILED(pDocument->get_body(&pElement)))
        return FALSE;
    if(FAILED(pElement->QueryInterface(IID_IHTMLElement2,(void**)&pElement2)))
        return FALSE;
    if(FAILED(pElement2->get_scrollHeight(&bodyHeight)))
        return FALSE;
    if(FAILED(pElement2->get_scrollWidth(&bodyWidth)))
        return FALSE;
    if(FAILED(pDispatch->QueryInterface(IID_IHTMLDocument3,(void**)&pDocument3)))
        return FALSE;
    if(FAILED(pDocument3->get_documentElement(&pElement)))
        return FALSE;
    if(FAILED(pElement->QueryInterface(IID_IHTMLElement2,(void**)&pElement2)))
        return FALSE;
    if(FAILED(pElement2->get_scrollHeight(&rootHeight)))
        return FALSE;   
    if(FAILED(pElement2->get_scrollWidth(&rootWidth)))
        return FALSE;

    HBITMAP m_hBmp;
    width = bodyWidth;
    height = rootHeight > bodyHeight ? rootHeight : bodyHeight;

    if(width > 2000)
        width = 2000;
    if(height > 2000)
        height = 2000;

    MoveWindow(0,0,width,height,TRUE);
    ::MoveWindow(m_hwndWebBrowser,0,0,width,height,TRUE);

    if(FAILED(m_pBrowser->QueryInterface(IID_IViewObject2,(void**)&pViewObject)))
        return FALSE;

    CDC *cdcMain = GetDC();
    HDC hdcMain = *cdcMain;
    HDC hdcMem = CreateCompatibleDC(hdcMain);
    m_hBmp = CreateCompatibleBitmap(hdcMain,width,height);
    SelectObject(hdcMem,m_hBmp);

    RECTL rcBounds = { 0, 0, width, height };

    hr = pViewObject->Draw(DVASPECT_CONTENT, -1, NULL, NULL,hdcMain,hdcMem, &rcBounds, NULL, NULL, 0);


        //Here is the code for saving an image as png

    CreateImage();
    ILHANDLE devHandle = m_hBmp;
    ilInit();
    ilBindImage(iluGenImage());
    ilLoadImage("123.bmp");

    ilSaveImage("abc1.png");

    pViewObject->Release();
    return TRUE;
}


//This method is used to save a  .bmp image as a bitmap stream

BOOL CCreateHtml::CreateImage()
{
    BITMAP bmp;
    HBITMAP hBmp = m_hBmp;
    int n= ::GetObject( hBmp,sizeof( BITMAP ),&bmp );
    BITMAPFILEHEADER   bmfHeader;    
    BITMAPINFOHEADER   bi;


    bi.biSize = sizeof(BITMAPINFOHEADER);    
    bi.biWidth = bmp.bmWidth;    
    bi.biHeight = bmp.bmHeight;  
    bi.biPlanes = 1;    
    bi.biBitCount = 32;    
    bi.biCompression = BI_RGB;    
    bi.biSizeImage = 0;  
    bi.biXPelsPerMeter = 0;    
    bi.biYPelsPerMeter = 0;    
    bi.biClrUsed = 0;    
    bi.biClrImportant = 0;

    DWORD dwBmpSize = ((bmp.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmp.bmHeight;
    HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize); 
    char *lpbitmap = (char *)GlobalLock(hDIB);  
    int m = GetDIBits((HDC)GetDC(), hBmp, 0,
        (UINT)bmp.bmHeight,
        lpbitmap,
        (BITMAPINFO *)&bi, DIB_RGB_COLORS);

    CString str = "captureqwsx.bmp";
    LPCSTR fNam  =(LPCSTR)(LPCTSTR) str;

    HANDLE hFile = CreateFile(fNam,
        GENERIC_WRITE,
        0,
        NULL,
        CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL, NULL);
    DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    //Offset to where the actual bitmap bits start.
    bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
    bmfHeader.bfSize = dwSizeofDIB;
    bmfHeader.bfType = 0x4D42; //BM 

    DWORD dwBytesWritten = 0;
    WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
    WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
    WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);

    //Unlock and Free the DIB from the heap
    GlobalUnlock(hDIB);    
    GlobalFree(hDIB);

    //Close the handle for the file that was created
    CloseHandle(hFile);

    //Clean up done:
    //DeleteObject(hBmp);
    return TRUE;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文