Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Draw on Printer Canvas

Posted on 2001-09-07
5
Medium Priority
?
672 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 200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

876 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