我需要使用 ECS/POS 使用热敏打印机打印货币符号。货币符号在打印机的任何字符表中均不可用。所以,我所做的是将货币符号和金额文本转换为显示在图片框中的图像。但是,将此图片框图像(位图)发送到打印机,打印了 1e1e1e1e(实心)图像,将图像转换为灰度没有任何区别。以下是将文本转换为图像的代码:
public static Bitmap Convert_ValueToImage(string ValueText, string Fontname, int Fontsize)
{
//creating bitmap image
Bitmap ValueBitmap = new Bitmap(1, 1);
//FromImage method creates a new Graphics from the specified Image.
Graphics Graphics = Graphics.FromImage(ValueBitmap);
// Create the Font object for the image text drawing.
Font Font = new Font(Fontname, Fontsize);
// Instantiating object of Bitmap image again with the correct size for the text and font.
SizeF stringSize = Graphics.MeasureString(ValueText, Font);
ValueBitmap = new Bitmap(ValueBitmap, (int)stringSize.Width, (int)stringSize.Height);
Graphics = Graphics.FromImage(ValueBitmap);
//Draw Specified text with specified format
Graphics.DrawString(ValueText, Font, Brushes.Black, 0, 0);
Font.Dispose();
Graphics.Flush();
Graphics.Dispose();
return ValueBitmap; //return Bitmap Image
}
用法:
private void btnConvertTextToImage_Click(object sender, EventArgs e)
{
// Passing appropriate values to Convert Value to Image method
this.pictureBoxValueImage.Image = Convert_ValueToImage((CurrencySymbol + " 5,500:00"), "Verdana", 20);
this.pictureBoxValueImage.SizeMode = PictureBoxSizeMode.Normal;
// Convert Value Image to Bitmap
ValueImage = new Bitmap(this.pictureBoxValueImage.Image);
}
我究竟做错了什么?
将位图发送到打印机的代码:
private void Print_Bipmap()
{
int x;
int y;
int i;
int RowBytes;
byte n;
Color Pixels;
byte[,] ImageArray = new byte[bitmap.Width, bitmap.Height];
// Calculate output size
RowBytes = (bitmap.Width + 7) / 8;
// Generate body of array
for (y = 0; y < bitmap.Height; y++)
{ // Each row...
for (x = 0; x < (bitmap.Width / 8); x++)
{ // Each 8-pixel block within row...
ImageArray[x, y] = 0;
for (n = 0; n < 8; n++)
{ // Each pixel within block...
Pixels = bitmap.GetPixel(x * 8 + n, y);
if (Pixels.GetBrightness() < 0.5)
{
ImageArray[x, y] += (byte)(1 << (7 - n));
}
}
}
}
comport_writeByte(18); //DC2
comport_writeByte(42); //*
comport_writeByte((byte)bitmap.Height); //r
comport_writeByte((byte)RowBytes); //n
for (y = 0; y < bitmap.Height; y++)
{
for (x = 0; x < RowBytes; x++)
{
comport_writeByte(ImageArray[x, y]); //[d1 ..... dn]
}
}
}