在我的应用程序中,我生成一个条形码图像,该图像是由用户使用OpenFileDialog上传的文件中的数据生成的。我的目的是允许用户在屏幕上查看条形码数据和图像本身,进行打印,并使他们既可以保存为PNG图像(也可以保存为单个图像)。我已经使用了PrintPageEventArgs,Graphics.DrawString,Graphics.DrawImage,2 PictureBox的-1是Barcode的值,其他是实际图像。我可以在屏幕上和打印时显示相关信息(我使用了Get和Set方法来从文件中检索数据):
保存图像:
// 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!");
}
}
显示中
打印(预览)
保存
我苦苦挣扎的问题是保存条形码,到目前为止,我只能保存图像,而不能保存信息/值。因此,我想知道是否可以同时保存条形码值和图像以形成一个图像?我已经将Graphics.DrawString用作值,并将Graphics.DrawImage用作实际的Barcode。我已经研究过,但找不到任何解决方案,尽管看起来似乎很简单,但显然不是...
解决了! (请参见已接受的答案)
万一您在挣扎,我在这里发布了一些代码和解释:
请注意,这可能不是最佳/有效方式
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
}
}
希望这对我所处的其他位置有所帮助:)
最佳答案
用Bitmap
创建一个new Bitmap(width, height)
用Graphics
为其获取Graphics.FromImage
用Graphics
在此DrawImage
上绘制条形码
用Graphics
在此DrawString
上绘制文本
处置Graphics
保存Bitmap
关于c# - 有没有一种方法可以将Graphics.DrawString和Graphics.DrawImage合并为一个图像?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20564150/