如何读取本地MJPEG文件

时间:2012-06-27 20:25:59

标签: c# .net xna jpeg mjpeg

我正在XNA中为Windows Phone编写应用程序...而且我想阅读存储在应用程序资源中的MJPEG流。我发现了许多如何通过WebHttpRequest从网站获取MJPEG的例子:

        // get the response
        HttpWebRequest request = (HttpWebRequest) asyncResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(asyncResult);

        try {
            // find boundary value
            string contentType = response.Headers["Content-Type"];

            if (!String.IsNullOrEmpty(contentType) && !contentType.Contains("=")) {
                throw new FormatException("Invalid content-type header.  The source is likely not returning a proper MJPEG stream.");
            }

            string boundary = response.Headers["Content-Type"].Split('=')[1].Replace("\"", "");
            byte[] boundaryBytes = Encoding.UTF8.GetBytes(boundary.StartsWith("--") ? boundary : "--" + boundary);

            using (Stream stream = response.GetResponseStream()) {
                using (BinaryReader binaryReader = new BinaryReader(stream)) {

                   // code to parse the MJPEG stream
                }
            }
        } finally {
            response.Close();
        }

...但这并不是我正在寻找的。以下是我从App的资源中读取MJPEG作为二进制流的代码:

    private void ParseMjpeg(object uri)
    {
        // what the corresponding code for determining the boundary bytes in my local MJPEG?
        byte[] boundaryBytes = ???

        using (Stream stream = TitleContainer.OpenStream(uri.ToString())) {
            using (BinaryReader binaryReader = new BinaryReader(stream)) {

                // code to parse the MJPEG stream as before: OK
            }
        }
    }

如何在上面的代码中确定边界字节?任何帮助都会非常感激。

谢谢, J3D

1 个答案:

答案 0 :(得分:1)

这应该会让你的生活变得更轻松。据我所知,DLL应该为您提供所需的大部分内容。

http://channel9.msdn.com/coding4fun/articles/MJPEG-Decoder