0

tl;dr: I think my code sucks. Can you help me?

I haven't written a test case for this class yet, but I want to make sure I'm doing it right before I go home tonight.
The gist of the class is to provide a reusable ImagePrinter object that can print a scaled bitmap to the default printer without prompting the user, but still ensuring that the printer is OK to print to. Admittedly, it's a hodge-podge of code segments I've dug up from parts of the web and I only know that certain parts of it will work. printIfOK is the function that gets the ball rolling after the object has been instantiated.
So yeah. I'm probably doing wrong. Show me some love, will ya, DaniWeb?

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Printing;
using System.IO;

namespace ScreenGrabber
{
    /// <summary>
    /// Prints creates a reusable object that prints a scaled image to the default printer.
    /// </summary>
    public class ImagePrinter
    {
        private Image source=null;
        private PrintServer localPrintServer=null;
        private PrintQueue  defaultPrintQueue=null;
        public ImagePrinter()
        {
            setupPrintServerAndQueue();
        }
        private void setupPrintServerAndQueue(){
            localPrintServer = new LocalPrintServer();
            defaultPrintQueue=getDefaultPrintQueue();
        }
        private bool OKtoPrint(){
            if(isPrintQueueOK()){

                return true;
            }else{
                return false;
            }
        }
        private PrintQueue getDefaultPrintQueue(){

            PrintQueue pq=null;
            // Retrieving collection of local printer on user machine
            PrintQueueCollection localPrinterCollection =
                localPrintServer.GetPrintQueues();

            System.Collections.IEnumerator localPrinterEnumerator =
                localPrinterCollection.GetEnumerator();

            if (localPrinterEnumerator.MoveNext())
            {
                // Get PrintQueue from first available printer
                pq = (PrintQueue)localPrinterEnumerator.Current;
            }

            return pq;

        }
        private bool isPrintQueueOK(){
            if(defaultPrintQueue==null)
                return false;
            if (defaultPrintQueue.HasPaperProblem)
            {
                return false;
            }
            if (!(defaultPrintQueue.HasToner))
            {
                return false;
            }
            if (defaultPrintQueue.IsDoorOpened)
            {
                return false;
            }
            if (defaultPrintQueue.IsInError)
            {
                return false;
            }
            if (defaultPrintQueue.IsNotAvailable)
            {
                return false;
            }
            if (defaultPrintQueue.IsOffline)
            {
                return false;
            }
            if (defaultPrintQueue.IsOutOfMemory)
            {
                return false;
            }
            if (defaultPrintQueue.IsOutOfPaper)
            {
                return false;
            }
            if (defaultPrintQueue.IsOutputBinFull)
            {
                return false;
            }
            if (defaultPrintQueue.IsPaperJammed)
            {
                return false;
            }
            if (defaultPrintQueue.IsPaused)
            {
                return false;
            }
            if (defaultPrintQueue.IsTonerLow)
            {
                return false;
            }
            if (defaultPrintQueue.NeedUserIntervention)
            {
                return false;
            }
            return true;
        }
        public void printIfOK(Image newSource){
            source=new Bitmap(newSource);
            if(source!=null){
                if(OKtoPrint()){
                    print();
                }
                source.Dispose();   //Cleanup
            }
        }
        private void print()
        {
            // Create the document and attach an event handler.
            PrintDocument doc = new PrintDocument();
            doc.DefaultPageSettings.Landscape = true; //Yes, I'm certain I want landscape.
            doc.PrintController = new StandardPrintController(); //No, we don't want to ask the user.
            doc.PrintPage += this.printBitmap;  //Add the actual printing information
            doc.Print(); //Make it so.
            doc.Dispose(); //Hide the evidence.
        }

        private void printBitmap(object sender, PrintPageEventArgs e)
        {
            // Define the font.
            float x1 = e.MarginBounds.Left;
            float y1 = e.MarginBounds.Top;
            float x2 = e.MarginBounds.Right;
            float y2 = e.MarginBounds.Bottom;
            float horizontalRatio=e.MarginBounds.Width/source.Width;
            float verticalRatio=e.MarginBounds.Height/source.Height;
            float scale;

            if(horizontalRatio<verticalRatio){
                scale=horizontalRatio;
            }else{
                scale=verticalRatio;
            }

            // Draw an image.
            e.Graphics.DrawImage(source,x1, y1, source.Width*scale, source.Height*scale);
        }
    }
}

Edited by DeanMSands3

1
Contributor
1
Reply
2
Views
4 Years
Discussion Span
Last Post by DeanMSands3
0

OK, so I wrote the test case.
Not good, my friends. Not good.

The scaling doesn't work and it still tries to print even if the printer is unable.
I'll research this further.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.