Solved

Draw on Printer Canvas

Posted on 2001-09-07
5
658 Views
Last Modified: 2010-05-19
Hi
I've faced with a problem.
I have some user component that contain the property with TBitmap type.
When I pass this Bitmap as a parameter to the StretchDraw method of the TImage's canvas property it works excellent. But if I do the same with StretchDraw of the Printer's canvas property either printer prints nothing or color of bitmap get lost. Moreover I can't fix the reason of Printer.Canvas.StretchDraw's different behaviour.
0
Comment
Question by:vterekh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Author Comment

by:vterekh
ID: 6463716
In addition, I can give the example;
In this case Image1 prints the ellipse but Printer prints nothing :(

procedure TForm1.Button1Click(Sender: TObject);
var
  b: TBitmap;
begin
    b := TBitmap.Create;
    b.Width := 300;
    b.Height := 300;
    b.Canvas.Ellipse(0, 0, 300, 300);

    Image1.Canvas.Draw(0, 0, b);

    Printer.BeginDoc;
    Printer.Canvas.Draw(0, 0, b);
    Printer.EndDoc;

    b.Free;
end;
0
 
LVL 3

Accepted Solution

by:
raidos earned 50 total points
ID: 6463759
I've had similar problems myself.

this is what i came up with..

Procedure DoPrint;
var
  FormImage: TBitmap;
  Info: PBitmapInfo;
  InfoSize: DWORD;
  Image: Pointer;
  ImageSize: DWORD;
  Bits: HBITMAP;
  DIBWidth, DIBHeight: Longint;
  PrintWidth, PrintHeight: Longint;
  LMargin,TMargin : Integer;
begin
  Printer.BeginDoc;
  try
    FormImage := GetFormImage;
    Visible := False;
    Canvas.Lock;
    try
      { Paint bitmap to the printer }
      with Printer, Canvas do begin
        Bits := FormImage.Handle;
        GetDIBSizes(Bits, InfoSize, ImageSize);
        Info := AllocMem(InfoSize);
        try
          Image := AllocMem(ImageSize);
          try
            GetDIB(Bits, 0, Info^, Image^);
            with Info^.bmiHeader do begin
              DIBWidth := biWidth;
              DIBHeight := biHeight;
            end;
            //PrintWidth := DIBWidth;
            //PrintHeight := DIBHeight;
            PrintWidth := PageWidth-10;
            PrintHeight := PageHeight-10;
            LMargin := Round(PageWidth / 76);
            TMargin := Round(PageHeight / 24);
            StretchDIBits(Canvas.Handle, LMargin, TMargin, PrintWidth-LMargin, PrintHeight-TMargin, 0, 0,
              DIBWidth, DIBHeight, Image, Info^, DIB_RGB_COLORS, SRCCOPY);
          finally
            FreeMem(Image, ImageSize);
          end;
        finally
          FreeMem(Info, InfoSize);
        end;
      end;
    finally
      Canvas.Unlock;
      FormImage.Free;
    end;
  finally
    Printer.EndDoc;
  end;
end;

It should be fairly simple to use, just replace the FormImage with your image and remove the call to GetFormImage;

//raidos
0
 
LVL 3

Expert Comment

by:raidos
ID: 6463766
ohh..
also the Canvas.Lock and Canvas.Unlock might be unnecesary too...
0
 
LVL 3

Expert Comment

by:raidos
ID: 6463779
lol, goddamn....

there's plenty of calls you could remove..=/

visible := FALSE; is one you most certainly don't need either..

//raidos
0
 
LVL 1

Author Comment

by:vterekh
ID: 6464006
Thanks a lot raidos
It works great.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question