我在將圖像打印到打印機設備上下文(真實或XPS文檔編寫器)時遇到問題。它們在屏幕上下文和打印預覽中很好地工作,但是當我將其打印到文件或打印機時,它們在位於另一個圖像上方時顯示爲黑色方塊。我用的的CImage ::繪製之前和也有類似的結果(黑色或透明正方形)作爲使用THES GDI + API直接:ATL :: CImage似乎在某些設備上歪曲每個像素的Alpha圖像
Gdiplus::Graphics g(hDestDC) ;
...
// Edit: the image value here was one aquired from a ATL::CImage not
// a Gdiplus::Image (see solution)
g.DrawImage(image,rect,0,0,GetWidth(),GetHeight(),Gdiplus::UnitPixel,0,0,0);
帽似乎表明上下文支持通過
GetDeviceCaps(hDestDC, SB_PIXEL_ALPHA)
混合的裝置
圖像的性質似乎並沒有任何關係,在這裏我使用了兩個格式:
PNG image data, 256 x 256, 8-bit/color RGBA, non-interlaced
PNG image data, 192 x 64, 1-bit colormap, non-interlaced
產量都使用GDI +接口與數據的CImage相同的結果。讓alpha圖像在打印環境中的行爲與屏幕上的行爲一樣的最佳方式是什麼?設備功能是否會誤導某些東西,因爲alpha通過使用BitmapMatrix並使用整個圖像的混合來工作?
編輯:2013年3月4日
我的新的方法是做所有的alpha混合在內存中我的想法是,如果打印機不支持alpha混合我會創造一個內存上下文中混合,然後只需將混合結果複製到上下文中。代碼的重要部分是這樣的:
int width = rectDest.right - rectDest.left;
int height = rectDest.bottom - rectDest.top;
BLENDFUNCTION blendFunction;
blendFunction.BlendOp = AC_SRC_OVER;
blendFunction.BlendFlags = 0;
blendFunction.SourceConstantAlpha = 0xFF;
blendFunction.AlphaFormat = AC_SRC_ALPHA;
HDC memDC = CreateCompatibleDC(hDestDC);
HBITMAP bitmap = CreateCompatibleBitmap(hDestDC,width,height);
SelectBitmap(memDC,bitmap);
//sample the underying area and copy it to memDC
::BitBlt(memDC, 0,0, width, height, hDestDC, rectDest.left, rectDest.top, SRCCOPY);
//now blend the image in memory onto the area.
GdiAlphaBlend(memDC,0,0, width, height,GetDC(), 0, 0, GetWidth(), GetHeight(),blendFunction);
//now just BitBlt the blended data to the context
::BitBlt(hDestDC,rectDest.left, rectDest.top,width,height,memDC,0,0,SRCCOPY);
......令我驚訝的是,我得到了幾乎相同的結果。實際上,我沿着屏幕左側的中間步驟來確保一切正常。它所抓取的背景和alpha混合結果(即我對打印機上下文的blit)在屏幕上看起來都很棒。這可能是一個錯誤?我猜想BitBlt保留了先前混合的alpha值,所以它是拋出打印機設備上下文的像素數據中的實際alpha值?如果是這樣,我怎麼能在最終的BitBlt之前刪除alpha?
編輯:2013年3月5日
現在,我已經試過如下:
1.使用設備無關位圖來創建HBITMAP參考。
2.使用CreateDiscardableBitmap創建HBITMAP(最成功)。
3.手動設定爲每個像素到0xFF和0×00的alpha通道。
BITMAPINFO bitmapInfo;
ZeroMemory(&bitmapInfo, sizeof(BITMAPINFO));
bitmapInfo.bmiHeader.biBitCount = 32;
bitmapInfo.bmiHeader.biCompression = BI_RGB;
bitmapInfo.bmiHeader.biPlanes = 1;
bitmapInfo.bmiHeader.biSize = sizeof(bitmapInfo.bmiHeader);
bitmapInfo.bmiHeader.biWidth = width;
bitmapInfo.bmiHeader.biHeight = height;
bitmapInfo.bmiHeader.biSizeImage = bitmapSizeBytes;
HDC memDC = CreateCompatibleDC(hDestDC);
//was HBITMAP bitmap = CreateCompatibleBitmap(hDestDC,width,height);
//also tried HBITMAP bitmap = CreateDiscardableBitmap(hDestDC,width, height);
HBITMAP bitmap = CreateDIBSection(memDC, &bitmapInfo,DIB_RGB_COLORS,&imageBits,NULL,0x00);
使用一次性位至少讓圖像渲染,但與黑色的其中alpha應的區域。
以任何機會爲您的打印機PostScript打印機? Postscript對透明膠片的支持有限。 – 2013-03-01 23:52:59
原來正在測試的打印機是PCL 6打印機。它在XPS文檔編寫器中也表現出相同的行爲(打印到文件)。 – 2013-03-05 13:24:41
啊,那麼你可以不理我的答案。除非你發佈你的代碼,否則我不知道任何人都可以提供幫助。 – 2013-03-05 14:56:49