更改非托管映像字节数组的格式



我有这个代码(C#):

    unsafe private Bitmap Test()
    {
        Bitmap test = null;
        byte[] data = memRenderAll.CurrentData;
        fixed (byte* m_pBuffer = _data)
        {
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                Bitmap a = new Bitmap(720, 576, 2160, System.Drawing.Imaging.PixelFormat.Format24bppRgb, new IntPtr(m_pBuffer));
                a.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                test =(Bitmap) Image.FromStream(ms);
                a.Dispose();
            }
        }
        return _test;
    }

通过将流保存为:

 a.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

我得到了10:1的缩小尺寸。

有没有办法避免这种情况:

 a.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

并以某种方式在中指定

Bitmap a = new Bitmap(720, 576, 2160, System.Drawing.Imaging.PixelFormat.Format24bppRgb, new IntPtr(m_pBuffer));

因为我只想创建一次位图。

谢谢,

Andrew

我认为使用标准System.Drawing.在一行代码中执行操作是不可能的

您使用该构造函数:http://msdn.microsoft.com/en-us/library/zy1a2d14(v=vs.110).aspx您的代码:

new Bitmap(720, 576, 2160, System.Drawing.Imaging.PixelFormat.Format24bppRgb, new IntPtr(m_pBuffer));

这是什么意思?m_pBuffer使用固定宽度、高度、步长和像素格式指向的位图读取内存。你不能把它读成jpeg,因为jpeg-图像压缩格式。看看它:http://en.wikipedia.org/wiki/JPEG#JPEG_codec_example。Jpeg编解码器需要你所有的图像进行压缩,它不能压缩部分并在之后加入它们。

最新更新