BitBlt Printer.Canvas到TBitMap显示为纯白色

时间:2013-07-23 16:50:22

标签: delphi printing bitblt

我正在尝试使用BitBlt将Printer.Canvas捕获为位图。然后,我想获取该位图并将其显示在paintbox上。但是,当我尝试这个时,我只得到一个与我为Bitmap.SetSize输入的值成比例的白色矩形。我的打印输出看起来是正确的,所以我几乎肯定打印机的画布​​正在被吸引。我尝试使用变量位图作为目标,并使用paintbox作为源代码(实质上我正在绘制一个简单的矩形和文本行到Paintbox,将其缩放到位图,清除它,然后发布它回到paintbox),但现在Printer.Canvas.Handle是它不显示的来源。

据我所知,屏幕和打印机之间的尺寸不同,所以我会清楚地指出尺寸,以防万一我做错了。

procedure TForm2.btnDrawClick(Sender: TObject);
begin
  Printer.BeginDoc;
  Printer.Canvas.Font.Size := 10; //Not Sure if this is necessary
  Printer.Canvas.Font.Name := 'Arial'; //Not Sure if this is necessary
  Printer.Canvas.Font.Color := clBlack; //Not Sure if this is necessary
  Printer.Canvas.Rectangle(100,100,200,200); //Should print very tiny to paper
                                             //But will look bigger when posted to 
                                             //The Paintbox
  Printer.Canvas.TextOut(120,120,'XRay-Cat');
  PCBitmap.SetSize(Paintbox1.Width,Paintbox1.Height); //Paint box is 300W,300H
  Application.ProcessMessages;
  BitBlt(PCBitmap.Canvas.Handle, //PCBitmap, is created on create, freed on destroy,
                                 //Defined in the private section                            
         0,
         0,
         PCBitmap.Width, //300
         PCBitmap.Height, //300
         Printer.Canvas.Handle,
         0,
         0,
         SRCCOPY);
  Application.ProcessMessages;
  Printer.EndDoc;

procedure TForm2.btnPostBMClick(Sender: TObject);
begin
  PaintBox1.Canvas.Draw(0,0,PCBitmap); 
end;

我希望画布也会被写入,画布会被复制到位图,然后可以在paintbox上绘制。但我看到的只是一个白色矩形。我将位图的尺寸设置为整个绘图框而不是打印机的整个画布。我这样做是因为如果我理解正确的话,我应该只是在打印机画布的TopLeft 0,0和BottomRight 300,300之间画画,就像我在我的画箱上一样。如果我直接对Paintbox这样做,我希望能看到相同的结果。

非常感谢任何帮助。提前致谢。

鉴于我收到的评论似乎我试图做的事情是不可能的。我想要做的是写入打印机画布,然后获取该画布的图像数据并将其存储在位图中。由于BitBlt无法使用,有没有办法做我想要的?我假设没有,因为我被告知Printer.Canvas无法读取。在这一点上,我找到了解决方法,但我只是好奇。

1 个答案:

答案 0 :(得分:0)

切换你的逻辑......绘制到PaintBox ...并打印PaintBox

procedure TForm55.Button1Click(Sender: TObject);
var
  a_BM: TBitMap;
begin
  a_BM := TBitmap.Create;
  try

    PaintBox1.Canvas.Font.Size := 10; //Not Sure if this is necessary
    PaintBox1.Canvas.Font.Name := 'Arial'; //Not Sure if this is necessary
    PaintBox1.Canvas.Font.Color := clBlack; //Not Sure if this is necessary
    PaintBox1.Canvas.Rectangle(0,0,300,300); //Should print very tiny to paper
                                               //But will look bigger when posted to
                                               //The Paintbox
    PaintBox1.Canvas.TextOut(120,120,'XRay-Cat');
    PaintBox1.Width := 300;
    PaintBox1.Height := 300;
    a_BM.SetSize(PaintBox1.Width, PaintBox1.Height);
    BitBlt(a_BM.Canvas.Handle, 0, 0, a_BM.Width, a_BM.Height, PaintBox1.Canvas.Handle, 0, 0, SRCCOPY);
    Application.ProcessMessages;
    Printer.BeginDoc;
    Printer.Canvas.Draw(a_BM.Canvas.ClipRect.Left, a_BM.Canvas.ClipRect.Top, a_BM);
    Printer.EndDoc;
    Application.ProcessMessages;
  finally
    a_BM.Free;
  end;
end;