TImage32.CopyToDc

procedure CopyToDc(dstDc: HDC; x: Integer = 0; y: Integer = 0; transparent: Boolean = true); overload;

procedure CopyToDc(const srcRect: TRect; dstDc: HDC; x: Integer = 0; y: Integer = 0; transparent: Boolean = true); overload;

procedure CopyToDc(const srcRect, dstRect: TRect; dstDc: HDC; transparent: Boolean = true); overload;


Copies the image into a Window's device context

Example 1 - Copying from TImage32 to TImage

 uses Img32, Img32.Fmt.PNG;
  ...
var
  img: TImage32;
begin
  img := TImage32.Create;
  img.LoadFromResource('Beetle', 'PNG');
  //Image1 is a standard Delphi TImage component
  Image1.Picture.Bitmap.SetSize(img.Width, img.Height);
  img.CopyToDc(Image1.Picture.Bitmap.Canvas.Handle);
  img.Free;
      

Example 2 - Printing an image

 uses Img32, Img32.Fmt.PNG;
  ...
var
  img: TImage32;
  minBorderPxls, PageSizePxls, UsablePageSizePxls: TPoint;
  BorderSizePxls, pxlsPerInch: TPoint;
const
  BorderSizeInch = 0.5; // ie set 1/2" border
begin
  //choose a printer
  if not PrintDialog1.Execute then Exit;

  img := TImage32.Create;
  img.LoadFromResource('Beetle', 'PNG');

  Printer.Orientation := poLandscape;
  Printer.BeginDoc;
  with Printer.Canvas do
  begin
    pxlsPerInch.X   := GetDeviceCaps(Handle, LOGPIXELSX);
    pxlsPerInch.Y   := GetDeviceCaps(Handle, LOGPIXELSY);
    minBorderPxls.X := GetDeviceCaps(Handle, PHYSICALOFFSETX);
    minBorderPxls.Y := GetDeviceCaps(Handle, PHYSICALOFFSETY);
    PageSizePxls.X  := GetDeviceCaps(Handle, PHYSICALWIDTH);
    PageSizePxls.Y  := GetDeviceCaps(Handle, PHYSICALHEIGHT);
  end;
  BorderSizePxls.X := Round(BorderSizeInch.X * pxlsPerInch.X);
  if BorderSizePxls.X < minBorderPxls.X then 
    BorderSizePxls.X := minBorderPxls.X;
  BorderSizePxls.Y := Round(BorderSizeInch.Y * pxlsPerInch.Y);
  if BorderSizePxls.Y < minBorderPxls.Y then 
    BorderSizePxls.Y := minBorderPxls.Y;
  //get printable page size in pixels
  UsablePageSizePxls.X := PageSizePxls.X - BorderSizePxls.X *2;
  UsablePageSizePxls.Y := PageSizePxls.Y - BorderSizePxls.Y *2;
  img.CropTransparentPixels;
  img.SetBackgroundColor(clWhite32);

  //scale the image to fill the whole page
  img.Resampler := rNearestResampler;
  img.ScaleToFit(UsablePageSizePxls.X, UsablePageSizePxls.Y);

  img.CopyToDc(Printer.Canvas.Handle, BorderSizePxls.X, BorderSizePxls.Y);
  Printer.EndDoc;
  img.Free;