Showing posts with label Deep Zoom. Show all posts
Showing posts with label Deep Zoom. Show all posts

Friday, June 5, 2009

Deep Zoom Batch Processing using DeepZoomTools.dll

The following code will open a folder browser dialog and allow you to recursively batch process multiple images using the DeepZoomTools.dll that is included when you download Deep Zoom Composer. I also added additional methods for creating a CSV of your directory structure so you can filter out your dzi files and export into a data source.

Note: This is part of the April 2009 version of Deep Zoom Composer.

Steps:

1. Create a Window Form Application in Visual Studio.

2. Make a reference to the DeepZoomTools.dll found in C:\Program Files\Microsoft Expression\Deep Zoom Composer.

3. Add the following code and call the GenerateDeepZoomImages method.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Microsoft.DeepZoomTools;
public void GenerateDeepZoomImages()
{
FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
DialogResult result = folderBrowser.ShowDialog();
if (result == DialogResult.OK)
{
List<string> extensionFilter = new List<string> { ".tiff", ".tif", ".jpg", ".bmp", ".png" }; //DeepZoom (April 2009) only supports these extensions
List<FileSystemInfo> fileList = GetFileSystemInfo(folderBrowser.SelectedPath, false, true, true, extensionFilter);
CollectionCreator collectionCreator = new CollectionCreator();
ImageCreator imageCreator = new ImageCreator();
List<Microsoft.DeepZoomTools.Image> images = new List<Microsoft.DeepZoomTools.Image>();
foreach (FileInfo fsInfo in fileList)
{
string imageFileName = fsInfo.FullName;
string imageFolderName = Path.GetDirectoryName(fsInfo.FullName);
string destinationFolder = imageFolderName + "\\" + fsInfo.Name + "_dzdata";
imageCreator.Create(fsInfo.FullName, destinationFolder);
Microsoft.DeepZoomTools.Image img = new Microsoft.DeepZoomTools.Image(imageFileName);
//You can manipulate this Image object before adding
images.Add(img);
collectionCreator.Create(images, destinationFolder);
}
}
}

public List<FileSystemInfo> GetFileSystemInfo(string directoryPath, bool includeFolders, bool includeFiles, bool recursive, List<string> extensionFilter)
{
extensionFilter = (from e in extensionFilter select e.ToLower()).ToList<string>();
List<FileSystemInfo> fileSystemList = new List<FileSystemInfo>();
DirectoryInfo directory = new DirectoryInfo(directoryPath);
if (recursive)
{
foreach (DirectoryInfo childDirectory in directory.GetDirectories())
{
fileSystemList.AddRange(GetFileSystemInfo(childDirectory.FullName, includeFolders, includeFiles, recursive, extensionFilter));
}
}
if (includeFolders)
{
fileSystemList.Add(directory);
}
FileInfo[] files = directory.GetFiles("*.*");
foreach (FileInfo file in files)
{
if (extensionFilter.Count != 0)
{
if (extensionFilter.Contains(file.Extension.ToLower()))
{
fileSystemList.Add(file);
}
}
else
{
fileSystemList.Add(file);
}
}
return fileSystemList;
}
public static string GetFileSystemCsv(List<FileSystemInfo> fileSystemList)
{
StringBuilder sb = new StringBuilder();
sb.Append("Name,Full Name,Parent Folder,Creation Time,Last Access Time,Last Write Time,Length, Extension,Attributes\r\n");
foreach (FileSystemInfo fileSystemItem in fileSystemList)
{
if (fileSystemItem.GetType() == typeof(FileInfo))
{
sb.Append(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8}\r\n", fileSystemItem.Name, fileSystemItem.FullName, fileSystemItem.FullName.Replace(fileSystemItem.Name, ""), fileSystemItem.CreationTime, fileSystemItem.LastAccessTime, fileSystemItem.LastWriteTime, ((FileInfo)fileSystemItem).Length.ToString(), fileSystemItem.Extension, fileSystemItem.Attributes.ToString()));
}
else
{
sb.Append(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8}\r\n", fileSystemItem.Name, fileSystemItem.FullName, fileSystemItem.FullName.Replace(fileSystemItem.Name, ""), fileSystemItem.CreationTime, fileSystemItem.LastAccessTime, fileSystemItem.LastWriteTime, "", "", fileSystemItem.Attributes.ToString()));
}
}
return sb.ToString();
}

private void SaveCsvFile(string fileText)
{
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "CSV Files (*.csv)*.csv";
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
StreamWriter wText = new StreamWriter(myStream);
wText.Write(fileText);
wText.Close();
}
}
}