C#学习教程:在C#中在单色位图上绘制文本生成的位图必须在热敏POS打印机上打印,因此位图必须为1bpp。我不擅长图形,所以我想找一些样本。这是我尝试过的:Bitmapbmp=newBitmap(300,300,PixelFormat.Format1bppIndexed);使用(Graphicsg=Graphics.FromImage(bmp)){Fontfont=newFont("Arial",20,FontStyle.Bold,GraphicsUnit.Point);g.Clear(Color.White);g.DrawString(文本,字体,Brushes.Black,0,0);}bmp.Save(@"c:xx.bmp",ImageFormat.Bmp);最后保存只是为了检查结果。使用此代码,我得到以下异常:无法从具有索引像素格式的图像创建图形对象。有没有办法将文本绘制到单色内存位图中?仅供参考:我需要这个,因为我愚蠢的POS打印机绘制的0与O完全相同,所以他们无法区分它们......试试这个:Bitmapbmp=newBitmap(300,300);使用(Graphicsg=Graphics.FromImage(bmp)){Fontfont=newFont("Arial",20,FontStyle.Bold,GraphicsUnit.Point);g.Clear(Color.White);g.DrawString("Hello",font,Brushes.Black,0,0);}System.Drawing.Imaging.BitmapDatabmpData=bmp.LockBits(新矩形(0,0,bmp.Width,bmp.Height),System.Drawing.Imaging.ImageLockMode.ReadOnly,System.Drawing.Imaging.PixelFormat.Format1bppIndexed);位图newBitmap=newBitmap(300,300,bmpData.Stride,System.Drawing.Imaging.PixelFormat.Format1bppIndexed,bmpData.Scan0);newBitmap.Save(@"c:xx.bmp");这是一个可能有帮助的链接:http://msdn.microsoft.com/en-us/library/zy1a2d14.aspx对你有用,需要详细了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
