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.

5 Comments + Add Comment

  • avatar

    Thanks so much this just saved me a mountain of time 🙂

  • avatar

    Thanks for the comments everyone, much appreciated.

  • avatar

    Awesome! Thank you so much for this, been all over the net trying to figure out how to workaround the getViewportDib() method. The code that I was able to do was only able to compensate for the width scaling. I love how you named your variables, so easy to read and learn from. Thanks again!

  • avatar

    I’ve been researching how to do this exact thing on and off for the last few days. I was even looking at maybe having to dig into dotNet for image cropping/resizing. From your explanation, this is spot-on exactly what I need. I’ve learned a lot along the way, but it’s nice to find your code. Thanks so much for sharing.

  • avatar

    nice! been thinking for something like this for ages

    thanks

Got anything to say? Go ahead and leave a comment!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>