Copying images from a Lazarus application lazarus / 39-Graphics ---------------------------------------------------------------------- hakelm · 2018-02-17 15:00 If I copy an image with   clipboard.Assign(image1.Picture.Graphic); I can paste the result into say Gimp or Thunderbird but not into for instance Libre Office. Looking into the clipboard formats with the code below I get ********************* 0 image/jpeg: 120 ********************* If I on the other hand copy an image from Gimp or a browser I get format contents like: ********************* 0 TIMESTAMP: 140 1 TARGETS: 137 2 MULTIPLE: 139 3 SAVE_TARGETS: 141 4 image/png: 119 5 image/tiff: 160 6 image/x-icon: 161 7 image/x-ico: 162 8 image/x-win-bitmap: 163 9 image/bmp: 117 10 image/x-bmp: 164 11 image/x-MS-bmp: 165 12 image/jpeg: 120 13 : 0 14 : 0 15 : 65765505 16 : 140737354051384 17 : 0 18 : 0 19 : 0 20 : 0 21 : 0 22 : 0 23 : 0 24 : 0 25 : 0 ********************* and the clipboard content seems to be acceptable by most applications. How can I get the same or similar behaviour in a Lazarus application? Thanks for any tip. H procedure TForm1.BitBtn1Click(Sender: TObject); var n:integer;  l:tstringlist; begin   writeln('*********************');   l:=tstringlist.create;   clipboard.SupportedFormats(l);   for n:=0 to l.Count-1 do     writeln(n,' ',l[n],': ',clipboard.formats[n]);   writeln('*********************');   l.free; end; ---------------------------------------------------------------------- taazz · 2018-02-17 15:26 1) you have to ask the libre office team which formats it supports and make sure that you copy one of those in memory 2) You can add multiple formats to the clipbaord by using Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   Clipboard.AddFormat(CF_Bitmap, aStream); you create multiple image classes (png, bmp, jpg etc) you use bmp.assign(primarygraphic) to copy the contents for each supported class you create a memory stream and call the savetostream method keep in mind you can't use one memory stream for all and you do not free the memory stream after to pass it to the clipboard or the data will be lost. ---------------------------------------------------------------------- hakelm · 2018-02-17 17:03 Thanks, but what do I put into the stream? H ---------------------------------------------------------------------- taazz · 2018-02-17 17:15 Quote from: hakelm on February 17, 2018, 06:03:20 pmThanks, but what do I put into the stream? H the images. for example Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses clipbrd, lclintf,......;procedure mageToClipboard(const aBitmap :string);var  vBmp:TBitmap;  vPng:TPortableNetworkGraphic;  vJpg:TJPEGImage;  vMs:TMemoryStream;begin  vBmp := TBitmap.Create;  vPng := TPortableNetworkGraphic.Create;  vJpg := TJPEGImage.Create;  try    vBmp.LoadFromFile(aBitmap);    vPng.Assign(vBmp);    vJpg.Assign(vBmp);    vMs := TMemoryStream.Create;    vBmp.SaveToStream(vMs);    Clipboard.AddFormat(ClipboardRegisterFormat(vBmp.MimeType), vMs);    vMs := TMemoryStream.Create;    vPng.SaveToStream(vMs);    Clipboard.AddFormat(ClipboardRegisterFormat(vPng.MimeType), vMs);    vMs := TMemoryStream.Create;    vJpg.SaveToStream(vMs);    Clipboard.AddFormat(ClipboardRegisterFormat(vJpg.MimeType), vMs);  finally    vBmp.Free;    vPng.Free;    vJpg.Free;  end;end; keep in mind the above sample was typed directly in the browser it was never compiled or tested, consider it a pseudo code that showcases how to add multiple image formats on the clipboard not a working sample. ---------------------------------------------------------------------- hakelm · 2018-02-18 07:47 Thank you! Brilliant, this does exactly what I needed. I can also report that Libre Office doesn't understand  image/jpeg but listens to image/bmp and image/png. In the case of image/bmp, however, the size of the image is not respected, the pasted image will fill the whole document page width. H ---------------------------------------------------------------------- wp · 2018-02-18 10:51 Make sure that the bitmap has the correct pixel density in the bitmap header. Normally this is given in pixels per inch, but bmp has "PelsPerMeter" in its header, pixels per meter. If the program creating the bitmap writes PPI into the PelsPerMeter field LibreOffice will calculate a huge image size from the small PelsPerMeter value! Conversely I know that the fpc bitmap writer had or still has a problem in respecting the correct pixel density (https://bugs.freepascal.org/view.php?id=29852, https://bugs.freepascal.org/view.php?id=29865). Maybe the following code helps. It reads the pixeldensity from the stream's bitmap header and multiplies it by 100/2.54 if its value is below some limit (which would mean that it had been entered as pixels per inch). Call this in Taaz's code after "vBmp.SaveToStream(vMs)". No warranty, of course. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure FixBmpPPI(AStream: TMemoryStream);var  p: Int64;  phdr: Int64;  fileHdr: TBitmapFileHeader;  infoHdr: TBitmapInfoHeader;  ppi: Integer;begin  p := AStream.Position;  try    AStream.Position := 0;  AStream.ReadBuffer(fileHdr, SizeOf(TBitmapFileHeader));    phdr := AStream.Position;    AStream.ReadBuffer(infoHdr, SizeOf(TBitmapInfoHeader));    if infoHdr.XPelsPerMeter < 1000 then    infoHdr.XPelsPerMeter := round(infoHdr.XPelsPerMeter * 100 / 2.54);    if infoHdr.YPelsPerMeter < 1000 then      infoHdr.YPelsPerMeter := round(infoHdr.YPelsPerMeter * 100 / 2.54);    AStream.Position := phdr;    AStream.WriteBuffer(infoHdr, SizeOf(TBitmapInfoHeader));  finally    AStream.Position;  end;end; ---------------------------------------------------------------------- hakelm · 2018-02-18 17:35 I always thought of bitmaps as rows and columns of pixels an pixel/m etc. was something for WYSIWYG applications and the printer. But you always learn something. H ---------------------------------------------------------------------- HeavyUser · 2018-02-18 18:47 Quote from: hakelm on February 18, 2018, 06:35:39 pmI always thought of bitmaps as rows and columns of pixels an pixel/m etc. was something for WYSIWYG applications and the printer. But you always learn something. H so where do you group an office application then if not in WYSIWYG? ---------------------------------------------------------------------- Julius7 · 2018-02-19 04:36 Here is a specification file for explaining the layout of bmp format http://www.digicamsoft.com/bmp/bmp.html .. Graphics File Formats This topic describes the graphics-file formats used by the Microsoft Windows .... ---------------------------------------------------------------------- Julius7 · 2018-02-19 04:39 here is another page http://paulbourke.net/dataformats/bmp/ [ this is the first few lines of the page] ... BMP image format Written by Paul Bourke  July 1998 Russian translation by Alex Marchenko.  Introduction BMP files are an historic (but still commonly used) file format for the historic (but still commonly used) operating system called "Windows". BMP images can range from black and white (1 bit per pixel) up to 24 bit colour (16.7 million colours). While the images can be compressed, this is rarely used in practice and won't be discussed in detail here. Structure A BMP file consists of either 3 or 4 parts as shown in the diagram on the right. ..............