Mirror

How to copy a bitmap, picture or metafile from the clipboard? (Views: 100)


Problem/Question/Abstract:

How to copy a bitmap, picture or metafile from the clipboard?

Answer:

var
  bmp: TBitmap;
  pic: TPicture;

begin
  bmp := TBitmap.Create;

  // PICTURE OR METAFILE
  if (ClipBoard.HasFormat(CF_PICTURE)) or
    (ClipBoard.HasFormat(CF_METAFILEPICT)) then
  begin
    pic := TPicture.Create;
    pic.Assign(ClipBoard);
    X := pic.Width;
    Y := pic.Height;
    bmp.Width := X;
    bmp.Height := Y;
    bmp.Canvas.Draw(0, 0, pic.Graphic);
    pic.Free;
  end;

  // BITMAP
  if (ClipBoard.HasFormat(CF_BITMAP)) then
  begin
    bmp.Assign(ClipBoard);
  end;
  // Bitmap, picture or metafile is now in bmp
  // When used free bmp!
end;

<< Back to main page