我的问题是我需要在单色位图上绘制文本.生成的位图必须打印在热敏POS打印机上,因此位图必须为1bpp.
我的图形不好,所以我试着找一些样品.
这是我尝试过的:
最后保存只是为了检查结果.
使用此代码,我得到以下异常:无法从具有索引像素格式的图像创建Graphics对象.
有没有办法将文本绘制到单色内存位图?
仅供参考:我需要这个,因为我的愚蠢POS打印机绘制0与O完全相同,所以它们无法区分……
解决方法
试试这个:
@H_404_5@Bitmap bmp = new Bitmap(300,300);
using (Graphics g = Graphics.FromImage(bmp))
{
Font font = new Font("Arial",GraphicsUnit.Point);
g.Clear(Color.White);
g.DrawString("Hello",0);
}
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(new Rectangle(0,bmp.Width,bmp.Height),System.Drawing.Imaging.ImageLockMode.ReadOnly,System.Drawing.Imaging.PixelFormat.Format1bppIndexed);
Bitmap newBitmap = new Bitmap(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