ScriptSpot

Forum topic · General Scripting

Getting getViewportDib() to a file ?

By chuck666 · 2018-02-02

Description

How can you retrieve an image of a viewport and then save it to a file without using the "Grab Viewport" macro. After a bit researching I found that getViewportDib() or view.getViewportDib() will get the bitmap image of the view but how can you save this to a file name, location, and type, jpeg or png, with no dialog boxes ?
Any help would be appreciated, thanks.

Comments (6)

chuck666 · 2018-02-08

Thanks, those methods worked like a charm. A little off topic but why the fudge can't programming languages like maxscript, auto and visual lisp, corelscript and probably many others simply cannot read a text file, skip down to a line, then replace that line with new content and be on your way. Its absurd how you have to read a line, then write it to a separate open file over and over just to change one line, makes no sense to me all. You can open a file and append to the end of it ?? Is there technical reason that makes this seem impossible to do ?

Thanks for the saving viewport to a file info.

.

jahman · 2018-02-08

appending is easy. check mxs reference on reading/writing to filestream.


(
	f = createfile @"C:/test.tmp"
	for i=1 to 10 do format "line #%\n" i to:f
	close f

	f = openfile @"C:/test.tmp" mode:"a+"
	for w in #( "I", "like","to","append!" ) do format "%\n" w to:f
	close f


	format "%\n" ((dotNetClass "system.io.file").readalltext @"C:/test.tmp")
)

miauu · 2018-02-06

.

.

miauu · 2018-02-05


(
	saveToPath = @"D:\\"
	
	grab_viewpBMP = gw.getViewportDib()
	
	tempBMP = bitmap grab_viewpBMP.width grab_viewpBMP.height
	
	pasteBitmap grab_viewpBMP tempBMP (box2 0 0 grab_viewpBMP.width grab_viewpBMP.height) [0,0] 
	
	
	
	tempBMP.filename = (saveToPath + "MyViewportSnapshot.jpg")
	
	save tempBMP
	close tempBMP
	close grab_viewpBMP
	tempBMP = undefined
	grab_viewpBMP = undefined
	freeSceneBitmaps()
)

.

jahman · 2018-02-05

:)

(
	
	bm = gw.getViewportDib()
	bm.filename = @"C:/tmp_bitmap.png"
	save bm
	
)

.

miauu · 2018-02-06

Yep, shorter. :)
When the snapshot must be bigger than the actual viewport the pasteBitmap can't be avoided. :)