gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:52:24 25 4
gpt4 key购买 nike

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

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

最佳答案

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

这是一个片段:

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 类没有保存功能,因此您可以自由地进行清除。

关于c# - 在 .net 紧凑框架中将图像转换为 1 bpp 位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1836632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com