0

I've a code where i am making a bitmap image in runtime and i want to get the print of that image. My image is saved in a bitmap variable. and i want to get the print of that image. or in other words i want to access that variable in "Print code": which is in my case is the second section of code.

But i am not getting anything after the executing the "Print Code" here is my code for Image.

 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
var bmp = new Bitmap(this.picBoxCard.Width, this.picBoxCard.Height);
           this.picBoxCard.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
           e.Graphics.DrawImage(bmp,50,105);
           bmp.Save(@"Image.png", ImageFormat.Png); //Just saving the Image for making sure
}

code to execute for printing.

 private void picPrint_Click(object sender, EventArgs e)
        {

   PrintDialog pd = new PrintDialog();
                PrintDocument doc = new PrintDocument();
                doc.PrintPage += printDocument1_PrintPage;
               pd.Document = doc;
              if (pd.ShowDialog() == DialogResult.OK)
               {
                   doc.Print();
               }
}

Please guide me where I am making any mistake.

cooder_one
  • 301
  • 1
  • 2
  • 11
  • 2
    Possible duplicate of [Print images c#.net](https://stackoverflow.com/questions/5750659/print-images-c-net) – Ňɏssa Pøngjǣrdenlarp Mar 10 '18 at 17:35
  • Sorry But, that is another problem – cooder_one Mar 10 '18 at 17:42
  • so the bmp looks ok? Can you draw anything e.g. a rectangle at 50,104? What are the units? – TaW Mar 10 '18 at 18:24
  • yes the desired area is saved in a .png file – cooder_one Mar 10 '18 at 18:28
  • @TaW Actually I got a solution. but it is not reliable. I am saving this bitmap image in a folder and then taking the print of that .png file and deleting that file at end. – cooder_one Mar 10 '18 at 18:30
  • Well there really ought to be no difference! Also: What do you mean by 'not reliable' ?? Can you draw the bmp to a control on the form? Does the pbox do any drawing or why do you use drawtobitmap?? – TaW Mar 10 '18 at 18:44
  • actually, it's not a PictureBox it's a panel and that panel contains other controls too. like textbox and picture box etc. so I've to get the print of all panel area. so that is why i am using drawtobitmap. – cooder_one Mar 11 '18 at 10:54
  • 1
    You ought to try to answer not just one but all questions we ask.. – TaW Mar 11 '18 at 14:13
  • @TaW Ooh sorry. No reliable means it's not a good approach to save it and then deleting it. it'll take more time to execute.And sorry I don't understand the second question. – cooder_one Mar 12 '18 at 14:26
  • Q2&3 were _Can you draw anything e.g. a rectangle at 50,104? What are the units?_ They refer to the drawing/printing routine. But since it now seems that you can draw the image after saving it they are moot. The idea was that the position (50,104) might be off the page in some page unit.. - Q5 meant : Can you draw ithe image onto some other target other than the printdocument.from the code that created the image? - And indeed saving is not a good approach! – TaW Mar 12 '18 at 18:04
  • Your code works fine. Do use the debugger to test it! – TaW Mar 12 '18 at 21:37

0 Answers0