简体   繁体   English

BitBlt打印机。到TBitMap的画布显示为纯白色

[英]BitBlt Printer.Canvas to a TBitMap displays as solid white

I am trying to capture Printer.Canvas as a Bitmap using BitBlt. 我正在尝试使用BitBlt将Printer.Canvas捕获为位图。 I want to then take that Bitmap and display it on a paintbox. 然后,我想获取该位图并将其显示在画板上。 However, when I attempt this I am given only a white rectangle proportionate to the values I entered for Bitmap.SetSize. 但是,当我尝试执行此操作时,只会给我一个与为Bitmap.SetSize输入的值成比例的白色矩形。 My printout looks correct, so I am almost positive the canvas of the printer is being properly drawn to. 我的打印输出看起来正确,因此我几乎可以肯定打印机的画布​​已正确绘制到该画布上。 I attempted the following code using the variable bitmap as the destination and the paintbox as the source (in essence I was drawing a simple rectangle and line of text to the Paintbox, bitblt-ing it to a bitmap, clearing it, and then posting it back to the paintbox), but now that Printer.Canvas.Handle is the source it doesn't display. 我尝试了以下代码,将变量位图用作目标,并将绘画框用作源(本质上,我是在绘画框上绘制一个简单的矩形和文本行,将其位化为位图,清除它,然后将其发布回到Paintbox),但是现在Printer.Canvas.Handle是它不会显示的源。

I understand that the dimensions between the screen and printer are different so I will clearly indicate dimensions, just in case I am doing it wrong. 我知道屏幕和打印机之间的尺寸是不同的,所以我会清楚地指出尺寸,以防万一我做错了。

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;

I expect that the canvas would be written too, the canvas would be copied to a bitmap, then be available to be drawn on the paintbox. 我希望画布也将被写入,画布将被复制到位图,然后可以在画板上绘制。 However all I see is a white rectangle. 但是,我所看到的只是一个白色矩形。 I am setting the dimensions of the bitmap to be the entire paintbox rather than the entire canvas of the printer. 我将位图的尺寸设置为整个绘画盒,而不是打印机的整个画布。 I am doing this because if I understand it correctly I should be only be drawing between the printer canvas's TopLeft 0,0 and BottomRight 300,300 the same way I would on my paint box. 我这样做是因为,如果我对它的理解正确的话,我应该只会在打印机画布的TopLeft 0,0和BottomRight 300,300之间进行绘画,就像在油漆盒上一样。 I would expect to see the same results as I would if I did this directly to the Paintbox. 我希望看到的结果与直接在Paintbox上看到的结果相同。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

Given the comments I've received it seems what I was trying to do was not possible. 鉴于我收到的评论,看来我想做的事情是不可能的。 What I wanted to do was to write to a printers canvas and then get the image data of that canvas and store it in a bitmap. 我要写的是写入打印机画布,然后获取该画布的图像数据并将其存储在位图中。 Since BitBlt can't be used is there a way to do what I wanted? 既然不能使用BitBlt,有什么方法可以做我想要的吗? I assume not, as I was told Printer.Canvas cannot be read from. 我假设不是这样,正如我被告知Printer.Canvas无法读取。 At this point I have Found a way around it but I am just curious. 在这一点上,我已经找到解决方法,但是我很好奇。

Switch your logic...draw to the PaintBox...and Print the PaintBox 切换逻辑...绘制到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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM