簡體   English   中英

從DataGridview打印圖像?

[英]Print image from DataGridview?

我需要顯示來自dataGrid的圖片才能打印Page嗎? 如何設置圖像值?

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();

因此,我從帶有圖像列的上一個示例WinForm開始 (您可能需要查看編輯歷史記錄才能查看最初添加圖像列的方式)。

下一步是轉到“工具箱”,然后將“ PrintDocument”添加到表單中(在“打印”下)。 它會顯示在屏幕底部,然后右鍵單擊>查看“屬性”。 在“屬性”窗口中,單擊雷電圖標以顯示事件,然后雙擊“ 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

根據OP的注釋進行編輯:如果要將特定圖像傳遞給打印機,則可以找到一種將圖像位置傳遞給Bitmap構造函數的方法(例如Bitmap(imageLocation) ),也可以直接從DataGridViewImageColumn中獲取Bitmap ,就像這樣:

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);     
}
    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);

       }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM