Solved

Batch printing and annotating TIF files

Posted on 2006-11-07
16
455 Views
Last Modified: 2012-06-27
What is the most efficient way to batch annotate and print group 4 tiff image files in VB.NET 2005.

I have multiple images in a folder that I need to add a couple lines of text at the top of the page then print the image in native size on a specific printer.

Can you give me code examples that will do this in the most efficient manner?
0
Comment
Question by:jimtxas
  • 11
  • 4
16 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 17951389
When you say native size do you mean full/actual size ?
What if the images are smaller or larger than the paper size in the printer ?
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17951393
Are any of the tiffs multi-page ?
0
 

Author Comment

by:jimtxas
ID: 17951417
yes... actual size...  i do not want to adjust page size based on printer and no multipage tiffs...
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17951430
OK. So if you have a tif image that is A3 you want it printed across 4 pages of A4 ?

Are all the tifs A4 portrait, i.e. are these scans or faxes of a4 pages ?
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17951441
The annotatoin should be pretty straight forward.
You load the tif image and then get a graphics object from the tif and use the DrawSyring method of the Graphics object to write the text onto the tif image.

The printing can get a bit tricky though.

If you were happy for the printing to just be the best fit of the image onto the paper available in the selected printer then it shouldn't be too hard.
0
 

Author Comment

by:jimtxas
ID: 17951474
if it's legal size paper and letter is in the tray, just print on the letter paper as legal size (understanding that the bottom would be cut off)

On the annotation, please provide the code needed to load an image from a file path, annotate, then send to printer...

0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17951693
I don't have an example of a class4 tif I can test with.
Can you email me one to : ozymandias[at]chainreactors[dot]co[dot]uk.

Also, any idea what you want printed at the top ?
How many lines of text etc ?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 15

Expert Comment

by:ozymandias
ID: 17951805
Here is the code. It works with Group 3 tiffs but I cannot test with group 4.

First here's a class that is a PrintDocument that holds a tif :

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;

      public class TifDocument : System.Drawing.Printing.PrintDocument
      {

            protected Image _Image = null;
            

            public TifDocument(string pathToTif) : base()
            {
                  Bitmap b = new Bitmap(pathToTif);
                  this._Image = this.ConvertToRGB(b);
                  this.PrintPage += new PrintPageEventHandler(TifDocument_PrintPage);
                  
            }

            protected Bitmap ConvertToRGB(Bitmap original) {
                  // .NET does not support class 4 tiff files or any Bitonal (one-bit-per-pixel)
                  // image formats, so we have to cheat a bit
                  // code nicked from http://www.codeproject.com/cs/media/BitonalImageConverter.asp
                  Bitmap newImage = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb);
                  newImage.SetResolution(original.HorizontalResolution, original.VerticalResolution);
                  Graphics g = Graphics.FromImage(newImage);
                  g.DrawImageUnscaled(original, 0, 0);
                  g.Dispose();
                  return newImage;
            }

            private void TifDocument_PrintPage(object sender, PrintPageEventArgs e) {
                  Graphics g = e.Graphics;
                  if (this._Image != null){
                        g.DrawImage(this._Image, e.PageBounds);
                  }
                  g.DrawString("This is a line of text added at the top of the document.",new Font("Arial", 14f), Brushes.Black, e.PageBounds.Left + 10, e.PageBounds.Top + 10);
                  e.HasMorePages = false;
            }
      }
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17951808
Here's an example of how to print a tif :

TifDocument doc = new TifDocument(@"c:\temp\test.tif");
PrintDialog pd = new PrintDialog();
pd.Document = doc;
if(pd.ShowDialog() == DialogResult.OK){
      doc.Print();
}

and here's how to print preview it :

TifDocument doc = new TifDocument(@"c:\temp\test.tif");
PrintPreviewDialog ppd = new PrintPreviewDialog();
ppd.Document = doc;
ppd.ShowDialog();
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17951811
The text you wanted added is added by the class when the document is printed as an overlay.
The actual tif image itself is not modified.
0
 

Author Comment

by:jimtxas
ID: 17953214
any chance you got this in VB?
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17954378
Here is the same class in VB. My VB is a bit rusty I'm afraid but it seems to work fine :

Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Printing

Public Class TifDocument
    Inherits System.Drawing.Printing.PrintDocument

    Protected _Image As Image

    Public Sub New(ByVal pathToTif As String)

        Dim b As Bitmap = New Bitmap(pathToTif)
        Me._Image = Me.ConvertToRGB(b)

    End Sub



    Protected Function ConvertToRGB(ByVal original As Bitmap) As Image

        ' .NET does not support class 4 tiff files or any Bitonal (one-bit-per-pixel)
        ' image formats, so we have to cheat a bit
        ' code nicked from http://www.codeproject.com/cs/media/BitonalImageConverter.asp
        Dim newImage As Bitmap = New Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb)
        newImage.SetResolution(original.HorizontalResolution, original.VerticalResolution)
        Dim g As Graphics = Graphics.FromImage(newImage)
        g.DrawImageUnscaled(original, 0, 0)
        g.Dispose()
        ConvertToRGB = newImage

    End Function

    Private Sub TifDocument_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles MyBase.PrintPage
        Dim g As Graphics = e.Graphics
        If Not Me._Image Is DBNull.Value Then
            g.DrawImage(Me._Image, e.PageBounds)
        End If
        g.DrawString("This is a line of text added at the top of the document.", New Font("Arial", 14.0F), Brushes.Black, e.PageBounds.Left + 10, e.PageBounds.Top + 10)
        e.HasMorePages = False

    End Sub
End Class
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 500 total points
ID: 17954386
And these should work in VB :

Dim doc As TifDocument = new TifDocument(@"c:\temp\test.tif")
Dim pd As PrintDialog = new PrintDialog()
pd.Document = doc
If pd.ShowDialog() Is DialogResult.OK Then
     doc.Print()
End If

and here's how to print preview it :

Dim doc As TifDocument = new TifDocument(@"c:\temp\test.tif")
Dim ppd As PrintPreviewDialog = new PrintPreviewDialog()
ppd.Document = doc
ppd.ShowDialog()
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 17959001
Did this work OK for you ?
0
 

Author Comment

by:jimtxas
ID: 17962696
haven't had a chance to try it yet... will very soon.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

16 Experts available now in Live!

Get 1:1 Help Now