-
-
Notifications
You must be signed in to change notification settings - Fork 727
Closed
Labels
Description
Hi, I'm trying to create deepzoom images in my C# wpf project, using netvips,
but for some reason it doesn't work and I can't figure out the error.
Here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using NetVips;
using Microsoft.Win32;
using System.IO;
using Path = System.IO.Path;
using static NetVips.Enums;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrayNotify;
using System.ComponentModel;
namespace WpfNetVipsDeepZoomMultipleImages
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CreateDeepZoomButton_Click_1(object sender, RoutedEventArgs e)
{
try
{
//using (var openFileDialog = new System.Windows.Forms.OpenFileDialog())
Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog
{
Multiselect = true,
Title = "Select image files for Deep Zoom",
Filter = "Image Files|*.jpg;*.jpeg;*.png|All Files|*.*"
};
if (openFileDialog.ShowDialog() == true)
{
// Get the selected output folder
var saveFolderDialog = new System.Windows.Forms.FolderBrowserDialog
{
Description = "Select a folder to save Deep Zoom images",
ShowNewFolderButton = true
};
if (saveFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string outputFolder = saveFolderDialog.SelectedPath;
// Set the Deep Zoom tile size (e.g., 256 for a common size)
int tileSize = 256;
// Set the Deep Zoom overlap size (e.g., 1 for no overlap)
int overlap = 1;
// Set the Deep Zoom format (jpeg or png)
string format = "jpeg";
//ProcessImages(openFileDialog.FileNames, outputFolder, tileSize, overlap, format);
System.Windows.MessageBox.Show("Deep Zoom images created successfully!", "Success", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
catch (Exception ex)
{
System.Windows.MessageBox.Show($"Error: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void ProcessImages(string[] imagePaths, string outputFolder, int tileSize, int overlap, string format)
{
foreach (var imagePath in imagePaths)
{
try
{
// Load the image using NetVips
var image = NetVips.Image.NewFromFile(imagePath);
// Create output folder if it doesn't exist
Directory.CreateDirectory(outputFolder);
// Build the output file path
var outputFileName = Path.GetFileNameWithoutExtension(imagePath) + "_dz";
var outputFilePath = Path.Combine(outputFolder, outputFileName, tileSize, overlap, format);
// Save the image as Deep Zoom
//Dzsave(string filename, [string imagename = null], [NetVips.Enums.ForeignDzLayout ? layout = null], [string suffix = null], [int ? overlap = null], [int ? tileSize = null], [bool ? centre = null], [NetVips.Enums.ForeignDzDepth ? depth = null], [NetVips.Enums.Angle ? angle = null], [NetVips.Enums.ForeignDzContainer ? container = null], [int ? compression = null], [NetVips.Enums.RegionShrink ? regionShrink = null], [int ? skipBlanks = null], [string id = null], [int ? q = null], [NetVips.Enums.ForeignKeep ? keep = null], [double[] background = null], [int ? pageHeight = null], [string profile = null])
image.Dzsave(imagePath, outputFilePath);
// Dispose of the image
//image.Dispose();
// Deep Zoom image created successfully for the current image
Console.WriteLine($"Deep Zoom image created for: {imagePath}");
}
catch (Exception ex)
{
System.Windows.MessageBox.Show($"Error for {imagePath}: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
}