繁体   English   中英

在.NET Compact Framework中将图像转换为1 bpp位图

[英]Convert image to 1 bpp bitmap in .net compact framework

我正在尝试将签名图像保存为1 bpp位图,以节省文件空间。 完整的.NET Framework具有枚举PixelFormat.Format1bppIndexed ,但.NET Compact Framework不支持它。

有没有人发现一种在Windows Mobile中实现此目标的方法?

过去,我必须这样做才能生成通过蓝牙打印的黑白报告(彩色或灰度图像对于打印机缓冲区而言过大)。 原来,我不得不使用本机代码创建图像。

这是一个片段:

private void CreateUnmanagedResources()
{
    // for safety, clean up anything that was already allocated
    ReleaseUnmanagedResources();

    bih = new BITMAPINFOHEADER();
    bih.biBitCount = 1;
    bih.biClrImportant = 0;
    bih.biClrUsed = 0;
    bih.biCompression = 0;
    bih.biHeight = m_cy;
    bih.biPlanes = 1;
    bih.biSize = (uint)(Marshal.SizeOf(typeof(BITMAPINFOHEADER)) - 8); 
    bih.biSizeImage = 0;
    bih.biWidth = m_cx;
    bih.biXPelsPerMeter = 0;
    bih.biYPelsPerMeter = 0;
    bih.clr2 = 0xffffff;
    bih.clr1 = 0x0;

    hDC = Win32.CreateCompatibleDC(IntPtr.Zero);
    pBits = IntPtr.Zero;
    hBitmap = Win32.CreateDIBSection(hDC, bih, 1, ref pBits, IntPtr.Zero, 0);
    hbmOld = Win32.SelectObject(hDC, hBitmap);
}

private void ReleaseUnmanagedResources()
{
    if (hbmOld != IntPtr.Zero)
        Win32.SelectObject(hDC, hbmOld);

    if(hBitmap != IntPtr.Zero)
        Win32.DeleteObject(hBitmap);

    if (hDC != IntPtr.Zero)
        Win32.DeleteDC(hDC);
}

然后,我使用Graphics.FromHdc获得了一个可管理的图形对象,可以将报告绘制到该对象上。

我确实使用BinaryWriter进行了保存,但是那是在CF 1.0天内,Bitmap类没有保存,因此您可以在那里自由地进行清理。

感谢您向正确的方向指点ctacke 我无法使用Bitmap类保存图像数据。 它不断抛出OutOfMemoryException 就像您建议的那样,我采取了使用BinaryWriter写入位图的方法。

我的最终解决方案返回一个字节数组,您可以选择该字节数组写入磁盘,保存到数据库,进行传输等。

class ImageHelper
{
    [StructLayout(LayoutKind.Sequential)]
    public struct BITMAPINFOHEADER
    {
        public BITMAPINFOHEADER(ushort bpp, int height, int width)
        {
            biBitCount = bpp;
            biWidth = width;
            biHeight = height;

            biSize = (uint)Marshal.SizeOf(typeof(BITMAPINFOHEADER));
            biPlanes = 1; // must be 1
            biCompression = 0; // no compression
            biSizeImage = 0; // no compression, so can be 0
            biXPelsPerMeter = 0;
            biYPelsPerMeter = 0;
            biClrUsed = 0;
            biClrImportant = 0;
        }

        public void Store(BinaryWriter bw)
        {
            Store(bw, null);
        }

        public void Store(BinaryWriter bw, uint[] colorPalette)
        {
            // Must maintain order for file writing
            bw.Write(biSize);
            bw.Write(biWidth);
            bw.Write(biHeight);
            bw.Write(biPlanes);
            bw.Write(biBitCount);
            bw.Write(biCompression);
            bw.Write(biSizeImage);
            bw.Write(biXPelsPerMeter);
            bw.Write(biYPelsPerMeter);
            bw.Write(biClrUsed);
            bw.Write(biClrImportant);

            // write color palette if 8 bpp or less
            if (biBitCount <= 8)
            {
                if (colorPalette == null)
                    throw new ArgumentNullException("bpp is 8 or less, color palette is required");

                uint paletteCount = BITMAPFILEHEADER.CalcPaletteSize(biBitCount) / 4;
                if (colorPalette.Length < paletteCount)
                    throw new ArgumentException(string.Format("bpp is 8 or less, color palette must contain {0} colors", paletteCount));

                foreach (uint color in colorPalette)
                    bw.Write(color);
            }
        }

        public uint biSize;
        public int biWidth;
        public int biHeight;
        public ushort biPlanes;
        public ushort biBitCount;
        public uint biCompression;
        public uint biSizeImage;
        public int biXPelsPerMeter;
        public int biYPelsPerMeter;
        public uint biClrUsed;
        public uint biClrImportant;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct BITMAPFILEHEADER
    {
        public BITMAPFILEHEADER(BITMAPINFOHEADER info, out uint sizeOfImageData)
        {
            bfType = 0x4D42;  // Microsoft supplied value to indicate Bitmap 'BM'
            bfReserved1 = 0;
            bfReserved2 = 0;

            // calculate amount of space needed for color palette
            uint paletteSize = CalcPaletteSize(info.biBitCount);

            bfOffBits = 54 + paletteSize; // default value + paletteSize

            // calculate size of image
            sizeOfImageData = (uint)(CalcRowSize(info.biWidth * info.biBitCount) * info.biHeight);
            bfSize = sizeOfImageData + bfOffBits;
        }

        private static int CalcRowSize(int bits)
        {
            return ((((bits) + 31) / 32) * 4);
        }

        public static uint CalcPaletteSize(int bpp)
        {
            // 8 bpp or less, needs an uint per color
            if (bpp <= 8)
                return 4 * (uint)Math.Pow(2, bpp);

            // no palette needed for 16bpp or higher
            return 0;
        }

        public void Store(BinaryWriter bw)
        {
            // Must maintain order for file writing
            bw.Write(bfType);
            bw.Write(bfSize);
            bw.Write(bfReserved1);
            bw.Write(bfReserved2);
            bw.Write(bfOffBits);
        }

        public ushort bfType;
        public uint bfSize;
        public short bfReserved1;
        public short bfReserved2;
        public uint bfOffBits;
    }

    public static byte[] GetByteArray(Bitmap image)
    {
        IntPtr hbmOld;
        IntPtr hBitmap;
        IntPtr hDC;

        // create infoheader
        BITMAPINFOHEADER bih = new BITMAPINFOHEADER(1, image.Height, image.Width);
        // set black and white for 1 bit color palette

        // create fileheader and get data size
        uint sizeOfImageData;
        BITMAPFILEHEADER bfh = new BITMAPFILEHEADER(bih, out sizeOfImageData);

        // create device context in memory
        hDC = Win32.CreateCompatibleDC(IntPtr.Zero);

        // create a 1 bpp DIB
        IntPtr pBits = IntPtr.Zero;
        hBitmap = Win32.CreateDIBSection(hDC, ref bih, 1, ref pBits, IntPtr.Zero, 0);

        // selet DIB into device context
        hbmOld = Win32.SelectObject(hDC, hBitmap);

        using (Graphics g = Graphics.FromHdc(hDC))
        {
             g.DrawImage(image, 0, 0);
        }

        byte[] imageData = new byte[sizeOfImageData];
        byte[] fileData;

        using (MemoryStream ms = new MemoryStream((int)bfh.bfSize))
        {
            using (BinaryWriter w = new BinaryWriter(ms))
            {
                bfh.Store(w);
                // store bitmapinfoheader with 1 bpp color palette for black and white
                bih.Store(w, new uint[] { (uint)0x0, (uint)0xffffff });

                // copy image data into imageData buffer
                Marshal.Copy(pBits, imageData, 0, imageData.Length);

                // write imageData to stream
                w.Write(imageData);

                w.Close();
            }

            fileData = ms.GetBuffer();
            ms.Close();
        }

        // select old object
        if (hbmOld != IntPtr.Zero)
            Win32.SelectObject(hDC, hbmOld);

        // delete memory bitmap
        if (hBitmap != IntPtr.Zero)
            Win32.DeleteObject(hBitmap);

        // delete memory device context
        if (hDC != IntPtr.Zero)
            Win32.DeleteDC(hDC);

        return fileData;
    }
}

即使在整个框架中,创建和保存双色调位图也是有问题的。

我以前写过一篇有关此问题的文章。

http://www.codeproject.com/KB/GDI-plus/BitonalImageConverter.aspx

我在紧凑框架的上下文中重新访问了此代码,并像您所做的那样发现枚举值不存在,因此您不能从头开始创建二进制图像。

我想知道您是否可以在紧凑型框架中加载现有的双色调图像。 如果您可以加载预先存在的双色调位图,则可以降低级别并将位图图像格式直接写入磁盘或内存流中,而不是使用GDI +对象,但是这样做并非易事。 。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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