Submitted by zhengrong on Tue, 08/04/2015 - 09:34
Forums:
Does anybody know how to record the print screen key stroke in journal to get a screenshot of current graphic window with white background. And then the screenshot picture can be pasted in other file or email. Thank you
re: screenshot to clipboard
The code below will take a screenshot and place the image information on the clipboard.
Option Strict Off
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UF
Module Module1
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Sub Main()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If
Dim workPart As Part = theSession.Parts.Work
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
'change export location as needed
ExportScreenshot("C:\Temp\NX_screenshot.png")
'optional, delete the image file
'DeleteFile("C:\Temp\NX_screenshot.png")
lw.Close()
End Sub
Sub ExportScreenshot(ByVal filename As String)
Dim wcsVisible As Boolean = theSession.Parts.Display.WCS.Visibility
Dim triadVisible As Integer = theSession.Preferences.ScreenVisualization.TriadVisibility
Try
DeleteFile(filename)
theSession.Parts.Display.WCS.Visibility = False
theSession.Preferences.ScreenVisualization.TriadVisibility = 0
theUfSession.Disp.CreateImage(filename, UFDisp.ImageFormat.Png, UFDisp.BackgroundColor.White)
Catch ex As Exception
MsgBox(ex.Message & ControlChars.NewLine & _
"'" & filename & "' could not be created")
'Return
Finally
theSession.Parts.Display.WCS.Visibility = wcsVisible
theSession.Preferences.ScreenVisualization.TriadVisibility = triadVisible
End Try
'copy image to clipboard
Dim theImage As Bitmap = CType(Image.FromFile(filename), Bitmap)
Clipboard.SetImage(theImage)
End Sub
Sub DeleteFile(ByVal filePath As String)
'does file exist? if so, try to delete it (overwrite)
If IO.File.Exists(filePath) Then
IO.File.Delete(filePath)
End If
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module
Thank you for the code. They
Thank you for the code. They are very helpful
zhengrong
User Selected Region
Hi,
is it possible to allow user to select specific region for Snipping (Just like Snipping tool) with white background?
Can you please add if possible...
I'm having below code provided by NXjournaling
'NXJournaling.com
'August 11, 2015
'Export a screenshot from NX with a white background
'place the image data on the Windows clipboard so you can immediately
'"paste" the image to a new location (email, powerpoint, etc).
'This version will crop the image and provide a user supplied amount of "whitespace" on each side of the image.
Option Strict Off
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UF
Module Module2
Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()
Sub Main()
If IsNothing(theSession.Parts.BaseWork) Then
'active part required
Return
End If
Dim lw As ListingWindow = theSession.ListingWindow
lw.Open()
Dim tempScreenshot As String
Dim tempLocation As String = IO.Path.GetTempPath
tempScreenshot = IO.Path.Combine(tempLocation, "NXscreenshot.png")
Try
ExportScreenshot(tempScreenshot)
Catch ex As Exception
lw.WriteLine("Error: " & ex.Message)
Return
End Try
'crop the screenshot (filename, amount of whitespace on each side of image [integer value, number of pixels])
CropScreenshot(tempScreenshot, 5)
'copy image to clipboard
Dim theImage As Bitmap = CType(Image.FromFile(tempScreenshot), Bitmap)
Clipboard.SetImage(theImage)
lw.Close()
End Sub
Sub ExportScreenshot(ByVal filename As String)
'save user preference for visibility of WCS, triad, view name, and view border
Dim wcsVisible As Boolean = theSession.Parts.BaseDisplay.WCS.Visibility
Dim triadVisible As Integer = theSession.Preferences.ScreenVisualization.TriadVisibility
Dim displayModelViewNames As Boolean = theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewNames
Dim displayModelViewBorders As Boolean = theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewBorders
'turn off the WCS, triad, view name, and view border
theSession.Parts.BaseDisplay.WCS.Visibility = False
theSession.Preferences.ScreenVisualization.TriadVisibility = 0
theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewBorders = False
theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewNames = False
Try
theUfSession.Disp.CreateImage(filename, UFDisp.ImageFormat.Png, UFDisp.BackgroundColor.White)
Catch ex As Exception
MsgBox(ex.Message & ControlChars.NewLine & _
"'" & filename & "' could not be created")
Throw New Exception("Screenshot could not be created")
Finally
'reset visibility of WCS, triad, view name, and view border to user's preference
theSession.Parts.BaseDisplay.WCS.Visibility = wcsVisible
theSession.Preferences.ScreenVisualization.TriadVisibility = triadVisible
theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewBorders = displayModelViewBorders
theSession.Parts.BaseDisplay.Preferences.NamesBorderVisualization.ShowModelViewNames = displayModelViewNames
End Try
End Sub
Sub CropScreenshot(ByVal theImageFile As String, ByVal borderWidth As Integer)
'borderWidth = whitespace to leave around the border of the image
Dim original As New Bitmap(theImageFile)
Dim JPGminX As Integer = original.Width
Dim JPGminY As Integer = original.Height
Dim JPGmaxX As Integer = 0
Dim JPGmaxY As Integer = 0
Dim bckgrndColor As Color = Color.White
For y As Integer = 0 To original.Height - 1
For x As Integer = 0 To original.Width - 1
If original.GetPixel(x, y).ToArgb <> bckgrndColor.ToArgb Then
If x < JPGminX Then
JPGminX = x
ElseIf x > JPGmaxX Then
JPGmaxX = x
End If
If y < JPGminY Then
JPGminY = y
ElseIf y > JPGmaxY Then
JPGmaxY = y
End If
End If
Next
Next
Dim rect As New Rectangle(JPGminX, JPGminY, JPGmaxX - JPGminX, JPGmaxY - JPGminY)
Dim cropped As Bitmap = original.Clone(rect, original.PixelFormat)
'create new image sized to add border around cropped image
Dim border As New Bitmap(cropped.Width + (2 * borderWidth), cropped.Height + (2 * borderWidth), cropped.PixelFormat)
'create a new graphics object
Dim g As Graphics = Graphics.FromImage(border)
'fill the graphics object with white color (for background)
Using myBrush As Brush = New SolidBrush(Color.White)
g.FillRectangle(myBrush, 0, 0, border.Width, border.Height)
End Using
'calculate position to place cropped image onto background image
Dim xImage As Integer = border.Width - cropped.Width - borderWidth
Dim yImage As Integer = border.Height - cropped.Height - borderWidth
'draw cropped image onto white background
g.CompositingMode = Drawing2D.CompositingMode.SourceOver
g.DrawImage(cropped, New Drawing.Point(xImage, yImage))
'dispose of unneeded graphics objects
original.Dispose()
cropped.Dispose()
g.Dispose()
'save our finished graphics file
border.Save(theImageFile, Imaging.ImageFormat.Png)
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
End Module
-[]-