简体   繁体   中英

Argument in Event Handler using Lambda

I've read a bunch of these similar topics and I can't seem to figure this one out.

Before I had something like this:

PrintDocument pd = new PrintDocument();
pd.PrintPage += PrintPage;
pd.PrinterSettings.PrinterName = photoPrinter;
pd.Print();

private void PrintPage(object sender, PrintPageEventArgs e)
{
  Image i = Image.FromFile(@"C:\workspace\FullSizeRender.jpg");
  Point p = new Point(0, 0);
  e.Graphics.DrawImage(i, p);
}

Because clearly, hard coding the file name to print doesn't work. I tried using a lambda expression which has come out to something like this. Which I know is wrong, because first of all it says I'm missing a ";". But where does the call to pd.Print(); go now?

PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = photoPrinter;
pd.PrintPage += (s, eventArgs) =>
    {
        Image i = Image.FromFile(newFile);
        Point p = new Point(0, 0);
        eventArgs.Graphics.DrawImage(i, p);
    }
pd.Print();

Thank you

lambda or not, this is still c#. remember to close statements with semicolon:

pd.PrintPage += (s, eventArgs) =>
{
    Image i = Image.FromFile(newFile);
    Point p = new Point(0, 0);
    eventArgs.Graphics.DrawImage(i, p);
};  // <-- here

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