C# 从DataGridview打印图像?
我需要将图片从dataGrid显示到打印页面?如何设置图像的值C# 从DataGridview打印图像?,c#,.net,image,datagridview,C#,.net,Image,Datagridview,我需要将图片从dataGrid显示到打印页面?如何设置图像的值 private void printDocument_nahlad_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //e.Graphics.DrawImage(dataGridView_nahlad.Rows[0].Cells[12]........... = , 620, 55, 120, 120); } 我添加了代码,将
private void printDocument_nahlad_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//e.Graphics.DrawImage(dataGridView_nahlad.Rows[0].Cells[12]........... = , 620, 55, 120, 120);
}
我添加了代码,将图片保存到数据库SQL。创建一个保存为图片的QRcode。我显示所有保存数据的代码
创建新的:
public partial class form : Form
{
kasspoxTestEntities test;
private void btn_novy_vystup_Click(object sender, EventArgs e)
{
try
{
panel_vystup.Enabled = true;
cmb_dodavatel_vystup.Focus();
tab_vystup f = new tab_vystup();
test.tab_vystup.Add(f);
tabvystupBindingSource.Add(f);
tabvystupBindingSource.MoveLast();
}
private void btn_generovat_vystup_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(cmb_dodavatel_vystup.Text) & !string.IsNullOrEmpty(txt_cislo_palety_vystup.Text) & !string.IsNullOrEmpty(txt_hrubka_vystup.Text) & !string.IsNullOrEmpty(txt_sirka_vystup.Text) & !string.IsNullOrEmpty(txt_dlzka_vystup.Text) & !string.IsNullOrEmpty(txt_pocet_vystup.Text) & !string.IsNullOrEmpty(cmb_kvalita_vystup.Text) & !string.IsNullOrEmpty(cmb_druh_vystup.Text))
{
txt_objem_vystup.Text = (((Convert.ToInt32(txt_hrubka_vystup.Text) * Convert.ToInt32(txt_sirka_vystup.Text) * Convert.ToInt32(txt_dlzka_vystup.Text) * 0.000000001) * Convert.ToInt32(txt_pocet_vystup.Text)).ToString());
txt_objem_vystup.Text = Math.Round(double.Parse(txt_objem_vystup.Text), 3).ToString();
MessagingToolkit.QRCode.Codec.QRCodeEncoder encoder = new MessagingToolkit.QRCode.Codec.QRCodeEncoder();
encoder.QRCodeScale = 8;
Bitmap bmp = encoder.Encode(cmb_dodavatel_vystup.Text + ";" +
txt_cislo_palety_vystup.Text + txt_datum_vystup.Text + txt_smena_vystup.Text + ";" +
txt_hrubka_vystup.Text + txt_sirka_vystup.Text + txt_dlzka_vystup.Text + ";" +
txt_pocet_vystup.Text + ";" +
cmb_kvalita_vystup.Text + ";" +
txt_objem_vystup.Text + ";" +
cmb_druh_vystup.Text);
txt_link.Text = (cmb_dodavatel_vystup.Text + ";" +
txt_cislo_palety_vystup.Text + txt_datum_vystup.Text + txt_smena_vystup.Text + ";" +
txt_hrubka_vystup.Text + txt_sirka_vystup.Text + txt_dlzka_vystup.Text + ";" +
txt_pocet_vystup.Text + ";" +
cmb_kvalita_vystup.Text + ";" +
txt_objem_vystup.Text + ";" +
cmb_druh_vystup.Text);
pictureBox.Image = bmp;
btn_tlacit_vystup.Enabled = true;
lb_pozor.Visible = false;
MessageBox.Show("Kód bol vygenerovaný a paleta uložená", "Informácia", MessageBoxButtons.OK, MessageBoxIcon.Information);
tabvystupBindingSource.EndEdit();
tabskladBindingSource.EndEdit();
test.SaveChangesAsync();
因此,我从我的开始(您可能需要查看编辑历史以查看我最初添加图像列的方式)
下一步是转到工具箱,将“PrintDocument”添加到表单中(在“Printing”下)。它将显示在屏幕底部,然后右键单击>查看属性。在属性窗口中,单击thunderbolt图标以显示事件,然后双击“PrintPage”字段。这将创建一个PrintPage事件。使用以下代码:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Bitmap bm = new Bitmap(this.dataGridView1.Width, this.dataGridView1.Height);
dataGridView1.DrawToBitmap(bm, new Rectangle(0, 0, this.dataGridView1.Width, this.dataGridView1.Height));
e.Graphics.DrawImage(bm, 0, 0);
}
为方便起见,您还可以添加打印按钮,例如:
private void button1_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument1;
if (printDialog.ShowDialog() == DialogResult.OK)
{
printDocument1.Print();
}
}
这会将DataGridView转换为图像,然后打印。我在表格中测试了它,它很好地打印了图像列
或者,您可以查看一些示例代码项目和
根据OP的评论进行编辑:如果要将特定图像传递给打印机,可以找到将图像位置传递给位图构造函数的方法(例如位图(imageLocation)
),也可以直接从DataGridViewImageColumn获取位图,如下所示:
printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// figure out how you're selecting an image
// this selects the highlighted row
int rowIndex = dataGridView1.CurrentCell.RowIndex;
// select the image column
Bitmap bm = (Bitmap) dataGridView1.Rows[rowIndex].Cells[0].Value;
e.Graphics.DrawImage(bm, 0, 0);
}
你能澄清这个问题吗?您正在打印带有图像列的DataGridView页面,图像在数据组中可见,但打印时没有图像显示?我添加了一张图片。是的,图片在dagrid中可见,我需要打印。在文本示例中:e.Graphics.DrawString(dataGridView_nahlad.Rows[I].Cells[11]。FormattedValue.ToString(),新字体(“Microsoft Sans Serif”,12,FontStyle.Bold),笔刷。黑色,新点(350,高度+80));但是我不知道的图片@C.Helling
pictureBox.Image=bmp
——这是一个保存在图片框中的位图,但是它是如何保存在列中的?列“QRCODE”的DataGridViewCell类型是什么?我认为它实际上是DataGridView上的一个字节数组,而不是位图。这很好,但我想打印一个带有图像的单元格,而不是整个表格。我更新了我的答案。使用示例代码可能会容易得多,因为有多种方法可以满足您的要求,但想法是一样的:获取System.Drawing.Bitmap对象,并在printDocument\u PrintPage
函数中调用e.Graphics.DrawImage(bm,0,0)
。看看你目前是如何实现的,它会使你的代码更容易修复。我的代码没有修复你的问题吗?如果没有,你能具体告诉我问题是什么吗?你的错误是无效的。您能给我看一下将图像数据插入DataGridView的代码吗?我不可能告诉你为什么你的演员阵容无效,而我不知道你的演员阵容。此外,请编辑您的问题以包含其他信息,而不是将其作为新的“答案”发布
private void printDocument_nahlad_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// prvy štitok
// prvy stlpec
e.Graphics.DrawString(dataGridView_nahlad.Columns[0].HeaderText.ToString(), new Font("Microsoft Sans Serif", 12, FontStyle.Bold), Brushes.Black, new Point(75,55));
e.Graphics.DrawString(dataGridView_nahlad.Rows[0].Cells[0].FormattedValue.ToString(), new Font("Microsoft Sans Serif", 12, FontStyle.Bold), Brushes.Black, new Point(150, 55));
e.Graphics.DrawString(dataGridView_nahlad.Columns[1].HeaderText.ToString(), new Font("Microsoft Sans Serif", 12, FontStyle.Bold), Brushes.Black, new Point(75, 75));
e.Graphics.DrawString(dataGridView_nahlad.Rows[0].Cells[1].FormattedValue.ToString(), new Font("Microsoft Sans Serif", 12, FontStyle.Bold), Brushes.Black, new Point(150, 75));
// druhy stlpec
e.Graphics.DrawString(dataGridView_nahlad.Columns[5].HeaderText.ToString(), new Font("Microsoft Sans Serif", 12, FontStyle.Bold), Brushes.Black, new Point(290, 55));
e.Graphics.DrawString(dataGridView_nahlad.Rows[0].Cells[5].FormattedValue.ToString(), new Font("Microsoft Sans Serif", 12, FontStyle.Bold), Brushes.Black, new Point(350, 55));
e.Graphics.DrawString(dataGridView_nahlad.Columns[6].HeaderText.ToString(), new Font("Microsoft Sans Serif", 12, FontStyle.Bold), Brushes.Black, new Point(290, 75));
e.Graphics.DrawString(dataGridView_nahlad.Rows[0].Cells[6].FormattedValue.ToString(), new Font("Microsoft Sans Serif", 12, FontStyle.Bold), Brushes.Black, new Point(350, 75));
// treti stlpec
e.Graphics.DrawString(dataGridView_nahlad.Columns[9].HeaderText.ToString(), new Font("Microsoft Sans Serif", 12, FontStyle.Bold), Brushes.Black, new Point(450, 55));
e.Graphics.DrawString(dataGridView_nahlad.Rows[0].Cells[9].FormattedValue.ToString(), new Font("Microsoft Sans Serif", 12, FontStyle.Bold), Brushes.Black, new Point(520, 55));
e.Graphics.DrawString(dataGridView_nahlad.Columns[10].HeaderText.ToString(), new Font("Microsoft Sans Serif", 12, FontStyle.Bold), Brushes.Black, new Point(450, 75));
e.Graphics.DrawString(dataGridView_nahlad.Rows[0].Cells[10].FormattedValue.ToString(), new Font("Microsoft Sans Serif", 12, FontStyle.Bold), Brushes.Black, new Point(520, 75));
// vykreslenie čiar
e.Graphics.DrawLine(new Pen (Brushes.Black, 3), 75 ,190,740,190);
e.Graphics.DrawLine(new Pen(Brushes.Black, 3), 75, 45, 740, 45);
//CODE FOR MY IMAGE
// Bitmap bm = (Bitmap)dataGridView_nahlad.Rows[0].Cells[12].Value;
//e.Graphics.DrawImage(bm, 620, 55, 120, 120);
}