Solved

Draw on Printer Canvas

Posted on 2001-09-07
5
642 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now