如何捕获WPF可视元素到文件 [英] How to capture WPF visual element to file
问题描述
亲爱的,
在我的应用程序中,我有不同的视觉元素,我的用户想在特定时间拍摄快照。
例如,如果我有一个名为myControl的控件,它嵌入了一个Image和一个文本注释,我想在文件中捕获myControl的全局内容到给定的宽度和高度。
关于如何捕获WPF视觉元素的任何建议和样本?
In my application I have different visual element that my user would like to make a snaphot at a certain time.
For exemple if I have a control named myControl which embedded an Image and an text annotation, I would like to capture in a file the global content of myControl to a given size in width and height.
Any advise and sample on how to capture the WPF visual element ?
推荐答案
对不起,代码是VB,但应该很容易转换。
Sorry the code is VB but should be easily converted.
首先,您需要将控件放在可以保存图像的网格中。 然后下面的代码将调用一个例程来获取图像的字节数组。
First you need to have your control in a grid that would have the image you want to save. Then the following code will call a routine to get a byte array of the image.
Dim screenshot As Byte() = GetScreenShot2(VisualGrid, 1, 90)
currentFrameNumber += 1
Dim fileName As String = theApplicationBinding.CaptureName + currentFrameNumber.ToString("D5") + ".jpg"
theApplicationBinding.currentCaptureTime = New DateTime(currentTime).ToString("HH:mm:ss")
theApplicationBinding.ProgressMax = currentFileBeingCaptured._outpoint - currentFileBeingCaptured._inPoint ' theMediaPlayer.NaturalDuration.TimeSpan.Ticks
theApplicationBinding.ProgressValue = 0 'theMediaPlayer.Position.Ticks
Dim fileStream As New FileStream(Path.Combine(theApplicationBinding.CaptureFolder, fileName), FileMode.Create, FileAccess.ReadWrite)
Dim binaryWriter As New BinaryWriter(fileStream)
' temp comment
binaryWriter.Write(screenshot)
binaryWriter.Close()
在上面的Visual Grid中是带有visual的网格。
In the above Visual Grid is the grid with the visual.
Private Function GetScreenShot2(source As Grid, scale As Double, quality As Integer) As Byte()
Dim actualHeight As Double = source.Height
Dim actualWidth As Double = source.Width
Dim renderHeight As Double = actualHeight * scale
Dim renderWidth As Double = actualWidth * scale
Dim renderTarget As New RenderTargetBitmap(CInt(renderWidth), CInt(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(actualWidth, actualHeight)))
End Using
renderTarget.Render(drawingVisual)
Dim jpgEncoder As New JpegBitmapEncoder()
jpgEncoder.QualityLevel = quality
jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget))
Dim imageArray As [Byte]()
Using outputStream As New MemoryStream()
jpgEncoder.Save(outputStream)
imageArray = outputStream.ToArray()
End Using
Return imageArray
End Function
以上是获取视觉(托管在网格中)并将其转换为字节数组的方法保存为jpg文件。
The above is the method to take the visual (hosted in the grid ) and convert it to a byte array which can then be save to a jpg file.
这篇关于如何捕获WPF可视元素到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!