Using PrintDocument to print other than text files

Posted on 2009-05-13
Last Modified: 2012-05-06
Has anybody ever used PRINTDOCUMENT to print anything else besides TEXT files? I want to print the contents of a control (picturebox1.image) and I can't determine a good solution. The image is stored in a SQL Server 2008 database table
Question by:Ingres4719
LVL 21

Accepted Solution

MogalManic earned 125 total points
Comment Utility
It should be relativly straight forward.  Just load the image from the blob in the database and draw it to the graphics object on the PrintPage event.

Here is an example that prints an image from a file:

Your only difference is that you need to build the Image object from the BLOB in SqlServer 2008 which you can do like this:

private Image LoadImage(SqlConnection connection)


// Assumes that connection is a valid SqlConnection object.

            SqlCommand command = new SqlCommand(

              "SELECT pub_id, logo FROM pub_info", connection);

            // Writes the BLOB to a file (*.bmp).

            Stream stream;

            // Streams the BLOB to the FileStream object.

            BinaryWriter writer;

            // Size of the BLOB buffer.

            int bufferSize = 100;

            // The BLOB byte[] buffer to be filled by GetBytes.

            byte[] outByte = new byte[bufferSize];

            // The bytes returned from GetBytes.

            long retval;

            // The starting position in the BLOB output.

            long startIndex = 0;

            // The publisher id to use in the file name.

            string pubID = "";

            // Open the connection and read data into the DataReader.


            SqlDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess);

            while (reader.Read())


                // Get the publisher id, which must occur before getting the logo.

                pubID = reader.GetString(0);

                // Create a file to hold the output.

                stream = new MemoryStream();

                writer = new BinaryWriter(stream);

                // Reset the starting byte for the new BLOB.

                startIndex = 0;

                // Read bytes into outByte[] and retain the number of bytes returned.

                retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);

                // Continue while there are bytes beyond the size of the buffer.

                while (retval == bufferSize)




                    // Reposition start index to end of last buffer and fill buffer.

                    startIndex += bufferSize;

                    retval = reader.GetBytes(1, startIndex, outByte, 0, bufferSize);


                // Write the remaining buffer.

                writer.Write(outByte, 0, (int)retval - 1);


                // Close the output file.




            // Close the reader and the connection.



            Image _Image = Image.FromStream(stream);

            return _Image;


Open in new window


Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now