如何将jpg文件列表转换为mjpg文件?

时间:2019-04-23 20:35:03

标签: vb.net jpeg

我需要获取.jpg文件列表,并将其合并为没有第三方.dll的.mjpg文件

我拥有的代码打开一个要流式传输的文件,并添加.mjpg标头,帧标头和图像,但是当我在诸如VLC媒体播放器的查看器中打开该文件时,该文件将无法运行。

   Private Sub Build_File(filepath As String)
    Dim oImage As Bitmap
    Dim fStream As FileStream
    Dim ms As MemoryStream
    Dim bHeader As Byte()
    Dim sFrameHeader As String
    Dim bFrameHeader As Byte()
    Dim bFrameFooter As Byte()

    Dim fileList = New DirectoryInfo(filepath).GetFiles("*.jpg").[Select](Function(o) o.Name).ToList()

    'encode the mjpg header 
    bHeader = Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\r\nContent-Type:multipart/ x - mixed - replace;boundary=myboundary\r\n")
    'put the frame header in a string to allow for replacing the length variable
    sFrameHeader = "myboundary\r\nContent-Type:image/jpeg\r\nContent - Length:[length]\r\n\r\n"
    'encode the frame footer
    bFrameFooter = Encoding.ASCII.GetBytes("\r\n")

    'open a file for streaming
    fStream = File.Open(Path.Combine(filepath, "test.mjpg"), FileMode.Append)
    'write the header for the mjpg
    fStream.Write(bHeader, 0, bHeader.Length)
    For Each sfile As String In fileList
        'get an image
        oImage = Image.FromFile(filepath & "\" & sfile)
        ms = New MemoryStream()
        'save the image into the memory stream
        oImage.Save(ms, ImageFormat.Jpeg)
        'build the frame header by putting the stream length in
        bFrameHeader = Encoding.ASCII.GetBytes(sFrameHeader.Replace("[length]", ms.Length))
        'add the header to the stream
        fStream.Write(bFrameHeader, 0, bFrameHeader.Length)
        'add the image to the stream
        ms.WriteTo(fStream)
        'add the footer to the stream
        fStream.Write(bFrameFooter, 0, bFrameFooter.Length)
        oImage.Dispose()
    Next
    fStream.Close()
End Sub

0 个答案:

没有答案