0

I need to print a currency symbol with a thermal printer using ECS/POS. The currency symbol is not available in any of the printer's character tables. So, what I did was to convert the currency symbol and the amount text to an image which is displayed in a picturebox. However, sending this picturebox image (bitmap) to the printer, a 1e1e1e1e (solid) image was printed, converting the image to gray-scale did not make any difference. Below is code to convert text to image:

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
}

Usage:

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);       
}

What am I doing wrong?

Code to send bitmap to printer:

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]
        }
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Topa
  • 19
  • 5
  • Hey, I noticed you were using the wrong tags (css). I have edited them for you to more relevant tags but keep that in mind, because your question will not receive the attention you want if you have the wrong tags. – Jesse Mar 05 '18 at 14:39
  • Many thanks Jesse, I will keep that in mind. – Topa Mar 05 '18 at 14:44
  • How do you send the bitmap image to the printer? – Marc Balmer Mar 06 '18 at 13:39
  • @ Marc Balmer, Many thanks for your response. See Edit for the code to send bitmap to the printer. Please note that I have successfully used this to send other pictureBox bitmap images to printer. – Topa Mar 06 '18 at 20:10
  • I did save the Bitmap to file, but the saved image is a solid black image. This is probably what the printer is getting as well. To this end the code to send bitmap to printer is not the cause of the problem. What could be the problem? – Topa Mar 06 '18 at 22:12
  • Not enough info for an answer so I'll comment- If you are would consider another approach, then by some strange coincidence, I blogged about [how to print custom currency symbols on a receipt printer](https://mike42.me/blog/2018-03-how-to-print-custom-currency-symbols-on-a-receipt-printer) a few days before you wrote this question. I used `ESC *` to print the symbol inline, and the GitHub link in the blog post uses `ESC &` and `ESC %` to give superior results. All use column format bit images. – mike42 Mar 11 '18 at 10:32
  • You could use upper ascii codes or the $ sign to define a new character for your currency code (unless it doesn't fit in one singe character). The command is `ESC & y c1` or `\x1B\x26ycl` – AaA Mar 13 '18 at 03:00

0 Answers0