2

From the Page_load event of Window1 I'm calling a function of a public class and passing parameter as the same Window1. After the function is called, a thread is started. The thread is called on Page_Loaded event of Window1. The code is like this:

Private Sub StartScreen_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Try
            TmHeartbeat.Stop()
            TmHeartbeat.Interval = 600000
            TmHeartbeat.Start()
            ResetVariables()
            FormLoadSetting(Me)
            SetButtonProperty(btnEnglish, "\Images\ButtonBgBig.png", GetFormLabel("Start Screen", "Common", "Button English"))
            SetButtonProperty(btnSpanish, "\Images\ButtonBgBig.png", GetFormLabel("Start Screen", "Common", "Button Spanish"))
            SetDisplayTimer(DGT, False, Me, 1800000)

            MediaElement1.Source = New Uri(GetPath("Images\EnglishVideo\" & GetFormLabel("Start Screen", "Common", "Video")))

            If GetFormLabel("Start Screen", "Common", "Audio") <> "" Then
                PlayAudio(APlay, GetFormLabel("Start Screen", "Common", "Audio"))
            End If

            Dim AudioPlay As New System.Media.SoundPlayer

            Dim sc As New Screenshot
            sc.TakeScreenshot(Me)
        Catch ex As Exception
            AliLogFileEntry(TransactionType.ErrorOnForm, "Error In Function: StartScreen_Loaded: " & Me.Title & ", ErrorMessage: " & ex.Message)
        End Try
    End Sub

The function TakeScreenshot(Me) which is in class Screenshot is called. Along with the Screenshot class, I also have another class named GetScreenshot and a function TakeScreenshot1. The code for the class file is like this:

Imports System.IO
Imports System.Threading
Public Class Screenshot
    Public Sub TakeScreenshot(ByVal formname As Window)
        Dim GT As New GetScreenshot
        GT.source = formname
        Dim newThread As New Thread(AddressOf GT.TakeScreenshot1)
        newThread.Start()
    End Sub

End Class

Public Class GetScreenshot
    Public source As Window
    Public Function TakeScreenshot1()
        Thread.Sleep(2000)
        If OG.GetValue("TakeScreenshot") <> "0" Then
            Try
                AliLogFileEntry(TransactionType.System, "In Function: TakeScreenshot")
                Dim scale As Double = OG.GetValue("Screenshot_Scale") / 100
                AliLogFileEntry(TransactionType.System, "In Function: GetJpgImage")
                Dim renderHeight As Double = source.RenderSize.Height * scale
                Dim renderWidth As Double = source.RenderSize.Width * scale
                Dim renderTarget As New RenderTargetBitmap(CInt(Math.Truncate(renderWidth)), CInt(Math.Truncate(renderHeight)), 96, 96, PixelFormats.Pbgra32)
                Dim sourceBrush As New VisualBrush(source)
                Dim drawingVisual As New DrawingVisual()
                Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()
                Using drawingContext
                    drawingContext.PushTransform(New ScaleTransform(scale, scale))
                    drawingContext.DrawRectangle(sourceBrush, Nothing, New Rect(New Point(0, 0), New Point(source.RenderSize.Width, source.RenderSize.Height)))
                End Using
                renderTarget.Render(drawingVisual)
                Dim jpgEncoder As New JpegBitmapEncoder()
                jpgEncoder.QualityLevel = 100

                jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget))
                Dim _imageArray As [Byte]()
                Using outputStream As New MemoryStream()
                    jpgEncoder.Save(outputStream)
                    _imageArray = outputStream.ToArray()
                End Using
                Dim screenshot As Byte() = _imageArray
                Dim dir As DirectoryInfo = New DirectoryInfo("Screenshots")
                If Not dir.Exists Then dir = Directory.CreateDirectory(dir.FullName)
                Dim path As String = AppDomain.CurrentDomain.BaseDirectory
                Dim fileStream As New IO.FileStream("Screenshots\" & source.Title & ".jpg", FileMode.Create, FileAccess.ReadWrite)
                Dim binaryWriter As New IO.BinaryWriter(fileStream)
                binaryWriter.Write(screenshot)
                binaryWriter.Close()
            Catch ex As Exception
                AliLogFileEntry(TransactionType.ErrorOnForm, "Error In Function: TakeScreenshot , ErrorMessage: " & ex.Message)
            End Try
        End If
    End Function
End Class

When I debug this file, I get this error: enter image description here

And the error is generated on line

Dim sourceBrush As New VisualBrush(source)

Pleas help

Farhan Mukadam
  • 470
  • 9
  • 25
  • On what line is it actually raising the exception? `ex.ToString()` What is needed is `If Source.InvokeRequired Then Source.Invoke` (the pattern is a bit bigger but a quick search would give you more info) – NiKiZe Aug 26 '13 at 21:52

1 Answers1

3

You are using source from a different thread than it was created on. In order to use a windows control on a different thread you need to call back to the original thread. See How to: Make Thread-Safe Calls to Windows Forms Controls for more.

alstonp
  • 700
  • 6
  • 25