views:

306

answers:

3

When i am trying to print an image to a printer for a 700kb file it is sending 120MB of data over to the printer. I can see this because I see the printer spooling 120MB. why would this happend?

Here is the code for the PrintDocument.PrintPage

private void PrintPage(object sender, PrintPageEventArgs ev)
{
                sw.WriteLine("start,PrintPage," + DateTime.Now.ToLongTimeString());

                if (_running && _currentPage != null)
                {
                    RectangleF PrintArea = ev.Graphics.VisibleClipBounds;
                    RectangleF NewImageSize = new RectangleF();
                    Double SF = Convert.ToDouble(PrintArea.Width) / Convert.ToDouble(_currentPage.Width);
                    NewImageSize.Width = Convert.ToInt32(_currentPage.Width * SF);
                    NewImageSize.Height = Convert.ToInt32(_currentPage.Height * SF);

                    //You can influence the quality of the resized image 
                    ev.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                    ev.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
                    ev.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Default;
                    //Draw the image to the printer 
                    ev.Graphics.DrawImage(_currentPage, NewImageSize);
                    _currentPage.Dispose();
                    _currentPage = null;

                    //Trace.WriteLine(string.Format("IsFinished {0}, Count {1}", (_queue.IsFinished ? "True" : "False"), _queue.Count));
                    ev.HasMorePages = (!((_queue.IsFinished) && (_queue.Count == 0)));                    
                }
                sw.WriteLine("end,PrintPage," + DateTime.Now.ToLongTimeString());

            }
A: 

There is two reasons that the printed image is larger than the image file:

The image file is most likely compressed. If it's a JPEG image, it's usually compressed to about 1/10 - 1/20 of it's size. When you load the image it's uncompressed to something around 10 MB.

You are resizing the image when you are sending it to the printer. The resolution of the printer is often quite high. If the resolution of the image is something like 300 PPI and the resolution of the printer is something like 1000 PPI, the image will be resized to about ten times it's original size.

Guffa
Any suggestions for improving the performance? the goal is that has to be letter quaility
greektreat
A: 

I don't know much about .Net, but I believe the System.Drawing functions are built on top of GDI+. GDI+ does much of the rendering on the CPU and transfers bitmaps to the target device. On modern system, when targeting the graphics display, this is fine. Unfortunately, it doesn't leave much opportunity to take advantage of the device's capabilities (or those of its drivers).

Many printers, for example, support JPEG and PNG directly. When using GDI, rather than GDI+, you can determine if the printer had such support, and transfer the original JPEG and let the printer do the decompression and resizing. It's still some work, and you still need the slow method for those printers that don't have such support.

Adrian McCarthy
A: 

I'm having a similar problem, but I don't think it has anything to do with images. I'm sending a single text box to be printed and it's sending 89.8MB of data to the printer.

What can I do to shrink this? 90MB is a little ridiculous for printing two words.

I have a very basic page:

<StackPanel x:Name="LayoutRoot" Orientation="Vertical">
    <Button Content="Print" Click="Button_Click" Width="50" Height="20"/> 
    <TextBox x:Name="tbPrintableTextBox" Text="Printing test." Width="300" Height="20"/>
</StackPanel> 

And the code:

public partial class Print : Page
{
    PrintDocument pd;

    public Print()
    {
        InitializeComponent();

        // Create new a new PrintDocument object 
        pd = new PrintDocument();
        pd.PrintPage += new EventHandler<PrintPageEventArgs>(pd_PrintPage);

    }
    // Executes when the user navigates to this page.
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    void pd_PrintPage(object sender, PrintPageEventArgs e)
    { 
        e.PageVisual = tbPrintableTextBox; 
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    { 
        pd.Print("Print test");
    }


}
Paul