StretchBlt 打印时失败
我有一个图表(位图格式),我正在尝试使用 StretchBlt
将其渲染到打印机。 当绘制到屏幕上时,StretchBlt
工作正常。 当绘制到 CutePDF 打印机时,它返回 0,将最后一个错误设置为 ERROR_INVALID_HANDLE
,并且仍然可以工作。 当绘制到 PDF995 打印机或物理 HP 打印机时,它返回 0,将最后一个错误设置为 ERROR_INVALID_HANDLE
,并且无法绘制任何内容。
什么会导致 StretchBlt
在某些设备上失败? 我已通过调用 GetDeviceCaps
验证源位图是 DIB 并且目标支持 StretchBlt
。
这是我的代码,以防相关:(它是用 C++Builder 编写的,因此它使用 Delphi 的 VCL;TCanvas 包装 HDC,TBitmap 包装 HBITMAP。VCL 提供自己的 StretchDraw
函数,它可以不支持半色调;我遇到了同样的问题。)
void PrettyStretchDraw(TCanvas *dest, const TRect& rect, TGraphic *source)
{
if (dynamic_cast<Graphics::TBitmap*>(source) && !source->Transparent) {
POINT pt;
GetBrushOrgEx(dest->Handle, &pt);
SetStretchBltMode(dest->Handle, HALFTONE);
SetBrushOrgEx(dest->Handle, pt.x, pt.y, NULL);
StretchBlt(
dest->Handle,
rect.Left,
rect.Top,
rect.Width(),
rect.Height(),
dynamic_cast<Graphics::TBitmap*>(source)->Canvas->Handle,
0,
0,
source->Width,
source->Height,
SRCCOPY);
} else {
DrawItSomeOtherWay(dest, rect, source);
}
}
I have a chart (in bitmap format) that I'm trying to render to a printer using StretchBlt
. When drawing to the screen, StretchBlt
works fine. When drawing to a CutePDF printer, it returns 0, sets the last error to ERROR_INVALID_HANDLE
, and works anyway. When drawing to a PDF995 printer or a physical HP printer, it returns 0, sets the last error to ERROR_INVALID_HANDLE
, and fails to draw anything.
What would cause StretchBlt
to fail for certain devices? I've verified that the source bitmap is a DIB and that the destination supports StretchBlt
by calling GetDeviceCaps
.
Here's my code, in case it's relevant: (It's written in C++Builder, so it uses Delphi's VCL; TCanvas wraps an HDC, and TBitmap wraps an HBITMAP. VCL provides its own StretchDraw
function which does not support HALFTONE; I'm getting the same problems with it.)
void PrettyStretchDraw(TCanvas *dest, const TRect& rect, TGraphic *source)
{
if (dynamic_cast<Graphics::TBitmap*>(source) && !source->Transparent) {
POINT pt;
GetBrushOrgEx(dest->Handle, &pt);
SetStretchBltMode(dest->Handle, HALFTONE);
SetBrushOrgEx(dest->Handle, pt.x, pt.y, NULL);
StretchBlt(
dest->Handle,
rect.Left,
rect.Top,
rect.Width(),
rect.Height(),
dynamic_cast<Graphics::TBitmap*>(source)->Canvas->Handle,
0,
0,
source->Width,
source->Height,
SRCCOPY);
} else {
DrawItSomeOtherWay(dest, rect, source);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
StretchBlt
在某些打印机驱动程序上被破坏(PDF995 是一个值得注意的例子)。我曾经遇到过这个错误,仅在 Windows 2003 Server 上发生(它在 XP 上有效)。
尝试在其他操作系统上重现该问题,如果没有,请考虑该问题是特定于操作系统的,并在此操作系统上使用
StretchDIBits
代替。StretchBlt
is broken on some printer drivers (PDF995 is notable example).I once encontered this error happening on Windows 2003 Server only (it worked on XP).
Try to reproduce the problem on other OS, and it it does not, consider it OS specific and use
StretchDIBits
instead on this OS.