0

I guess I miss something when it comes to printing files. I read all bytes out of a png image and send them to printer's job stream. However I get bytes printed out and not the image itself. I guess I need proper print format.

This is the link containing code I am using:

http://support.microsoft.com/kb/322091/en

Any ideas?

dev hedgehog
  • 8,698
  • 3
  • 28
  • 55

2 Answers2

2

Sending raw data to a printer doesn't mean just dumping the contents of a file to the printer queue. To send raw data to a printer you would need to be sending PCL, PS or some other equivalent data which tells the printer how to print your document.

You'll likely need to use the System.Drawing.Printing.PrintDocument class.

Edit: There is a good example of how to print an image here on SO:

Printing image with PrintDocument. how to adjust the image to fit paper size

PrintDocument pd = new PrintDocument();
pd.DefaultPageSettings.PrinterSettings.PrinterName = "Printer Name";
pd.DefaultPageSettings.Landscape = true; //or false!
pd.PrintPage += (sender, args) =>
{
    Image i = Image.FromFile(@"C:\...\...\image.jpg");
    Rectangle m = args.MarginBounds;

    if ((double)i.Width / (double)i.Height > (double)m.Width / (double)m.Height) // image is wider
    {
        m.Height = (int)((double)i.Height / (double)i.Width * (double)m.Width);
    }
    else
    {
        m.Width = (int)((double)i.Width / (double)i.Height * (double)m.Height);
    }
    args.Graphics.DrawImage(i, m);
};
pd.Print();
Community
  • 1
  • 1
Ashigore
  • 4,618
  • 1
  • 19
  • 39
  • Thanks. Print image was just an example. However the app itself accepts any file path. Do you have an idea how to print any file or at least try to print? – dev hedgehog Apr 29 '14 at 08:03
0

I think you can use my code here:

    //Print Button Event Handeler
    private void btnPrint_Click(object sender, EventArgs e)
    {
        PrintDocument pd = new PrintDocument();
        pd.PrintPage += PrintPage;
        //here to select the printer attached to user PC
        PrintDialog printDialog1 = new PrintDialog();
        printDialog1.Document = pd;
        DialogResult result = printDialog1.ShowDialog();
        if (result == DialogResult.OK)
        {
            pd.Print();//this will trigger the Print Event handeler PrintPage
        }
    }

    //The Print Event handeler
    private void PrintPage(object o, PrintPageEventArgs e)
    {
        try
        {
            if (File.Exists(this.ImagePath))
            {
                //Load the image from the file
                System.Drawing.Image img = System.Drawing.Image.FromFile(@"C:\myimage.jpg");

                //Adjust the size of the image to the page to print the full image without loosing any part of it
                Rectangle m = e.MarginBounds;

                if ((double)img.Width / (double)img.Height > (double)m.Width / (double)m.Height) // image is wider
                {
                    m.Height = (int)((double)img.Height / (double)img.Width * (double)m.Width);
                }
                else
                {
                    m.Width = (int)((double)img.Width / (double)img.Height * (double)m.Height);
                }
                e.Graphics.DrawImage(img, m);
            }
        }
        catch (Exception)
        {

        }
    }
Anas Naguib
  • 1,006
  • 11
  • 12