简体   繁体   中英

Change format of unmanaged image byte array

I have this code (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;
    }

By saving the stream as this:

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

I get a 10:1 reduction is size.

Is there a way of avoiding this:

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

and specifying it somewhere/somehow in:

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

as I would like to create the Bitmap only once.

thanks,

Andrew

I think its impossible todo in one code line using standard System.Drawing.

You use that constructor: http://msdn.microsoft.com/en-us/library/zy1a2d14(v=vs.110).aspx Your code:

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

What does it mean? Bitmap read memory pointed by m_pBuffer using fixed width, height, stride and pixel format. You can't read it as jpeg, because jpeg - image zipping fomat. Look on it: http://en.wikipedia.org/wiki/JPEG#JPEG_codec_example . Jpeg codec need all your image for ziping, it can't zip parts and join them after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM