[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Using PrintDocument to print other than text files

Posted on 2009-05-13
2
Medium Priority
?
1,166 Views
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
0
Comment
Question by:Ingres4719
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
ID: 24383584
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:
  http://www.java2s.com/Tutorial/CSharp/0460__GUI-Windows-Forms/PrintBMPimage.htm

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.
            connection.Open();
            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)
                {
                    writer.Write(outByte);
                    writer.Flush();
 
                    // 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);
                writer.Flush();
 
                // Close the output file.
                writer.Close();
                stream.Close();
            }
 
            // Close the reader and the connection.
            reader.Close();
            connection.Close();
 
            Image _Image = Image.FromStream(stream);
 
 
            return _Image;
}

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

649 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