在.net紧凑框架中将图像转换为1 bpp位图

发布于 2024-08-13 03:09:11 字数 182 浏览 3 评论 0原文

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

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

I have an image of a signature I am trying to save as 1 bpp bitmap to save file space. The full .NET Framework has the enum PixelFormat.Format1bppIndexed, but the .NET Compact Framework does not supported it.

Has any one discovered a way to accomplish this in Windows Mobile?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一指流沙 2024-08-20 03:09:11

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

下面是一个片段:

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 类没有 Save,所以你在那里是自由和清晰的。

I had to do this in the past for generating black & white reports printed via Bluetooth (color or greyscale images were too large for the printer's buffer). Turned out I had to create the images using native code.

Here's a snippet:

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);
}

I then used Graphics.FromHdc to get a managed graphics object that I could paint the report onto.

I did saving with a BinaryWriter, but that was in CF 1.0 days when the Bitmap class didn't have a Save, so you're free and clear there.

笑脸一如从前 2024-08-20 03:09:11

感谢您为我指明了正确的方向,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;
    }
}

Thanks for pointing me in the right direction, ctacke.
I was unable to use the Bitmap class to save the image data. It continually threw an OutOfMemoryException. I resorted to writing the bitmap out using a BinaryWriter, like you suggested.

My end solution returns a byte array, with which you can choose to write to disk, save to a database, transmit, etc.

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;
    }
}
病女 2024-08-20 03:09:11

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

我之前写过一篇文章讨论这个涉及到的问题。

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

我在紧凑框架的上下文中重新访问了这段代码,并发现枚举值不存在,因此您无法从头开始创建双色调图像

我很想知道您是否可以在紧凑框架中加载预先存在的双色调图像。如果您可以加载预先存在的双色调位图,那么可能可以进入较低级别并将位图图像格式直接写入磁盘或内存流,而不是使用 GDI+ 对象,但这样做并不简单。

Creating and saving a bitonal bitmap is problematic even in the full framework.

I previously authored an article on this issues involved.

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

I revisited this code in the context of the compact framework and discovered as you did that the enum value does not exist, so you can't create a bitonal image from scratch.

I'd be interested to know if you can load pre-existing bitonal images in the compact framework. If you can load pre-existing bitonal bitmaps, then it might be possible to go lower level and write the bitmap image format to disk or a memory stream directly, rather than use the GDI+ objects, but it would be non-trivial to do so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文