Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to print the barcode to my Xprinter 360B with lable stickers 40*25 My problem is the barcode is not being printed on one sticker, and not even centered.

enter image description here

I am using this below code. any help would be much appriciated. This is the updated code:

C#
 public partial class Form1 : Form
{
    FontFamily ff;
    static Font font;
    Bitmap bitmap;

    public Form1()
    {
        InitializeComponent();
    }

    protected void btnPrint_Click(object sender, EventArgs e)
    {
        System.Drawing.Printing.PrintDocument myPrintDocument1 = new System.Drawing.Printing.PrintDocument();
        PrintDialog myPrinDialog1 = new PrintDialog();
        myPrintDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintPage);
        myPrinDialog1.Document = myPrintDocument1;

        if (myPrinDialog1.ShowDialog() == DialogResult.OK)
        {
            myPrintDocument1.Print();
        }
    }

    private void PrintPage(object o, PrintPageEventArgs e)
    {
        int width = pictureBox1.Width;
        int height = pictureBox1.Height;
        pictureBox1.Width = 140;
        pictureBox1.Height = 60;
        Bitmap myBitmap1 = new Bitmap(pictureBox1.Width, pictureBox1.Height);
        pictureBox1.DrawToBitmap(myBitmap1, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
        e.Graphics.DrawImage(myBitmap1, 0, 0);
        myBitmap1.Dispose();
        pictureBox1.Width = width;
        pictureBox1.Height = height;
    }

    [DllImport("gdi32.dll")]
    private static extern IntPtr AddFontMemResourceEx(IntPtr pbfont, uint cbfont, IntPtr pdv, [In] ref uint pcFonts);

    private void loadFont()
    {
        byte[] fontArray = fontTestDeleteWhenDone.Properties.Resources.BarcodeFont;
        int dataLength = fontTestDeleteWhenDone.Properties.Resources.BarcodeFont.Length;
        IntPtr ptrData = Marshal.AllocCoTaskMem(dataLength);
        Marshal.Copy(fontArray, 0, ptrData, dataLength);
        uint cFonts = 0;
        AddFontMemResourceEx(ptrData, (uint)fontArray.Length, IntPtr.Zero, ref cFonts);
        PrivateFontCollection pfc = new PrivateFontCollection();
        pfc.AddMemoryFont(ptrData, dataLength);
        // clear memory
        Marshal.FreeCoTaskMem(ptrData);
        ff = pfc.Families[0];
        font = new Font(ff, 60f, FontStyle.Regular);
    }
    private void allocFont(Font f, Control c, float size)
    {
        FontStyle fontStyle = FontStyle.Regular;
        c.Font = new Font(ff, 60f, fontStyle);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        loadFont();
        allocFont(font, this.txtCode, 60);
    }

    private void btn_Click(object sender, EventArgs e)
    {
        try
        {
            string barcode = txtCode.Text;

            bitmap = new Bitmap(barcode.Length * 40, 150);
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                Font ofont = new System.Drawing.Font("BarcodeFont.ttf", 20);
                PointF point = new PointF(2f, 2f);
                SolidBrush black = new SolidBrush(Color.Black);
                SolidBrush white = new SolidBrush(Color.White);
                graphics.FillRectangle(white, 0, 0, bitmap.Width, bitmap.Height);
                allocFont(font, this.pictureBox1, 80);
                graphics.DrawString(barcode, font, black, point);
            }
            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, ImageFormat.Png);
                pictureBox1.Image = bitmap;
                pictureBox1.Height = bitmap.Height;
                pictureBox1.Width = bitmap.Width;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}


What I have tried:

I tried to set the custom paper size. but it didn't work
PrinterSettings ps = new PrinterSettings();
PrintDocument myPrintDocument1 = new System.Drawing.Printing.PrintDocument();
myPrintDocument1.PrinterSettings = ps;
myPrintDocument1.DefaultPageSettings.PaperSize = new PaperSize("40 x 25 mm", 40, 25);
Posted
Updated 30-May-16 1:05am
v2
Comments
[no name] 31-May-16 0:54am    
Have you ever saved the bitmap to disk and examined its properties?
What are the properties of the printer / driver?
You should be able to send a "proper" image to the printer and have it print using any print method from any image viewer once the proper driver is selected.
If not, your image does not match the printer properties; i.e. DPI; printable areas.
Spending time on coding otherwise is a waste of time.
(Label printers usually do not scale).
plazzasele 31-May-16 3:03am    
The image i am printing is the barcode generated programmatically, I don't need to save the bitmap to disk I can save the code and whenever i need the barcode i can generate it again from the code. and the drivers of the printer are normal, supports various sticker sizes. I have a 3d part program that prints lables, It is working fine, but i want to add this task as part of my application
Member 12521232 16-Apr-19 1:42am    
Hi! Can you please tell me how u solved the problem? Now I'm getting trouble with it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900