简体   繁体   中英

C# print image doesn't work

I'm trying print the bitmap with the code below but it doesn't work. I don't know why....

   private void btnImprimir_Click(object sender, EventArgs e)
    {
        PrintDocument pd = new PrintDocument();
        pd.PrintPage += PrintPage;
        pd.Print(); 
    }

    private void PrintPage(object o, PrintPageEventArgs e)
    {
        button1.Visible = false;
        button2.Visible = false;
        button3.Visible = false;
        button4.Visible = false;
        Bitmap bitmap = new Bitmap(this.Width, this.Height);
        this.DrawToBitmap(bitmap, this.ClientRectangle);
        Point loc = new Point(this.Width, this.Height);
        e.Graphics.DrawImage(bitmap, loc);
        button1.Visible = true;
        button2.Visible = true;
        button3.Visible = true;
        button4.Visible = true;
    }

use this way

Image bmIm;
private void PrintImage(Image img)
{
  bmIm = img;
  PrintDocument pd = new PrintDocument();
  pd.OriginAtMargins = true;
  pd.DefaultPageSettings.Landscape = true;
  pd.PrintPage += pd_PrintPage;
  pd.Print();
}
void pd_PrintPage(object sender, PrintPageEventArgs e)
{
  double cmToUnits = 100 / 2.54;
  e.Graphics.DrawImage(bmIm, 0, 0,(float)(27 * cmToUnits),(float)(18 * cmToUnits));
}

It looks like your event is not wired up properly (unless we're missing some code):

pd.PrintPage += this.PrintPage;

You were trying to assign pd_PrintPage which isn't in your code sample, so I don't know what it is supposed to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM