如何将打印机hdc保存到图像 [英] how to save a printer hdc to image

查看:87
本文介绍了如何将打印机hdc保存到图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好所有



我想将打印机硬盘保存到图像文件,而我从钩子api EndPage()得到的hdc。

代码如下:



 bool SaveToBmp(HDC hdc,RECT rc,SIZE ImgDstSize,LPCWSTR lpFilePath)
{
bool bResult = false;
BITMAPINFO bmpInfo = {0};
BYTE * pData = NULL;
SIZE ImgSrcSize = {rc.right - rc.left,rc.bottom - rc.top};
HBITMAP hBmp = NULL;
HGDIOBJ hOldObj = NULL;
HDC hdcMem = NULL;
//初始化位图信息
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = ImgDstSize.cx;
bmpInfo.bmiHeader.biHeight = ImgDstSize.cy;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
//
HANDLE hFile = INVALID_HANDLE_VALUE;
BITMAPFILEHEADER bmFileHeader = {0};
BITMAPINFOHEADER bmInfoHeader = {0};
hdcMem = :: CreateCompatibleDC(NULL);
if(hdcMem == NULL){
goto _EXIT_FUNC;
}
//从内存中获取数据DC
hBmp = CreateDIBSection(hdcMem,& bmpInfo,DIB_RGB_COLORS,reinterpret_cast< VOID **>(& pData),NULL,0 );
if(hBmp == NULL){
goto _EXIT_FUNC;
}
//吸引到内存DC
hOldObj = SelectObject(hdcMem,hBmp);
:: StretchBlt(hdcMem,0,0,ImgDstSize.cx,ImgDstSize.cx,hdc,rc.left,rc.top,ImgSrcSize.cx,ImgSrcSize.cy,SRCCOPY);
DWORD dwbmpSize = ImgDstSize.cx * ImgDstSize.cy * 3;


bmInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
bmInfoHeader.biWidth = ImgDstSize.cx;
bmInfoHeader.biHeight = ImgDstSize.cy;
bmInfoHeader.biPlanes = 1;
bmInfoHeader.biBitCount = 24;
// Bimap文件头为了写bmp文件
bmFileHeader.bfType = 0x4d42; // bmp
bmFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER)+ sizeof(BITMAPINFOHEADER);
bmFileHeader.bfSize = bmFileHeader.bfOffBits +((bmInfoHeader.biWidth * bmInfoHeader.biHeight)* 3); /// 3 =(24/8)
hFile = CreateFile(lpFilePath,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile == INVALID_HANDLE_VALUE){
goto _EXIT_FUNC;
}
DWORD dwWrite = 0;
WriteFile(hFile,& bmFileHeader,sizeof(BITMAPFILEHEADER),& dwWrite,NULL);
WriteFile(hFile,& bmInfoHeader,sizeof(BITMAPINFOHEADER),& dwWrite,NULL);
WriteFile(hFile,pData,dwbmpSize,& dwWrite,NULL);
SelectObject(hdcMem,hOldObj);
bResult = true;
_EXIT_FUNC:
if(hBmp!= NULL){
:: DeleteObject(hBmp);
}
if(hdcMem!= NULL){
:: DeleteObject(hdcMem);
}
if(hFile!= INVALID_HANDLE_VALUE){
CloseHandle(hFile);
}
返回bResult;
}





如果我放入hdc屏幕hdc,该程序可以获取图像,但如果我放入在hdc中挂钩EndPage();该程序保存错误image.why



请给我最好的解决方案。



谢谢你!

解决方案

没有保存HDC的意义;你无法使用它。这是一个在运行时创建的句柄,每次都可以不同。在不创建HDC的情况下,您无法使用它的值。这与使用一些随机值的指针相同,而没有获取某些真实存在对象的地址的内存分配。



整个想法没有任何意义。你的最终目标是什么?



-SA


问题可能不在代码中你发布了。



这可能是你打电话的方式/时间。



我怀疑打印机硬盘可能没有指出你认为它在你打电话给你的日常工作时所指的是什么。



你没有说出你得到的错误是什么。 (这有助于理解问题。)



并且您没有显示您如何调用此代码。当你说钩掉EndPage()的hdc时,你还在做什么。



Win32函数 int EndPage(HDC hdc) )将hdc作为输入,它不提供hdc。你确定你有合适的硬盘吗?



此外,MSDN说EndPage功能通常用于指示设备驱动程序前进到新页面。这并不意味着在那时关于设备上下文的状态。它已经被打印出来了,所以它很可能是空的。


你解决了这个问题吗?

或者改为使用驱动程序。

hello all

I want to save the printer hdc to a image file,and the hdc I get from hook the api EndPage().
the code as follow:

bool SaveToBmp(HDC hdc, RECT rc, SIZE ImgDstSize, LPCWSTR lpFilePath)
{
    bool bResult = false;
    BITMAPINFO bmpInfo = { 0 };
    BYTE *pData = NULL;
    SIZE ImgSrcSize = { rc.right - rc.left, rc.bottom - rc.top };
    HBITMAP hBmp = NULL;
    HGDIOBJ hOldObj = NULL;
    HDC hdcMem = NULL;
    //Initilaize the bitmap information   
    bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmpInfo.bmiHeader.biWidth = ImgDstSize.cx;
    bmpInfo.bmiHeader.biHeight = ImgDstSize.cy;
    bmpInfo.bmiHeader.biPlanes = 1;
    bmpInfo.bmiHeader.biBitCount = 24;
    // 
    HANDLE hFile = INVALID_HANDLE_VALUE;
    BITMAPFILEHEADER bmFileHeader = { 0 };
    BITMAPINFOHEADER bmInfoHeader = { 0 };
    hdcMem = ::CreateCompatibleDC(NULL);
    if (hdcMem == NULL) {
        goto _EXIT_FUNC;
    }
    // Get the data from the memory DC 
    hBmp = CreateDIBSection(hdcMem, &bmpInfo, DIB_RGB_COLORS, reinterpret_cast<VOID **>(&pData), NULL, 0);
    if (hBmp == NULL) {
        goto _EXIT_FUNC;
    }
    // Draw to the memory DC
    hOldObj = SelectObject(hdcMem, hBmp);
    ::StretchBlt(hdcMem, 0, 0, ImgDstSize.cx, ImgDstSize.cx, hdc, rc.left, rc.top, ImgSrcSize.cx, ImgSrcSize.cy, SRCCOPY);
    DWORD dwbmpSize = ImgDstSize.cx * ImgDstSize.cy * 3;
    

    bmInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmInfoHeader.biWidth = ImgDstSize.cx;
    bmInfoHeader.biHeight = ImgDstSize.cy;
    bmInfoHeader.biPlanes = 1;
    bmInfoHeader.biBitCount = 24;
    //Bimap file header in order to write bmp file
    bmFileHeader.bfType = 0x4d42;  //bmp 
    bmFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
    bmFileHeader.bfSize = bmFileHeader.bfOffBits + ((bmInfoHeader.biWidth * bmInfoHeader.biHeight) * 3); ///3=(24 / 8)
    hFile = CreateFile(lpFilePath,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    if(hFile == INVALID_HANDLE_VALUE) {
        goto _EXIT_FUNC;
    }
    DWORD dwWrite = 0;
    WriteFile(hFile,&bmFileHeader,sizeof(BITMAPFILEHEADER),&dwWrite,NULL);
    WriteFile(hFile,&bmInfoHeader, sizeof(BITMAPINFOHEADER),&dwWrite,NULL);
    WriteFile(hFile,pData, dwbmpSize,&dwWrite,NULL);
    SelectObject(hdcMem, hOldObj);
    bResult = true;
_EXIT_FUNC:
    if (hBmp != NULL) {
        ::DeleteObject(hBmp);
    }
    if (hdcMem != NULL) {
        ::DeleteObject(hdcMem);
    }
    if (hFile != INVALID_HANDLE_VALUE) {
        CloseHandle(hFile); 
    }
    return bResult;
}



If I put in the hdc From the screen hdc ,the program can Get the Image,but if I put in the hdc from hook the EndPage(); the program save a error image.why

Please give me the best solution for this.

thank you!

解决方案

There is not point of saving HDC; you wan't be able to use it. This is a handle which is created during run-time and can be different each time. Without creating a HDC you cannot use a value of it. This would be the same as using a pointer of some random value without memory allocation of getting address of some really existing object.

The whole idea does not makes any sense. What's your ultimate goal?

—SA


The problem probably isn't in the code you posted.

It's probably in how/when you are calling it.

I would suspect that the printer hdc may not point to what you think it's pointing to at the time you call your routine.

You don't say what the error is that you are getting. (That would be helpful in understanding the problem.)

And you don't show how you are calling this code. It's not clear what you are doing when you say "hdc from hook the EndPage()".

The Win32 function int EndPage(HDC hdc) takes an hdc as an input, it doesn't supply an hdc. Are you sure you have the right hdc?

Also, MSDN says that the EndPage function is "typically used to direct the device driver to advance to a new page". That doesn't imply anything about the state of the device context at that point. It's already been printed at that point, so it could very well be empty.


Have you solved the problem ?
or use the driver instead .


这篇关于如何将打印机hdc保存到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆