Using TIFF on server side C#
Generally we always use jpeg or gif in our web app.But working with a TIFF is a bit tricky.Which needs to parse the each page and convert them to byte array and there by saving to temp location.Unlike any other images we should not use a tiff in HTML like
<img alt='' src=''/>
it can contain n number of images in itself. Generally it will be used for FAX purpose.We see many snippet of code to convert an PDF to TIFF for faxing. how to fetch every file in it to get a html in turn to convert to pdf?
if a TIFF image is having multiple pages. then how will we can get each of them? Here am going to show how to retrieve all the files in a TIFF and prepare a HTML out of it and finally sending it to an printer on network. Hope it reduces the burden of googling. Happy coding :)
// Finding the extension of a file
using System.Drawing;
using System.Drawing.Imaging;
string file = @"C:\Pavan\tifffolder\hello.tif";
string newextension = file.Substring(file.LastIndexOf("."),file.Length - file.LastIndexOf(".")).ToLower();
// Finding the each image of a TIFF file
if (ext == "tif" || ext == "tiff")
{
Image image = Bitmap.FromFile(file);
int pageCount = image.GetFrameCount(FrameDimension.Page);
if (pageCount > 1)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine('<html><body>');
for (int i = 0; i < pageCount; i++)
{
Image tiffImg = getTiffImage(file, i);
XImage img = XImage.FromGdiPlusImage(tiffImg);
string tiffpath = localReportPath + @"Reports\Savedfiles\" + ImageDocId + "_" + DateTime.Now.ToString("MMddyyyhhmmssfffff") + ".tif";
tiffImg.Save(tiffpath);
sb.AppendLine('<img alt=\'toprint\' src=\'' + tiffpath + '\'/>');
}
sb.AppendLine('</body></html>');
filename = ImageDocId + "_" + DateTime.Now.ToString("MMddyyyhhmmssfffff");
filePath = @"C:\Pavan\tifffolder\" + filename;
htmlFile = filePath + ".html";
fs = new FileStream(htmlFile, FileMode.Create, FileAccess.Write);
writer = new StreamWriter(fs, Encoding.UTF8);
writer.Write(sb.ToString());
writer.Close();
fs.Close();
}
}
//Finally giving it to printer
System.Drawing.Printing.PrintingPermission perm = new System.Drawing.Printing.PrintingPermission(System.Security.Permissions.PermissionState.Unrestricted);
perm.Level = System.Drawing.Printing.PrintingPermissionLevel.AllPrinting;
HTMLPrinter htmlPrinter = new HTMLPrinter();
htmlPrinter.Print(htmlFile, printerName, copies);
Reference: a HTML can be printed using SHDOCW & internet explorer component