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
Solved

Batch printing and annotating TIF files

Posted on 2006-11-07
16
457 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

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…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

829 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