将多页TIFF转换为PNG .Net

时间:2010-08-25 14:04:28

标签: .net png tiff multipage

我能够将单页TIFF转换为.Net中的PNG,但是,如何为多页TIFF执行此操作?

3 个答案:

答案 0 :(得分:6)

您应该在循环中选择活动框架(页面)并将每个tiff页面转换为png。

int pageCount = 1;
try
{
    pageCount = bmp.GetFrameCount(FrameDimension.Page);
}
catch (Exception)
{
    // sometimes GDI+ throws internal exceptions.
    // just ignore them.
}

for (int page = 0; page < pageCount; page++)
{
    bmp.SelectActiveFrame(FrameDimension.Page, page);
    // save or otherwise process tiff page
}

此代码假定您可以在System.Drawing.Bitmap对象中加载Tiff图像。

答案 1 :(得分:1)

Imagemagick可以对图像做任何事情,但由于可供选择的选项数量庞大,可能需要一段时间才能完成。您可以使用interop直接使用Imagemagick,也可以使用.NET包装器。我只使用了互操作,所以我不知道wrapper有多好或多坏。

private readonly ImageMagickObject.MagickImageClass _imageMagick = new ImageMagickObject.MagickImageClass();

var parameters = new object[] {sourcePath, destPath};
_imageMagick.Convert(ref parameters);

您必须在ImageMagick网站上找到的参数。查看命令行参数的帮助并在论坛中搜索多页tiff。我假设你想将tiff分成多个png?那么也许是这样的:

转换multipage.tif single%d.png

答案 2 :(得分:1)

完整示例,无需第3方聚会:

' MAIN CODE '

Dim ImageBitmap = Bitmap.FromStream(ImageStream)

Dim FrameCount = ImageBitmap.GetFrameCount(FrameDimension.Page)

Dim RunningHeight As Integer = 0
Dim MaxWidth As Integer = 0

For MeasurementFrameIndex As Integer = 0 To FrameCount - 1
    ImageBitmap.SelectActiveFrame(FrameDimension.Page, MeasurementFrameIndex)

    RunningHeight += ImageBitmap.Height
    MaxWidth = Math.Max(MaxWidth, ImageBitmap.Width)
Next

Dim CombinedBitmap As New Bitmap(MaxWidth, RunningHeight)
Dim RunningVerticalPosition As Integer = 0

For CombinationFrameIndex As Integer = 0 To FrameCount - 1
    ImageBitmap.SelectActiveFrame(FrameDimension.Page, CombinationFrameIndex)

    EmbedBitmap(ImageBitmap, CombinedBitmap, RunningVerticalPosition)

    RunningVerticalPosition += ImageBitmap.Height + 1
Next



    ' SUPPORT ROUTINES '

Private Shared Sub EmbedBitmap(
        SourceBitmap As Bitmap,
        ByRef DestinationBitmap As Bitmap,
        VerticalPosition As Integer)

    Dim SourceRectangle As New Rectangle(
        New Point(0, 0),
        New Size(SourceBitmap.Width, SourceBitmap.Height))

    Dim DestinationRectangle As New Rectangle(
        New Point(0, VerticalPosition),
        New Size(SourceBitmap.Width, SourceBitmap.Height))

    Using Canvas As Graphics = Graphics.FromImage(DestinationBitmap)
        Canvas.DrawImage(
            SourceBitmap,
            DestinationRectangle,
            SourceRectangle,
            GraphicsUnit.Pixel)
    End Using
End Sub