2010-08-25 16 views

Antwort

6

Sie sollten aktiv Rahmen auswählen (Seite) in einer Schleife und umwandeln in eine png jede TIFF-Seite.

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 
} 

In diesem Code wird davon ausgegangen, dass Sie das Tiff-Bild im System.Drawing.Bitmap-Objekt laden können.

1

Imagemagick kann fast alles mit Bildern tun, aber es könnte eine Weile dauern, um es richtig zu machen aufgrund der überwältigenden Anzahl von Optionen zur Auswahl. Sie können Interop verwenden, um direkt mit Imagemagick zu arbeiten, oder Sie können einen .NET-Wrapper verwenden. Ich habe nur Interop verwendet, also weiß ich nicht, wie gut oder schlecht die wrapper ist.

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

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

Die Parameter, die Sie auf der ImageMagick-Website selbst herausfinden müssen. Schau dir die Hilfe für den Kommandozeilenparameter an und suche das Forum nach multipage tiff. Ich nehme an, Sie möchten die Tiff in mehrere PNGs teilen? Dann vielleicht so etwas wie folgt aus:

convert MULTIPAGE.TIF Einzel% d.png

1

Komplettes Beispiel ohne die Notwendigkeit für 3'rd Partei Baugruppen:

' 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 
6

dank @ Tom Halladay

ich eine C# Version des Codes liefern werde

private static Bitmap ConvertTiffToBitmapStream(byte[] tiffImage){ 
    System.Drawing.Image ImageBitmap = Bitmap.FromStream(new MemoryStream(tiffImage)); 
    int FrameCount = ImageBitmap.GetFrameCount(FrameDimension.Page); 
    int RunningHeight = 0; 
    int MaxWidth = 0; 

    for (int MeasurementFrameIndex = 0; MeasurementFrameIndex <= FrameCount - 1; MeasurementFrameIndex++){ 
     ImageBitmap.SelectActiveFrame(FrameDimension.Page, MeasurementFrameIndex); 
     RunningHeight += ImageBitmap.Height; 
     MaxWidth = Math.Max(MaxWidth, ImageBitmap.Width); 
    } 

    Bitmap CombinedBitmap = new Bitmap(MaxWidth, RunningHeight); 
    int RunningVerticalPosition = 0; 

    for (int CombinationFrameIndex = 0; CombinationFrameIndex <= FrameCount - 1; CombinationFrameIndex++){ 
     ImageBitmap.SelectActiveFrame(FrameDimension.Page, CombinationFrameIndex); 
     EmbedBitmap(new Bitmap(ImageBitmap), ref CombinedBitmap, RunningVerticalPosition); 
     RunningVerticalPosition += ImageBitmap.Height + 1; 
    } 
    return CombinedBitmap; 
} 

private static void EmbedBitmap(Bitmap SourceBitmap, ref Bitmap DestinationBitmap, int VerticalPosition){ 
    Rectangle SourceRectangle = new Rectangle(new Point(0, 0), new Size(SourceBitmap.Width, SourceBitmap.Height)); 
    Rectangle DestinationRectangle = new Rectangle(new Point(0, VerticalPosition), new Size(SourceBitmap.Width, SourceBitmap.Height)); 

    using (Graphics Canvas = Graphics.FromImage(DestinationBitmap)){ 
     Canvas.DrawImage(SourceBitmap, DestinationRectangle, SourceRectangle, GraphicsUnit.Pixel); 
    } 
} 
Verwandte Themen