2

从 Window1 的 Page_load 事件中,我正在调用公共类的函数并将参数作为相同的 Window1 传递。调用该函数后,将启动一个线程。在 Window1 的 Page_Loaded 事件上调用该线程。代码是这样的:

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

TakeScreenshot(Me)类中的函数Screenshot被调用。除了 Screenshot 类,我还有另一个名为GetScreenshot和 function 的类TakeScreenshot1。类文件的代码是这样的:

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

当我调试这个文件时,我得到这个错误: 在此处输入图像描述

并且在线生成错误

Dim sourceBrush As New VisualBrush(source)

请帮忙

4

1 回答 1

3

您正在使用source与创建它的线程不同的线程。为了在不同的线程上使用 Windows 控件,您需要回调原始线程。有关更多信息,请参见如何:对 Windows 窗体控件进行线程安全调用。

于 2013-09-18T20:11:35.640 回答