我需要从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);
}
我添加了代码,将图片保存到数据库SQL。创建一个保存为图片的QR码。我显示所有保存数据的代码。
创建新:
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();
答案 0 :(得分:0)
所以,我从我的previous example WinForm with an image column.开始(您可能需要查看编辑历史记录以查看我最初添加图像列的方式)。
下一步是转到您的工具箱,然后添加" PrintDocument"到您的表格(" Printing")。它将显示在屏幕底部,然后右键单击>查看属性。在“属性”窗口中,单击“迅雷”图标以显示事件,然后双击" 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转换为图像,然后打印。我在我的表单中对它进行了测试,它打印出的图像列很好。
或者,您可以查看一些示例CodeProjects here和here.
根据OP的评论进行编辑:如果您想将特定图片传递给打印机,您可以找到将图像位置传递给Bitmap构造函数的方法(例如{ {1}})或者您可以直接从DataGridViewImageColumn获取Bitmap,如下所示:
Bitmap(imageLocation)
答案 1 :(得分:0)
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);
}