Grasshopper

algorithmic modeling for Rhino

Hi All,

 

I've been tinkering with an idea to create a script that creates a screenshot (of the rhino window).

For me this could be very handy in order to iterate through various parameter configurations.

I have found various dotnet examples, like this one:

 

Dim gDest As Graphics
Dim hdcDest As IntPtr
Dim hdcSrc As Integer
Dim hWnd As Integer '= Control.Handle.ToInt32
Dim bmp As Bitmap

hWnd = system.diagnostics.Process.GetCurrentProcess.MainWindowHandle.ToInt32
bmp = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
gDest = gDest.FromImage(bmp)
hdcSrc = GetWindowDC(hWnd)
hdcDest = gDest.GetHdc

BitBlt(hdcDest.ToInt32, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, hdcSrc, 0, 0, SRCCOPY)

gDest.ReleaseHdc(hdcDest)
ReleaseDC(hWnd, hdcSrc)
bmp.Save("C:\BITMAP" & DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") & ".png")

 

This script fails at the red bit (see image)

 

GetWindowDC is apparentlypart of user32, I dared to reference the user32.dll but that didn't work, obviously because I am just poking around in the dark here..

 

All help is appreciated.

 

thanks,

Sander

 

Views: 1104

Attachments:

Replies to This Discussion

Your jumping through more hoops than you have too.  .Net has a much easier way to retrieve the screen cap.  See the example below.  Note that I'm using fully qualified names for Point, since the compiler can't distinguish between Rhino.Geometry.Point and System.Drawing.Point.

 

Also note that I'm using using blocks.  All these graphics objects make calls outside of the .Net framework, therefore need to be properly disposed, or they'll leak memory.  If you have an object that has a Dispose() method on it, then putting it inside a using block will allow it to automatically be disposed when that block goes out of scope.  If not, you should manually call the Dispose() method once you're done with the object.

 

<code>

Rectangle bounds = Screen.GetBounds(System.Drawing.Point.Empty);
using(Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height)){
using(Graphics g = Graphics.FromImage((Image) bitmap)){
g.CopyFromScreen(System.Drawing.Point.Empty, System.Drawing.Point.Empty, bounds.Size);
}
bitmap.Save(@"C:\SomePath\SomeFilename.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}

</code>

Thanks for replying Damien,

 

I was also looking at some rhino sdk examples:

http://www.rhino3d.com/5/rhinocommon/html/M_Rhino_Display_DisplayPi...

and, older:

http://wiki.mcneel.com/developer/sdksamples/screencaptureview

 

But let's test your snippet, back in a few..

 

sander

Works swimmingly =)

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service