- create a
Bitmap
withnew Bitmap(width, height)
- obtain a
Graphics
for it withGraphics.FromImage
- draw the barcode on this
Graphics
withDrawImage
- draw the text on this
Graphics
withDrawString
- dispose the
Graphics
- save the
Bitmap
Is there a way to merge Graphics.DrawString & Graphics.DrawImage into a single image?
-
01-09-2022 - |
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
Printing (Preview)
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 :)
La solution