gpt4 book ai didi

wpf - 从 ImageSource 创建视频

转载 作者:行者123 更新时间:2023-12-01 16:22:23 26 4
gpt4 key购买 nike

是否有任何简单的方法可以将 ImageSource 添加到堆栈并从中创建视频?

最佳答案

我已经上过这样的课了。我只需要提交我的“ImageInfo”,它是一个system.DrawingBitmap。这可以通过使用以下代码轻松创建:

Public Function WpfBitmapSourceToBitmap(ByVal source As BitmapSource) As System.Drawing.Bitmap
If source Is Nothing Then Return Nothing
Dim bmp As New System.Drawing.Bitmap(source.PixelWidth, source.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
Dim data As System.Drawing.Imaging.BitmapData = bmp.LockBits(New System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size), System.Drawing.Imaging.ImageLockMode.[WriteOnly], System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
source.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride)
bmp.UnlockBits(data)
Return bmp
End Function

然后我做了一个 AviClass 向其中添加帧并将其存储为具有预选编解码器(例如 XVid MPEG4)的 AVI 文件

    Public Class clsAviWriter
Inherits MAINInterface.TB.Imaging.Pia7.clsDspTemplate


Private cAvi As AviReaderWriter.AviFile.AviManager
Private AviStream As AviReaderWriter.AviFile.VideoStream
Private AudioStream As AviReaderWriter.AviFile.AudioStream


Private cFps As clsTbQueue
Private OldFpsDate As Date = Now




''' <summary>
''' The image object to paint graphical objects on it
''' </summary>
''' <value>descriptor of the image</value>
Public Overrides Property ImageInfo() As MAINInterface.TB.Imaging.Pia7.clsImageInfo
Get
Return Me._ImageInfo
End Get
Set(ByVal value As MAINInterface.TB.Imaging.Pia7.clsImageInfo)
Me._ImageInfo = value
Call WriteFrame()
Call Me.OnPropertyChanged(Me.Guid)
End Set
End Property

Private Sub WriteFrame()
Dim D As Date = Now
Dim Fps As Single


Me.cFps.Values.Add(D.Subtract(Me.OldFpsDate).Ticks)
Me.OldFpsDate = D

Me.cFps.Trim()

Fps = 1000 / New TimeSpan(Me.cFps.Average).TotalMilliseconds
Me.cFps.BufferSize = TB.Math.myTrim(Fps * 1, 1, 1000)


If Me.AviStream IsNot Nothing Then
Me.AviStream.AddFrame(Me._ImageInfo.Image.Clone)
End If
End Sub

Public Sub New()
Me.ClassDescription = "Write images into an avi file"
Me.cFps = New clsTbQueue(10)
End Sub



Private Sub InitializeAvi()
Dim W As String
Dim Fps As Single
Dim di As New IO.DirectoryInfo(TB.SystemMain.AppPath & "Avi\")
TB.FileSystem.CreateDirectories(di)

W = IO.Path.Combine(di.FullName, "Record_" & Now.Ticks.ToString("0") & ".avi")

Me.cAvi = New AviReaderWriter.AviFile.AviManager(W, False)

Dim Opts As New AviReaderWriter.AviFile.Avi.AVICOMPRESSOPTIONS
Opts.fccType = 0
Opts.fccHandler = 1684633208
Opts.dwKeyFrameEvery = 0
Opts.dwQuality = 0 '0 ... 10000
Opts.dwFlags = 8 'AVICOMRPESSF_KEYFRAMES = 4
Opts.dwBytesPerSecond = 0
Opts.lpFormat = 0
Opts.lpParms = New IntPtr(0)
Opts.cbParms = 3532
Opts.dwInterleaveEvery = 0


Fps = 1000 / New TimeSpan(Me.cFps.Average).TotalMilliseconds

'Dim bm1 As Bitmap
'bm1 = TB.Imaging.CreateReScaledImage(Me.pic.Image, New Size(Me.pic.Image.Width, Me.pic.Image.Height), False)
Me.AviStream = cAvi.AddVideoStream(Opts, Math.Floor(TB.Math.myTrim(Fps, 1, 50)), Me._ImageInfo.Image.Clone)

End Sub


Public Overrides Property Run() As Boolean
Get
Return Me._Run
End Get
Set(ByVal value As Boolean)
If Me._Run <> value Then
Me._Run = value
If Me._Run = True Then
Call InitializeAvi()
Else
If Me.cAvi IsNot Nothing Then
Me.cAvi.Close()
Me.cAvi = Nothing
Me.AviStream = Nothing
End If
End If
Call Me.OnPropertyChanged(Me.Guid)
End If
End Set
End Property


End Class

更多代码请看这里:http://www.wischik.com/lu/programmer/avi_utils.htmlMSDNhttp://www.codeproject.com/KB/audio-video/avigenerator.aspx

我已经发布了源代码来展示这样的序列的外观(上面的代码需要更多不公开的引用资料)。您可以看到,您只需要初始化、添加帧、存储 FPS 值并将其安全到硬盘。

此外,如果需要,您可以搜索 DirectShow 以查看全部工作原理。 DirectShow Pins

关于wpf - 从 ImageSource 创建视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6194002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com