Question

In my application, I generate a barcode image which is produced from data located in a file the user uploads using OpenFileDialog. My aim is to allow the user to view both barcode data and the image itself on screen, printing and also enabling them to save both as a PNG image (as a single image). I have used PrintPageEventArgs, Graphics.DrawString, Graphics.DrawImage, 2 PictureBox's - 1 is the value of Barcode and the other the actual image. I can display relevant info on screen and when printing (I have used Get and Set Method in order to retrieve data from the file):

Saving Image:

// Link to Form1 (Global Variable)
Form1 f1 = new Form1();

private void BtnSave_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.Filter = "PNG Files (*.png) | *.png";
    saveFileDialog1.RestoreDirectory = true;
    saveFileDialog1.FileName = "Barcode";
    ImageFormat format = ImageFormat.Png;

    if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        bm.Save(saveFileDialog1.FileName, format); // ADDED LINE
        MessageBox.Show("Your image has been saved!");
    }
}

Displaying

Displaying

Printing (Preview)

Printing

Saving

Saving

The issue I am struggling with is saving the Barcode, so far I am only able to save the image and NOT the information/value. Therefore, I am wondering if it is possible to save BOTH Barcode value and image to form one single image? I have used Graphics.DrawString for the value and Graphics.DrawImage for the actual Barcode. I have researched and cannot find any sort of solution although it would seem simple enough, obviously not...

SOLVED! (See accepted answer)

In case you're struggling, I have posted some code and explanation here: Please Note, this may not be the best/efficient way

Bitmap bm = new Bitmap(497, 140);

// This method is called from the Save Click event
private void Merge_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    Pen blackPen = new Pen(Color.Transparent, 1);
    StringFormat strF = new StringFormat();

    using (g = Graphics.FromImage(bm)) // Using bitmap...
    {
        g.DrawImage(BarcodePic.Image, 0, 0); // Draw the BarcodePic to bm

        string bb = f1.GetSetBarcode; // Getting value of Barcode

        using (Font font = new Font("New Courier", 13, FontStyle.Regular)) // Declare font
        {
            strF.Alignment = StringAlignment.Center; // Set alignment of text
            Rectangle value = new Rectangle(0, 120, 496, 20); // Position text
            g.DrawString(bb.ToString(), font, Brushes.Black, value, strF); // Draw text
        }
    }

    SaveFileDialog saveFileDialog1 = new SaveFileDialog(); // Create instance
    saveFileDialog1.Filter = "PNG Files (*.png) | *.png";
    saveFileDialog1.RestoreDirectory = true;
    saveFileDialog1.FileName = "Barcode"; // Create default file name
    ImageFormat format = ImageFormat.Png;

    if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        bm.Save(saveFileDialog1.FileName, format); // Save bm

        MessageBox.Show("Your image has been saved!"); // Confirmation of action
    }
}

Hope this helps for anyone else who was in my position :)

Était-ce utile?

La solution

  1. create a Bitmap with new Bitmap(width, height)
  2. obtain a Graphics for it with Graphics.FromImage
  3. draw the barcode on this Graphics with DrawImage
  4. draw the text on this Graphics with DrawString
  5. dispose the Graphics
  6. save the Bitmap
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top