Solved

Batch printing and annotating TIF files

Posted on 2006-11-07
16
459 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
[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
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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
 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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