在C#中的单色位图上绘制文本

我的问题是我需要在单色位图上绘制文本。 生成的位图必须打印在热敏POS打印机上,因此位图必须为1bpp。

我的图形不好,所以我试图找一些样品。 这是我尝试过的:

Bitmap bmp = new Bitmap(300, 300, PixelFormat.Format1bppIndexed); using (Graphics g = Graphics.FromImage(bmp)) { Font font = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Point); g.Clear(Color.White); g.DrawString(text, font, Brushes.Black, 0, 0); } bmp.Save(@"c:\x\x.bmp", ImageFormat.Bmp); 

最后保存只是为了检查结果。 使用此代码,我得到以下exception:无法从具有索引像素格式的图像创建Graphics对象。

是否有任何方法可以将文本绘制到单色内存位图?

仅供参考:我需要这个,因为我的愚蠢POS打印机绘制的0与O完全相同,所以它们无法区分……

试试这个:

 Bitmap bmp = new Bitmap(300, 300); using (Graphics g = Graphics.FromImage(bmp)) { Font font = new Font("Arial", 20, FontStyle.Bold, GraphicsUnit.Point); g.Clear(Color.White); g.DrawString("Hello", font, Brushes.Black, 0, 0); } System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format1bppIndexed); Bitmap newBitmap = new Bitmap(300, 300, bmpData.Stride, System.Drawing.Imaging.PixelFormat.Format1bppIndexed, bmpData.Scan0); newBitmap.Save(@"c:\x\x.bmp"); 

这是一个可以提供帮助的链接: http : //msdn.microsoft.com/en-us/library/zy1a2d14.aspx