Solved

Batch printing and annotating TIF files

Posted on 2006-11-07
16
454 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
Comment Utility
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
Comment Utility
Are any of the tiffs multi-page ?
0
 

Author Comment

by:jimtxas
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
any chance you got this in VB?
0
 
LVL 15

Expert Comment

by:ozymandias
Comment Utility
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
Comment Utility
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
Comment Utility
Did this work OK for you ?
0
 

Author Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Management Instrument 6 57
Trouble with References... 5 21
Why is "packages element is not declared"? 2 31
XML & .net 5 14
Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

763 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

11 Experts available now in Live!

Get 1:1 Help Now