Paint Text to PictureBox

Andrew Klasen 1 Reputation point
2023-01-05T18:34:06.33+00:00

Hi everyone I am making inventory management software with the ability to create/print barcodes. My form looks like this:

276653-image.png

Upon generating a barcode (with the data being that is in the text box). I also would like that data to be printed under the barcode. This is my code:

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
  
namespace InventoryManagementSystem  
{  
    public partial class Barcode : Form  
    {  
        public Barcode()  
        {  
            InitializeComponent();  
        }  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            Zen.Barcode.Code128BarcodeDraw barcode = Zen.Barcode.BarcodeDrawFactory.Code128WithChecksum;  
            myPicturebox.Image = barcode.Draw(textBox1.Text, 50);  
              
              
  
        }  
  
        private void button2_Click(object sender, EventArgs e)  
        {  
            Zen.Barcode.CodeQrBarcodeDraw qrcode = Zen.Barcode.BarcodeDrawFactory.CodeQr;  
            myPicturebox.Image = qrcode.Draw(textBox2.Text, 50);  
  
        }  
        private void myPrintDocument2_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)  
        {  
            Bitmap myBitmap1 = new Bitmap(myPicturebox.Width, myPicturebox.Height);  
              
            myPicturebox.DrawToBitmap(myBitmap1, new Rectangle(0, 0, myPicturebox.Width, myPicturebox.Height));  
            e.Graphics.DrawImage(myBitmap1, 0, 0);  
            myBitmap1.Dispose();  
        }  
        private void btnPrint_Click(object sender, EventArgs e)  
        {  
            System.Drawing.Printing.PrintDocument myPrintDocument2 = new System.Drawing.Printing.PrintDocument();  
            PrintDialog printDialog1 = new PrintDialog();  
            myPrintDocument2.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(myPrintDocument2_PrintPage);  
            printDialog1.Document = myPrintDocument2;  
            if (printDialog1.ShowDialog() == DialogResult.OK)  
            {  
                myPrintDocument2.Print();  
            }  
        }  
  
        private void btnClose_Click(object sender, EventArgs e)  
        {  
            this.Close();  
        }  
  
        private void btnClear_Click(object sender, EventArgs e)  
        {  
            textBox1.Text = "";  
            textBox2.Text = "";  
              
        }  
    }  
  
}  
  

What would be the best way to paint whatever textbox1.text is on the picture box, under the barcode? Thanks!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,097 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 45,996 Reputation points
    2023-01-05T20:37:43.397+00:00

    So you want to place a watermark on top of an image that you're displaying in the picture box? To do that you'd have to get back to the image (PictureBox.Image) if you don't already have it. Then use Graphics.FromImage to get the graphics context. Then use DrawString to write the text to the image. You'll have to determine what formatting to use (font, size, etc) and you'll also need to determine the position within the image. That is specific to your requirements. Once you're done you save the modified image (either temporarily or to a memory stream) and then assign it back to the picture box.

    This is a common request so you can google for the code needed but one possible solution is provided here.

    0 comments No comments

  2. Jack J Jun 24,276 Reputation points Microsoft Vendor
    2023-01-06T02:09:46.227+00:00

    @Andrew Klasen , Welcome to Microsoft Q&A, I recommend that you use the picturebox_paint event to Paint Text to PictureBox.

    Here is a code example you could refer to.

     private void pictureBox1_Paint(object sender, PaintEventArgs e)  
            {  
                if(pictureBox1.Image!=null)  
                {  
                    using (Font myFont = new Font("Arial", 14))  
                    {  
                        e.Graphics.DrawString("Hello .NET Guide!", myFont, Brushes.Green, new Point(2, 2));  
                    }  
                }  
                  
            }  
    

    Result:

    276732-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments