Skip to content

Commit

Permalink
用了新pdf轮子,
Browse files Browse the repository at this point in the history
pdf压缩率是这样的,如果你原图就是高压的jpg或者png可以直接用0,需要有损压缩请输入0-99,无损压缩只要你写大于等于100,默认就是高压24bit png。压缩采用硬盘缓存,节约内存但是速度慢。
  • Loading branch information
NaughtDZ committed Oct 24, 2023
1 parent b17ee4c commit cca5cf6
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 161 deletions.
14 changes: 11 additions & 3 deletions App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8.1" />
</startup>
</configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Magick.NET.Core" publicKeyToken="2004825badfa91ec" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-13.4.0.0" newVersion="13.4.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
77 changes: 48 additions & 29 deletions Form1.Designer.vb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Form1.resx
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@
<value>167, 17</value>
</metadata>
<metadata name="FolderBrowserDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>312, 17</value>
<value>518, 16</value>
</metadata>
<metadata name="ToolTip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>494, 17</value>
<value>846, 16</value>
</metadata>
</root>
91 changes: 46 additions & 45 deletions Form1.vb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
Imports System.IO '虽然写在这里,但是pdfsharp和sytem里都有Drawing等字类同名,请下面详写
Imports PdfSharp
Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports System.Linq.Expressions
Imports Ghostscript
Imports ImageMagick

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Expand Down Expand Up @@ -89,7 +88,7 @@ Public Class Form1
Try
Merg2pdf(w8t2con, TextBox2.Text)
Catch ex As Exception
MsgBox(ex)
MsgBox(ex.ToString)
End Try
End Sub

Expand All @@ -102,53 +101,56 @@ Public Class Form1

Private Sub Merg2pdf(ByVal w8t2con As Collection, ByVal fileloca As String)
TrackBar1.Maximum = w8t2con.Count
Dim document As PdfDocument = New PdfDocument '创建pdf文件
If TextBox3.Text = 0 Then '免压缩,通常情况
Using images As MagickImageCollection = New MagickImageCollection
For Each picfile In w8t2con
images.Add(picfile)
TrackBar1.Value += 1
Application.DoEvents()
Next
images.Write(fileloca)
End Using
Else
TrackBar1.Maximum = TrackBar1.Maximum * 2 + 1
Directory.CreateDirectory(TextBox1.Text & "\temp\")
'先转格式
Using tmpimages As MagickImageCollection = New MagickImageCollection()
For Each img In w8t2con
tmpimages.Add(img)

' 创建一个临时文件夹来保存文件流
Dim tempFolderPath As String = Path.Combine(Path.GetTempPath(), "PdfSharpTemp")
If Not Directory.Exists(tempFolderPath) Then
Directory.CreateDirectory(tempFolderPath)
End If
Next
For i = 0 To tmpimages.Count - 1
If TextBox3.Text >= 100 Then
tmpimages(i).Format = MagickFormat.Png24
tmpimages(i).Quality = 1
tmpimages(i).Write(TextBox1.Text & "\temp\" & i & ".png")
Else
tmpimages(i).Format = MagickFormat.Jpg
tmpimages(i).Quality = TextBox3.Text
tmpimages(i).Write(TextBox1.Text & "\temp\" & i & ".jpg")
End If

For Each img In w8t2con
Dim tempImgFile As String = Path.Combine(tempFolderPath, Path.GetFileName(img))
Using fs As New FileStream(tempImgFile, FileMode.Create, FileAccess.ReadWrite)
' 写入图片数据到文件流中
Dim imgBytes As Byte() = File.ReadAllBytes(img)
fs.Write(imgBytes, 0, imgBytes.Length)
fs.Flush()
' 将文件流的位置重置到开始
fs.Position = 0
TrackBar1.Value += 1
Application.DoEvents()
Next
End Using

Dim page As PdfPage = document.AddPage() '创建新页
Dim gfx As XGraphics = XGraphics.FromPdfPage(page) '创建画布在page上
Using ximg As XImage = XImage.FromStream(fs) '创建gfx可用的image
'Dim g As Graphics
'g = Graphics.FromImage(Image.FromFile(img))
'Debug.WriteLine(ximg.HorizontalResolution) 'pdf打印页面大小与DPI有关
page.Width = ximg.PixelWidth '设置页面为图片分辨率,piexel是分辨率,width是通过dpi转换后的大小
page.Height = ximg.PixelHeight
'Debug.WriteLine("{0} x:{1} y:{2}", img, page.Width, page.Height)
'Debug.WriteLine("x:{0} y:{1}", g.DpiX / TextBox3.Text, g.DpiY / TextBox3.Text)
gfx.ScaleTransform(ximg.HorizontalResolution / TextBox3.Text)
gfx.DrawImage(ximg, 0, 0)
page.Close()
End Using
'再写入
Using images As MagickImageCollection = New MagickImageCollection
Dim files As New DirectoryInfo(TextBox1.Text & "\temp\")
Dim fileslist = files.GetFiles()
For Each picfile In fileslist
images.Add(picfile.FullName)
TrackBar1.Value += 1
Application.DoEvents()
Next
images.Write(fileloca)
End Using
TrackBar1.Value += 1
Next
document.Save(fileloca)
Directory.Delete(TextBox1.Text & "\temp\", True)
End If
Beep()
document.Close()
' 删除临时文件夹
Directory.Delete(tempFolderPath, True)
TrackBar1.Value = 0
Conver.Enabled = True
Try
GC.Collect()
Catch ex As Exception
MsgBox(ex)
End Try
End Sub


Expand All @@ -173,5 +175,4 @@ Public Class Form1
TextBox2.Text = temp
End If
End Sub

End Class
24 changes: 19 additions & 5 deletions Image2Pdf.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
<AssemblyName>Image2Pdf</AssemblyName>
<FileAlignment>512</FileAlignment>
<MyType>WindowsForms</MyType>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -69,11 +72,14 @@
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="PdfSharp, Version=1.50.5147.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
<HintPath>packages\PDFsharp.1.50.5147\lib\net20\PdfSharp.dll</HintPath>
<Reference Include="Ghostscript.NET, Version=1.2.3.0, Culture=neutral, PublicKeyToken=f85051de34525b59, processorArchitecture=MSIL">
<HintPath>packages\Ghostscript.NET.1.2.3.1\lib\net40\Ghostscript.NET.dll</HintPath>
</Reference>
<Reference Include="PdfSharp.Charting, Version=1.50.5147.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
<HintPath>packages\PDFsharp.1.50.5147\lib\net20\PdfSharp.Charting.dll</HintPath>
<Reference Include="Magick.NET-Q8-AnyCPU, Version=13.4.0.0, Culture=neutral, PublicKeyToken=2004825badfa91ec, processorArchitecture=MSIL">
<HintPath>packages\Magick.NET-Q8-AnyCPU.13.4.0\lib\netstandard20\Magick.NET-Q8-AnyCPU.dll</HintPath>
</Reference>
<Reference Include="Magick.NET.Core, Version=13.4.0.0, Culture=neutral, PublicKeyToken=2004825badfa91ec, processorArchitecture=MSIL">
<HintPath>packages\Magick.NET.Core.13.4.0\lib\netstandard20\Magick.NET.Core.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
Expand Down Expand Up @@ -111,6 +117,7 @@
<Compile Include="My Project\Application.Designer.vb">
<AutoGen>True</AutoGen>
<DependentUpon>Application.myapp</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<Compile Include="My Project\Resources.Designer.vb">
<AutoGen>True</AutoGen>
Expand Down Expand Up @@ -148,4 +155,11 @@
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
<Import Project="packages\Magick.NET-Q8-AnyCPU.13.4.0\build\netstandard20\Magick.NET-Q8-AnyCPU.targets" Condition="Exists('packages\Magick.NET-Q8-AnyCPU.13.4.0\build\netstandard20\Magick.NET-Q8-AnyCPU.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('packages\Magick.NET-Q8-AnyCPU.13.4.0\build\netstandard20\Magick.NET-Q8-AnyCPU.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Magick.NET-Q8-AnyCPU.13.4.0\build\netstandard20\Magick.NET-Q8-AnyCPU.targets'))" />
</Target>
</Project>
Loading

0 comments on commit cca5cf6

Please sign in to comment.