Search code examples
c#lambdaevent-handlingprintdocument

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


Solution

  • 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