Draw on Printer Canvas

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.
LVL 1
vterekhAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
raidosConnect With a Mentor Commented:
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
 
vterekhAuthor Commented:
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
 
raidosCommented:
ohh..
also the Canvas.Lock and Canvas.Unlock might be unnecesary too...
0
 
raidosCommented:
lol, goddamn....

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

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

//raidos
0
 
vterekhAuthor Commented:
Thanks a lot raidos
It works great.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.