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