Working with printer - printing a image loaded in bmp variable, how?

Hello guys, I don't want to print a large image only print it in a small frame, like this:

 ____________
|                    |heigh = 49
|                    |  
|                    |
|                    |
|___________ |
  width = 57

There is a option to stretch the image, the image must to be print in that measure.

Thanks
LVL 1
hidrauAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
pcsentinelCommented:
 Printer.BeginDoc;
  with Printer.Canvas do
  begin
    StretchDraw(Rect(0,0,57,49),Image1.Picture.Bitmap);
  end;
  Printer.EndDoc;

obviously yo will need to adjust the top and bottom of the rect for different lines

regards
0
 
hidrauAuthor Commented:
I have a image in my Image1, I loaded the jpg file in it and not bmp. When I press the button to print, nothing is printed.
Why? Maybe it is due to the jpg?

0
 
pcsentinelCommented:
use this instead

Printer.BeginDoc;
  with Printer.Canvas do
  begin
    StretchDraw(Rect(0,0,57,49),Image1.Picture.Graphic);
  end;
  Printer.EndDoc;


regards
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Pierre CorneliusCommented:
You will have to convert the rect size to the suit printer device resolution.

If you print something on a printer set at say 600 dpi then a block of say 50 by 50 pixels will be about 1,5cm x 1,5cm on screen (96dpi) but on the printer it would only be about 0,2cm x 0,2cm which is very small. I have previously discussed this in detail over here:

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21494420.html#14464224

Regards
Pierre
0
 
hidrauAuthor Commented:
oK PierreC,
Is it possible to load in bmp variable a Jpg?

How could I create a function to print a picture, this way:

Function (bmp : Tbitmap; jpge : Tjpg)
  procedure print_Bmp
  Begin
     Printer.BeginDoc;
     with Printer.Canvas do
     begin
        StretchDraw(Rect(0,0,57,49),Image1.Picture.bmp);
     end;
     Printer.EndDoc;
  End;

  Procedure print_jpg
  Begin
     // the same cod only this changed line
     StretchDraw(Rect(0,0,57,49),Image1.Picture.Graphic);
  End

Begin
  If bmp <> nil Then  Print_bmp;
  If jpge <> nil Then  Print_jpg
   
End
0
 
Pierre CorneliusCommented:
Here is a demo showing a function that takes a graphic regardless of jpeg or bitmap and prints it at requested top and left coords and requested physical height and width (all in inches). I load an image into a TImage in the demo but the important part is in the button events showing that you can print both a TJpeImage and TBitmap regardless of how you got it. In my case I assigned it from the TImage control but you could also do for e.g. jpg.loadfromfile and the call the print function. No difference.

PAS
===
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JPeg, ExtDlgs, ExtCtrls, StdCtrls, Printers;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Image1: TImage;
    Button2: TButton;
    OpenPictureDialog1: TOpenPictureDialog;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure DemoPrintImage(AImage: TGraphic; x,y, SizeX, SizeY: single);
var PrnXRes, PrnYRes: integer;
    SrcBmp, DestBmp: TBitmap;
begin
  //x and y are the top and left in inches

  //SizeX and SizeY represent the physical size (in inches)
  // required regardless of the Printer resolution

  Printer.BeginDoc;
  try
    PrnXRes:= GetDeviceCaps(Printer.Handle, LOGPIXELSX); //Get dpi of printer along width
    PrnYRes:= GetDeviceCaps(Printer.Handle, LOGPIXELSY); //Get dpi of printer along height

    DestBmp:= TBitmap.Create;
    SrcBmp := TBitmap.Create;
    try
      srcBmp.Assign(AImage);
      DestBmp.Width := Round(SizeX * PrnXRes);
      DestBmp.Height:= Round(SizeY * PrnYRes);
      StretchBlt(DestBmp.Canvas.Handle, 0, 0, DestBmp.Width, DestBmp.Height,
                 SrcBmp.Canvas.Handle, 0, 0, SrcBmp.Width, SrcBmp.Height,
                 SRCCOPY);

      Printer.Canvas.Draw(Round(x * PrnXRes), Round(y * PrnYRes), DestBmp);
    finally
      DestBmp.Free;
      SrcBmp.Free;
    end;

    Printer.EndDoc;
  except on e: exception do
    begin
      Printer.Abort;
      raise exception.Create('Error printing: '+e.Message);
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenPictureDialog1.Execute then
  begin
    image1.Picture.LoadFromFile(OpenPictureDialog1.FileName);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var jpg: TJpegImage;
begin
  jpg:= TJpegImage.Create;
  try
    jpg.Assign(Image1.Picture.Graphic);
    DemoPrintImage(jpg, 1.5, 1.5, 0.59, 0.51);
  finally
    jpg.Free;
  end;
end;

procedure TForm1.Button3Click(Sender: TObject);
var bmp: TBitmap;
begin
  bmp:= TBitmap.Create;
  try
    bmp.Assign(Image1.Picture.Graphic);
    DemoPrintImage(bmp, 1.5, 1.5, 0.59, 0.51);
  finally
    bmp.Free;
  end;
end;

end.


DFM
===
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 251
  Height = 248
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Image1: TImage
    Left = 136
    Top = 16
    Width = 57
    Height = 113
    Stretch = True
  end
  object Button1: TButton
    Left = 16
    Top = 16
    Width = 75
    Height = 25
    Caption = 'Load image'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 16
    Top = 56
    Width = 75
    Height = 25
    Caption = 'Print JPeg'
    TabOrder = 1
    OnClick = Button2Click
  end
  object Button3: TButton
    Left = 16
    Top = 80
    Width = 75
    Height = 25
    Caption = 'Print Bmp'
    TabOrder = 2
    OnClick = Button3Click
  end
  object OpenPictureDialog1: TOpenPictureDialog
    Left = 160
    Top = 96
  end
end

Regards
Pierre
0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
 
hidrauAuthor Commented:
thanks guys for your help
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.