Bitblt on Windows 8.1 and black image results lazarus / 39-Graphics ---------------------------------------------------------------------- PaulANormanNZ · 2014-04-23 11:54 Hi, I'm not sure what is wrong with my code, this used to work on Delphi under Xp. I have tried it under FPC 2.7.1 in Lazarus (CodeTyphon 4.8) Windows 8.1 Pro, and now I always get just black filled rectangles of the correct dimensions (attached). I am effectively trying to screen capture TWinControls (like TBitBtns). Appreciate any pointers please for code. This is the main portion of my function, var control here is screen.ActiveControl try DC := GetDC ( thisForm.handle);    R := Control.boundsRect;    bmp := tBitmap.create;    bmp.width := R.Right-R.Left;    bmp.Height := R.Bottom - R.Top;    Bitblt(bmp.canvas.handle,0,0,bmp.Width,bmp.height,dc,r.left,r.top,S RCCOPY );    bmp.SaveToFile( SavePath + 'check.bmp');    jpegMake := tjpegimage.Create;    jpegMake.Assign(Bmp);    jpegMake.CompressionQuality := 100;     saveName :=  SavePath + saveName ;    jpegMake.saveToFile ( SAVEnAME);    bmp.free;    jpegMake.Free;    finally  if (dc <> null) then  releasedc(thisForm.handle,dc);  end; ---------------------------------------------------------------------- marcov · 2014-04-23 17:31 Quote from: PaulANormanNZ on April 23, 2014, 01:54:04 pm    bmp := tBitmap.create;    bmp.width := R.Right-R.Left;    bmp.Height := R.Bottom - R.Top;    Bitblt(bmp.canvas.handle,0,0,bmp.Width,bmp.height,dc,r.left,r.top,S RCCOPY ); The bmp you create has no relation to the form or another handle bearing control. Thus its canvas has probably no windows handle, so this will fail.  Do you have the complete functioning code for Delphi? ---------------------------------------------------------------------- engkin · 2014-04-23 21:00 Create a compatible bitmap and assign it to your bmp: Code: [Select]var ...   cBMP: HBITMAP; ...      bmp := Graphics.tBitmap.create;      cBMP := CreateCompatibleBitmap(DC, R.Right - R.Left, R.Bottom - R.Top);  //<---      bmp.Handle:=cBMP;  //<---      Bitblt(bmp.canvas.handle,0,0,bmp.Width,bmp.height,dc,r.left,r.top ,SRCCOPY ); ---------------------------------------------------------------------- PaulANormanNZ · 2014-04-24 07:13 Thanks marcov,      - that was pretty much identical to the Delphi code. Didn't in Delphi require CreateCompatibleBitmap(), perhaps some direct casting or something behind the scenes? Many thanks for that engkin,      - implemented it, and it works very well, as per attachments. Although I found I had to use Windows unit (before , Graphics) .. or the positioning was all wrong and instead I took a photo of the top right of the form instead (as shown in no Windows unit - saveAsFile.jpg attached) I use this in generating application images for help files. The unit and form are attached in the 7z.  I think Peter Belowes gave some pointers for this in the past. Paul ---------------------------------------------------------------------- marcov · 2014-04-24 09:52 Afaik Delphi only creates a handle under the hood when the parent of the tbitmap is assigned to a gui control, that's why  I asked. ---------------------------------------------------------------------- PaulANormanNZ · 2014-04-24 10:26 Thanks, I don't know about the inner working of Delphi on that one, but looking at FP TBitmap, it seems to inherit from TRasterImage  somewhere along the way, and TRasterImage appears to provide BitmapHandleNeeded; with appropriate steps? ---------------------------------------------------------------------- taazz · 2014-04-24 12:21 Quote from: marcov on April 24, 2014, 11:52:10 amAfaik Delphi only creates a handle under the hood when the parent of the tbitmap is assigned to a gui control, that's why  I asked. erm are you sure this isn't for the canva? TBitmap should have a handle or create a DIB handle the first time it is required what you describe will make the code windows dependant makes no sense for TBitmap non at all. ---------------------------------------------------------------------- z505 · 2017-03-09 04:03 Quote from: engkin on April 23, 2014, 11:00:17 pmCreate a compatible bitmap and assign it to your bmp: Code: [Select]var ...   cBMP: HBITMAP; ...      bmp := Graphics.tBitmap.create;      cBMP := CreateCompatibleBitmap(DC, R.Right - R.Left, R.Bottom - R.Top);  //<---      bmp.Handle:=cBMP;  //<---      Bitblt(bmp.canvas.handle,0,0,bmp.Width,bmp.height,dc,r.left,r.top ,SRCCOPY ); Sorry for replying to an old post, but remember if you CreateCompatibleBitmap() you should then free the memory when you are done, to not cause GDI leak or similar, AFAIK. 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         DeleteObject(cBMP);  The MSDN says: QuoteWhen you no longer need the bitmap, call the DeleteObject function to delete it. MSDN doesn't specify what would happen if you did not DeleteObject, but maybe a leak would occur (I'm too lazy to test to see if that's what happens with some leak check tool).