unit uClipboardPNG;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TForm2 =
class(TForm)
Image1: TImage;
procedure Image1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form2: TForm2;
implementation
uses
Clipbrd, pngimage;
{$R *.dfm}
// The given TargetPNG will load the PNG from the clipboard, if possible
// Will return True if successfull
// TargetPNG won't be changed if unsuccessfull
function LoadPNGFromClipboard(_TargetPNG: TPngImage): Boolean;
var
DataStream: TMemoryStream;
Data: Pointer;
DataHandle: THandle;
PNGClipboardFormat: Integer;
begin
Result := False;
DataStream :=
NIL;
try
if not assigned(_TargetPNG)
then Exit;
PNGClipboardFormat := RegisterClipboardFormat('
PNG');
DataStream := TMemoryStream.Create;
Clipboard.Open;
DataHandle := Clipboard.GetAsHandle(PNGClipboardFormat);
Clipboard.Close;
// DataHandle will only be <> 0, if the clipboard contains a PNG Image
if DataHandle <> 0
then begin
Data := GlobalLock(DataHandle);
if Data <>
nil then begin
try
DataStream.
Write(Data^, GlobalSize(DataHandle));
DataStream.Position := 0;
Result := True;
finally
GlobalUnlock(DataHandle);
end;
end;
end;
// If Handle has been assigned and data has been read, load the PNG Data into the PNGImage
if Result
then begin
_TargetPNG.LoadFromStream(DataStream);
end;
except
//
end;
FreeAndNil(DataStream);
end;
// The given TargetImage will contain a TPNGImage with the clipboard's content, if clipboard contains a PNG
// Returns True if successfull
// TargetImage won't be changed if unsuccessfull
function LoadImageFromClipboard_PNG(_TargetImage: TImage): Boolean;
var
png: TPngImage;
begin
Result := False;
png :=
NIL;
try
if not assigned(_TargetImage)
then Exit;
png := TPngImage.Create;
Result := LoadPNGFromClipboard(png);
_TargetImage.Picture.Assign(png);
except
//
end;
FreeAndNil(png);
end;
procedure TForm2.Image1Click(Sender: TObject);
begin
// Load PNG into Image1 from Clipboard
if not LoadImageFromClipboard_PNG(Image1)
then begin
// If not successfull, clear Image1
Image1.Picture.Assign(
NIL);
// If Clipboard <> PNG, feel free to use other methods to load it, like
// Image1.Picture.Assign(Clipboard);
end;
end;
end.