Home > Back-end >  Save to File from picturebox
Save to File from picturebox

Time:11-23

The PictureBox has Image on it, Barcode

how to save to image file from PictureBox

this is the button to generate BARCODE

private void Generate_Barcode_Click(object sender, EventArgs e)
{
    string barcode = textBox1.Text;
    Bitmap bitmap = new Bitmap(barcode.Length * 40, 150);
    using (Graphics graphics = Graphics.FromImage(bitmap))
    {
        Font ofont = new System.Drawing.Font("IDAutomationHC39M Free Version", 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);
        graphics.DrawString("*"   barcode   "*", ofont, black, point);
    }
    using (MemoryStream ms = new MemoryStream())
    {
        bitmap.Save(ms, ImageFormat.Png);
        Barcode_Result.Image = bitmap;
        Barcode_Result.Height = bitmap.Height;
        Barcode_Result.Width = bitmap.Width;
    }
}

CodePudding user response:

Save the pictures drawn by PictrueBox to the specified folder.

Idea: Do not draw the picture directly in the PictrueBox, but draw it in the Image property of the PictrueBox, and then refresh the display.

For detailed operation method, please refer to the following code:

    private void button1_Click(object sender, EventArgs e)
    {
        string barcode = textBox1.Text;
        Bitmap bitmap = new Bitmap(barcode.Length * 40, 150);
        pictureBox1.Image = bitmap;
        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            Font ofont = new System.Drawing.Font("IDAutomationHC39M Free Version", 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);
            graphics.DrawString("*"   barcode   "*", ofont, black, point);
            pictureBox1.Invalidate(); // refresh
            bitmap.Save(@"xxx:/your path/temp.bmp");
        }
   }

test code:

enter image description here

enter image description here

  • Related