在我的应用程序中,我生成一个条形码图像,该图像是根据用户使用OpenFileDialog上传的文件中的数据生成的。我的目标是允许用户在屏幕上查看条形码数据和图像本身,打印并使它们能够同时保存为PNG图像(作为单个图像)。我使用过PrintPageEventArgs,Graphics.DrawString,Graphics.DrawImage,2 PictureBox's - 1是条形码的值,另一个是实际图像。我可以在屏幕上和打印时显示相关信息(我使用了获取和设置方法来从文件中检索数据):
// 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!");
}
}
我正在努力解决的问题是保存条形码,到目前为止我只能保存图像并且不信息/值。因此,我想知道是否可以将 BOTH 条形码值和图像保存为单张图片?我使用Graphics.DrawString作为值,使用Graphics.DrawImage作为实际条形码。我已经研究过,找不到任何解决方案,虽然看起来很简单,显然不是......
如果你正在努力,我在这里发布了一些代码和解释: 请注意,这可能不是最佳/有效的方式
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
}
}
希望这有助于任何处于我职位的人:)
答案 0 :(得分:0)
Bitmap
new Bitmap(width, height)
Graphics
Graphics.FromImage
Graphics
DrawImage
上绘制条形码
Graphics
DrawString
上绘制文字
Graphics
Bitmap