using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
class Program
{
private static WebBrowser InitialiseBrowser(string source)
{
// create a hidden web browser, which will navigate to the page
WebBrowser browser = new WebBrowser();
browser.ScrollBarsEnabled = false; // we don't want scrollbars on our image
browser.ScriptErrorsSuppressed = true; // don't let any errors shine through
LoadWebPage(browser, source);
// set the size of our web browser to be the same size as the page
browser.Width = browser.Document.Body.ScrollRectangle.Width;
browser.Height = browser.Document.Body.ScrollRectangle.Height;
return browser;
}
private static void LoadWebPage(WebBrowser browser, string url)
{
browser.Navigate(url);
while (browser.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
System.Threading.Thread.Sleep(10);
}
}
private static TiffBitmapEncoder InitialiseTiffBitmapEncoder(WebBrowser browser)
{
int width = browser.Width;
int height = browser.Height;
// a bitmap that we will draw to
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
// draw the web browser to the bitmap
browser.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));
BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
TiffBitmapEncoder tiff = new TiffBitmapEncoder();
tiff.Compression = TiffCompressOption.Ccitt4;
tiff.Frames.Add(BitmapFrame.Create(bitmapSource));
return tiff;
}
//this method doesn't work becuase there is no space in the metadata
//to add the value for FillOrder
//http://social.msdn.microsoft.com/Forums/br/windowswic/thread/9d06b503-45e7-4ad2-bb68-cac4d2269d91
private static void OldFixTiff(string fileName)
{
using (var openFile = new FileStream(fileName, FileMode.Open))
{
TiffBitmapDecoder output = new TiffBitmapDecoder(openFile, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
InPlaceBitmapMetadataWriter metadata = output.Frames[0].CreateInPlaceBitmapMetadataWriter();
var fillOrder = metadata.GetQuery("/ifd/{ushort=266}");
Debug.Print("Read Fill Order Metadata tag as {0}", fillOrder);
// If .Net added a bogus fill order, correct it
if (fillOrder == null || (ushort)fillOrder == 0)
{
Debug.Print("Correcting FILL ORDER in file {0}", fileName);
metadata.SetQuery("/ifd/{ushort=266}", (ushort)1);
// Try to save new metadata
if (metadata.TrySave())
{
fillOrder = metadata.GetQuery("/ifd/{ushort=266}");
Debug.Print("Fill order correction successful!");
Debug.Print("Read New Fill Order Metadata tag as {0}", fillOrder);
}
}
}
}
private static void SaveTiff(TiffBitmapEncoder tiff, string fileName)
{
FileStream file = new FileStream(fileName, FileMode.Create);
tiff.Save(file);
file.Flush();
file.Close();
}
[STAThread]
static void Main(string[] args)
{
string source = args[0];
string target = args[1];
WebBrowser browser = InitialiseBrowser(source);
TiffBitmapEncoder tiff = InitialiseTiffBitmapEncoder(browser);
SaveTiff(tiff, target);
}
}