Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I'm trying to add receipt printing to a POS project I'm working on
I've create a win form with an image and listview

I've added the below code for printing but it only gives me empty page

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace CrepeCity
{
    public partial class printPreview : Form
    {
        public printPreview()
        {
            InitializeComponent();
        }

        private void printPreview_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            CaptureScreen();
            printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
            printDocument1.Print();
        }
        Bitmap memoryImage;

        private void CaptureScreen()
        {
            Graphics myGraphics = this.CreateGraphics();
            Size s = this.Size;
            memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            memoryGraphics.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
        }


        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {

        }

        private void printDocument1_PrintPage_1(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(memoryImage, 0, 0);
        }
    }
}


What I have tried:

Also I've tried this code to print a specific panel and it also gives me an empty page

<pre lang="c#">  [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
        private Bitmap memoryImage;
        private void CaptureScreen()
        {
            Graphics mygraphics = this.CreateGraphics();
            Size s = this.Size;
            memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            IntPtr dc1 = mygraphics.GetHdc();
            IntPtr dc2 = memoryGraphics.GetHdc();
            BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
            mygraphics.ReleaseHdc(dc1);
            memoryGraphics.ReleaseHdc(dc2);
        }
        private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(memoryImage, 0, 0);
        }

        private void button102_Click_1(object sender, EventArgs e)
        {
            CaptureScreen();
            printDocument1.Print();
        }
Posted
Updated 15-Feb-17 2:39am
Comments
[no name] 15-Feb-17 8:12am    
Probably because you are capturing the screen and then doing nothing with the result.
Joey Arnanzo 15-Feb-17 9:57am    
could you please guide me with the code to print out the result?
Dave Kreskowiak 15-Feb-17 12:15pm    
You also didn't Dispose your Graphics object after you're done with them. Your code is leaking resources and will eventually crash your application and Windows.

1 solution

It works fine for me:
CaptureScreen();
memoryImage.Save(@"D:\Temp\aaaPix.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = printDocument1;
ppd.ShowDialog();
printDocument1.Print();
I get a JPG file containing my form, I see it in the print preview, and I see it on paper from the preview "Print" button and from the direct print command.
I'd check that any of your code is being executed via the debugger.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900