View Single Post
Old 01-30-2020, 02:40 PM   #936
Edgemeal
Human being with feelings
 
Edgemeal's Avatar
 
Join Date: Apr 2016
Location: ASU`ogacihC
Posts: 3,913
Default

Quote:
Originally Posted by mespotine View Post
Basically my question is, how do I create a bitmap from the screen's contents, so I can use JS_LICE_WritePNG?
Excellent question! I messed around with that a little bit before, PNG's look OK but I was never really sure I'm doing it correctly , would love to get Julian's feedback!

Capture screen area to PNG,..
Updated, Requires API v0.999
Code:
function CaptureScreenArea(x,y,w,h,filename)
  scrnDC = reaper.JS_GDI_GetScreenDC() --< Only available on Windows!
  destBmp = reaper.JS_LICE_CreateBitmap(true,w,h)
  destDC = reaper.JS_LICE_GetDC(destBmp)
  reaper.JS_GDI_Blit(destDC, 0, 0, scrnDC, x, y, w, h)
  reaper.JS_LICE_WritePNG(filename, destBmp, false)
  reaper.JS_LICE_DestroyBitmap(destBmp)
  reaper.JS_GDI_ReleaseDC(scrnDC)
end

Capture a window to PNG,..
* On Windows 10 it captures only the target app window, even when behind other windows!
Code:
function GetBounds(hwnd)
  local _, left, top, right, bottom = reaper.JS_Window_GetRect(hwnd)
  return left, top, right-left, bottom-top
end

function CaptureWindow(windowTitle, filename, win10) 
  window = reaper.JS_Window_FindTop(windowTitle, true)
  if not window then reaper.MB('Window not found!','Capture Error',0) return end
  sourceHDC = reaper.JS_GDI_GetWindowDC(window)
  _,_,w,h = GetBounds(window)
  srcx,srcy = 0,0
  if win10 then srcx=7 w=w-14 h=h-7 end -- workaround for invisible Win10 borders.
  dest_bmp = reaper.JS_LICE_CreateBitmap(true,w,h)
  destDC = reaper.JS_LICE_GetDC(dest_bmp)
  -- copy source to dest & write PNG
  reaper.JS_GDI_Blit(destDC, 0, 0, sourceHDC, srcx, srcy, w, h)
  reaper.JS_LICE_WritePNG(filename, dest_bmp, false) 
  -- clean up resources
  reaper.JS_GDI_ReleaseDC(window, sourceHDC)
  reaper.JS_LICE_DestroyBitmap(dest_bmp)
end

Last edited by Edgemeal; 02-08-2020 at 04:12 PM. Reason: Update for JS_API v0.999
Edgemeal is offline   Reply With Quote