C# 有没有合并图形的方法;Graphics.DrawImage到单个图像中?
在我的应用程序中,我生成一个条形码图像,该图像由用户使用OpenFileDialog上传的文件中的数据生成。我的目标是允许用户在屏幕上查看条形码数据和图像本身,打印并使他们能够将两者保存为PNG图像(作为单个图像)。我使用了PrintPageEventArgs、Graphics.DrawString、Graphics.DrawImage,2个PictureBox的-1是条形码的值,另一个是实际图像。我可以在屏幕上和打印时显示相关信息(我使用Get和Set方法从文件中检索数据): 保存图像: 展示 打印(预览) 拯救 我正在努力解决的问题是保存条形码,到目前为止,我只能保存图像,而不能保存信息/值。因此,我想知道是否有可能同时保存条形码值和图像以形成一张图像?我使用Graphics.DrawString作为值,使用Graphics.DrawImage作为实际条形码。我已经研究过了,但找不到任何解决方案,尽管它看起来很简单,显然不是 解决了的!(见公认答案) 如果您遇到困难,我在这里发布了一些代码和说明: 请注意,这可能不是最佳/有效的方法 希望这对其他处于我的位置的人有所帮助:)C# 有没有合并图形的方法;Graphics.DrawImage到单个图像中?,c#,winforms,picturebox,savefiledialog,C#,Winforms,Picturebox,Savefiledialog,在我的应用程序中,我生成一个条形码图像,该图像由用户使用OpenFileDialog上传的文件中的数据生成。我的目标是允许用户在屏幕上查看条形码数据和图像本身,打印并使他们能够将两者保存为PNG图像(作为单个图像)。我使用了PrintPageEventArgs、Graphics.DrawString、Graphics.DrawImage,2个PictureBox的-1是条形码的值,另一个是实际图像。我可以在屏幕上和打印时显示相关信息(我使用Get和Set方法从文件中检索数据): 保存图像: 展示
新位图(宽度、高度)
Graphics.FromImage
DrawImage
抽绳
图形
位图
请看编辑后的文章,我已经发布了新代码并删除了一些。它保存了一个黑色正方形,当我打开它时,它是空白的。显然我没有正确地执行该操作。@lornasw,您只是在订阅Paint事件后立即保存图像,但在调用Save之前没有机会引发此事件,因此不会执行Merge_Paint方法。您根本不需要在绘画活动中进行合并。它可以工作:D谢谢!好长时间我都没办法控制它D:
// 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!");
}
}
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
}
}