Imports Leadtools
Imports Leadtools.Codecs
Imports Leadtools.ImageProcessing
Imports Leadtools.ImageProcessing.Color
Imports Leadtools.Drawing
Private Shared Sub SaveMultiPageFile(rasterCodecs As RasterCodecs, multiPageImage As RasterImage, outputFile As String, format As RasterImageFormat)
' Check if the image has multi-ple pages and the format supports multi-page
If multiPageImage.PageCount > 1 AndAlso rasterCodecs.FormatSupportsMultipageSave(format) Then
' Yes, just save the file
rasterCodecs.Save(multiPageImage, outputFile, format, 0, 1, -1, 1, CodecsSavePageMode.Overwrite)
Else
' No, we need to save each page in a separate file
Dim originalPageNumber As Integer = multiPageImage.Page
For pageNumber As Integer = 1 To multiPageImage.PageCount
' Get the new file name
Dim name As String = Path.GetFileNameWithoutExtension(outputFile) + "_page" + pageNumber.ToString()
name = Path.ChangeExtension(name, Path.GetExtension(outputFile))
Dim pageFile As String = Path.Combine(Path.GetDirectoryName(outputFile), name)
' Save this page
multiPageImage.Page = pageNumber
rasterCodecs.Save(multiPageImage, pageFile, format, 0)
Next
multiPageImage.Page = originalPageNumber
End If
End Sub
Public Shared Sub FormatSupportsMultipageSaveExample()
Dim inDir As String = LeadtoolsExamples.Common.ImagesPath.Path
Dim outDir As String = Path.Combine(inDir, "FormatSupportsMultipageSave")
If Not Directory.Exists(outDir) Then
Directory.CreateDirectory(outDir)
End If
Using rasterCodecs As New RasterCodecs()
' Create a multi-page image
Using multiPageImage As RasterImage = GetMultipageImage(rasterCodecs, inDir)
' Save the image as TIF, this should create a single file
SaveMultiPageFile(rasterCodecs, multiPageImage, Path.Combine(outDir, "out.tif"), RasterImageFormat.Tif)
' Save the image as PNG, this should create multiple files (one for each page)
SaveMultiPageFile(rasterCodecs, multiPageImage, Path.Combine(outDir, "out.png"), RasterImageFormat.Png)
End Using
End Using
End Sub
Private Shared Function GetMultipageImage(rasterCodecs As RasterCodecs, inDir As String) As RasterImage
' Create a multi-page image from some known LEADTOOLS images
Dim multiPageImage As RasterImage = Nothing
For imageNumber As Integer = 1 To 4
Dim fileName As String = Path.Combine(inDir, "Ocr" + imageNumber.ToString() + ".tif")
Dim pageImage As RasterImage = rasterCodecs.Load(fileName, 1)
If IsNothing(multiPageImage) Then
multiPageImage = pageImage
Else
multiPageImage.AddPage(pageImage)
pageImage.Dispose()
End If
Next
Return multiPageImage
End Function
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;
using Leadtools.ImageProcessing.Color;
private static void SaveMultiPageFile(RasterCodecs rasterCodecs, RasterImage multiPageImage, string outputFile, RasterImageFormat format)
{
// Check if the image has multi-ple pages and the format supports multi-page
if (multiPageImage.PageCount > 1 && RasterCodecs.FormatSupportsMultipageSave(format))
{
// Yes, just save the file
rasterCodecs.Save(multiPageImage, outputFile, format, 0, 1, -1, 1, CodecsSavePageMode.Overwrite);
}
else
{
// No, we need to save each page in a separate file
int originalPageNumber = multiPageImage.Page;
for (int pageNumber = 1; pageNumber <= multiPageImage.PageCount; pageNumber++)
{
// Get the new file name
string name = Path.GetFileNameWithoutExtension(outputFile) + "_page" + pageNumber.ToString();
name = Path.ChangeExtension(name, Path.GetExtension(outputFile));
string pageFile = Path.Combine(Path.GetDirectoryName(outputFile), name);
// Save this page
multiPageImage.Page = pageNumber;
rasterCodecs.Save(multiPageImage, pageFile, format, 0);
}
multiPageImage.Page = originalPageNumber;
}
}
public static void FormatSupportsMultipageSaveExample()
{
string inDir = LeadtoolsExamples.Common.ImagesPath.Path;
string outDir = Path.Combine(inDir, @"FormatSupportsMultipageSave");
if (!Directory.Exists(outDir))
Directory.CreateDirectory(outDir);
using (RasterCodecs rasterCodecs = new RasterCodecs())
{
// Create a multi-page image
using (RasterImage multiPageImage = GetMultipageImage(rasterCodecs, inDir))
{
// Save the image as TIF, this should create a single file
SaveMultiPageFile(rasterCodecs, multiPageImage, Path.Combine(outDir, "out.tif"), RasterImageFormat.Tif);
// Save the image as PNG, this should create multiple files (one for each page)
SaveMultiPageFile(rasterCodecs, multiPageImage, Path.Combine(outDir, "out.png"), RasterImageFormat.Png);
}
}
}
private static RasterImage GetMultipageImage(RasterCodecs rasterCodecs, string inDir)
{
// Create a multi-page image from some known LEADTOOLS images
RasterImage multiPageImage = null;
for (int imageNumber = 1; imageNumber <= 4; imageNumber++)
{
string fileName = Path.Combine(inDir, "Ocr" + imageNumber.ToString() + ".tif");
RasterImage pageImage = rasterCodecs.Load(fileName, 1);
if (multiPageImage == null)
{
multiPageImage = pageImage;
}
else
{
multiPageImage.AddPage(pageImage);
pageImage.Dispose();
}
}
return multiPageImage;
}