Created
August 10, 2014 08:24
-
-
Save mk3008/e81750b5f2c44d8d7ace to your computer and use it in GitHub Desktop.
Magick.NET Export PDF Thumbnail Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Imports ImageMagick | |
| Imports System.Drawing | |
| ''' <summary> | |
| ''' Magick.NETラッパー | |
| ''' </summary> | |
| ''' <remarks></remarks> | |
| Public Class Magick | |
| ''' <summary> | |
| ''' サムネイル出力(先頭ページのみ) | |
| ''' </summary> | |
| ''' <param name="fromPath">データソースパス</param> | |
| ''' <param name="toPath">出力ファイルパス</param> | |
| ''' <param name="density">解像度</param> | |
| ''' <remarks></remarks> | |
| Public Shared Sub ExportThumbnail(fromPath As String, toPath As String, density As Integer) | |
| Export(fromPath, toPath, density, | |
| Sub(fromImages) | |
| '1ページのみ出力 | |
| Using toImage As MagickImage = fromImages.Item(0) | |
| toImage.Write(toPath) | |
| End Using | |
| End Sub) | |
| End Sub | |
| ''' <summary> | |
| ''' サムネイル出力(水平) | |
| ''' </summary> | |
| ''' <param name="fromPath">データソースパス</param> | |
| ''' <param name="toPath">出力ファイルパス</param> | |
| ''' <param name="density">解像度</param> | |
| ''' <remarks></remarks> | |
| Public Shared Sub ExportHThumbnail(fromPath As String, toPath As String, density As Integer) | |
| Export(fromPath, toPath, density, | |
| Sub(fromImages) | |
| '全ページ出力 | |
| Using toImage As MagickImage = fromImages.AppendHorizontally() | |
| toImage.Write(toPath) | |
| End Using | |
| End Sub) | |
| End Sub | |
| ''' <summary> | |
| ''' サムネイル出力(水平) | |
| ''' </summary> | |
| ''' <param name="fromPath">データソースパス</param> | |
| ''' <param name="toPath">出力ファイルパス</param> | |
| ''' <param name="density">解像度</param> | |
| ''' <param name="startPage">開始ページ</param> | |
| ''' <param name="endPage">終了ページ</param> | |
| ''' <remarks></remarks> | |
| Public Shared Sub ExportHThumbnail(fromPath As String, toPath As String, density As Integer, startPage As Integer, endPage As Integer) | |
| Dim pages As New List(Of Integer) | |
| For i As Integer = startPage To endPage | |
| pages.Add(i) | |
| Next | |
| ExportHThumbnail(fromPath, toPath, density, pages) | |
| End Sub | |
| ''' <summary> | |
| ''' サムネイル出力(水平) | |
| ''' </summary> | |
| ''' <param name="fromPath">データソースパス</param> | |
| ''' <param name="toPath">出力ファイルパス</param> | |
| ''' <param name="density">解像度</param> | |
| ''' <param name="pages">対象ページ</param> | |
| ''' <remarks></remarks> | |
| Public Shared Sub ExportHThumbnail(fromPath As String, toPath As String, density As Integer, pages As IEnumerable(Of Integer)) | |
| Export(fromPath, toPath, density, | |
| Sub(works) | |
| '対象ページのみ出力 | |
| Using fromImages As New MagickImageCollection() | |
| Dim cnt As Integer = 0 | |
| For Each page In pages | |
| If page <= works.Count Then fromImages.Add(works.Item(page - 1)) | |
| Next | |
| Using toImage As MagickImage = fromImages.AppendHorizontally() | |
| toImage.Write(toPath) | |
| End Using | |
| End Using | |
| End Sub) | |
| End Sub | |
| Private Shared Sub Export(fromPath As String, toPath As String, density As Integer, act As Action(Of MagickImageCollection)) | |
| '事前準備 | |
| Dim toDir As String = IO.Path.GetDirectoryName(toPath) | |
| If IO.Directory.Exists(toDir) = False Then IO.Directory.CreateDirectory(toDir) | |
| If IO.File.Exists(toPath) Then IO.File.Delete(toPath) | |
| 'メイン処理 | |
| Using imgs As New MagickImageCollection | |
| '解像度を指定して読み込み | |
| Dim s As New MagickReadSettings With {.Density = New MagickGeometry(density)} | |
| imgs.Read(fromPath, s) | |
| 'メインロジックをコール | |
| act.Invoke(imgs) | |
| End Using | |
| End Sub | |
| End Class |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Module Module1 | |
| Public ReadOnly Property BaseDirectory As String | |
| Get | |
| Return IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) | |
| End Get | |
| End Property | |
| Public ReadOnly Property OutputDirectory As String | |
| Get | |
| Return IO.Path.Combine(BaseDirectory, "Output") | |
| End Get | |
| End Property | |
| Sub Main() | |
| Dim f As String = IO.Path.Combine(BaseDirectory, "Sample.pdf") | |
| Dim toPath_top As String = IO.Path.Combine(OutputDirectory, "Sample_Top.jpg") | |
| Dim toPath_p2_3 As String = IO.Path.Combine(OutputDirectory, "Sample_P2_4.jpg") | |
| Dim toPath_full As String = IO.Path.Combine(OutputDirectory, "Sample_Full.jpg") | |
| Dim density As Integer = 16 | |
| Magick.ExportThumbnail(f, toPath_top, density) | |
| Magick.ExportHThumbnail(f, toPath_p2_3, density, startPage:=2, endPage:=3) | |
| Magick.ExportHThumbnail(f, toPath_full, density) | |
| End Sub | |
| End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment