Capturing the viewport according to render resolution.

Aug 18, 2010 by     5 Comments    Posted under: DotNet

Another snippet!

For Rig Studio, I was faced with the problem of providing a method to not only render the view to the design surface, but capture it also. I felt that sometimes a full render wouldn’t be neccesary and users would want the option of performing a viewport capture for the UI background. If you have ever used the command

gw.getViewportDib()

to retrieve a bitmap of the current viewport, you’ll notice that even if you have the safe frames/ livearea turned on, it still returns the whole viewport.

I worked out a function to calculate the correct crop margin to return a viewport bitmap according to the current render resolution.

Fn LR_CaptureRenderAspectViewport  = 
	(
	local ViewCap=undefined			
	local cv = getViewSize()
	local ratio = undefined

           case of 
           ( 
            (cv.x > cv.y):(ratio = cv.y/cv.x)
            (cv.x = cv.y):(ratio = 1)			
            (cv.x < cv.y):(ratio = cv.x/cv.y)
	   )

		VptDib =gw.getViewportDib();
                ViewCap = bitmap renderwidth renderheight color:white
                ViewportRatio = VptDib.width/VptDib.height as float
                RenderRatio = renderwidth/renderheight as float

					case of
					(
					(ViewportRatio <= RenderRatio):(
					CapturetoRenderHeightRatio =VptDib.width/RenderRatio as float
					TopEdge = ((VptDib.Height-CapturetoRenderHeightRatio)/ 2.0) as integer
					FullViewcap = bitmap vptdib.width CapturetoRenderHeightRatio color:white
					pasteBitmap VptDib FullViewcap (box2 0 TopEdge VptDib.width VptDib.height) [0,0]
					Copy FullViewcap ViewCap)
					(ViewportRatio > RenderRatio):(
					CapturetoRenderHeightRatio =VptDib.height*RenderRatio as float
					LeftEdge = ((VptDib.width-CapturetoRenderHeightRatio)/ 2.0) as integer
					FullViewcap = bitmap CapturetoRenderHeightRatio VptDib.height color:white
					pasteBitmap VptDib FullViewcap (box2 LeftEdge 0 VptDib.width VptDib.height) [0,0]
					Copy FullViewcap ViewCap)
					default:()
					)
                close VptDib
		gc()

                if viewcap != undefined then (setclipboardbitmap viewcap;close ViewCap;return true)else(return false)
	)

	tempbmp = LR_CaptureRenderAspectViewport()
	display (getclipboardbitmap())

I hope this is useful.

404
cryptocurrency golem