从一个 .IMG 文件中读取两个 .TIF 文件

Read two .TIF files from an .IMG file

处理一些从 .IMG 文件中解析 .TIF 图像(用于支票的正面和背面图像)的遗留代码。例如,我有以下文件:05090001.IMG 然后我有关于该文件的以下值:

前线起点:8 |前长:10600 |后启动:10608 |后长:6372

05090001.IMG 的大小是 16980 字节,所以看起来前面的图像应该是 10600 字节并且实际上创建了一个有效的 .TIF 文件,而后面的图像总是损坏。

这是检索前面.TIF文件的现有代码:

Dim fs As New FileStream(Me.FileName, FileMode.Open, FileAccess.Read)
Dim sr As New BinaryReader(fs)
Dim fname As String = {long formula to generate fname}

Dim fsFront As New FileStream(fname & "_Front.tif", FileMode.Create)
Dim swFront As New BinaryWriter(fsFront)
Dim imgBytesFront As Byte()

fs = New FileStream(Path.Combine(DownImageFiles, dr("ImgFile")), FileMode.Open, FileAccess.Read)
sr = New BinaryReader(fs)
imgBytesFront = sr.ReadBytes(dr("FrontLength"))

swFront.Write(imgBytesFront)
swFront.Close()
fsFront.Close()

我正在尝试添加类似的代码来访问后方图像文件:

Dim fsRear As New FileStream(fname & "_Rear.tif", FileMode.Create)
Dim swRear As New BinaryWriter(fsRear)
Dim imgBytesRear As Byte()

fs = New FileStream(Path.Combine(DownImageFiles, dr("ImgFile")), FileMode.Open, FileAccess.Read)
Using br As New BinaryReader(fs)
    br.BaseStream.Seek(Long.Parse(dr("FrontLength"), Globalization.NumberStyles.Integer), SeekOrigin.Begin)
    imgBytesRear = br.ReadBytes(dr("RearLength"))
End Using

imgBytesRear = sr.ReadBytes(dr("RearLength"))
swRear.Write(imgBytesRear)
swRear.Close()
fsRear.Close()

这会生成一个图像,但是 Windows 说它是 "damaged, corrupted, or is too large"。

知道我遗漏了什么吗?我是否正确使用了 Seek 方法?我是否以某种方式再次读取前 6372 个字节,而不是跳过前 10600 个字节并从那里开始?非常感谢任何帮助!

感谢上面评论中的几个人,我发现只需将 .IMG 文件加载为 System.Drawing.Image,然后我就可以将其拆分为单独的页面文件。

Dim tiffCheck As Image = Image.FromFile(Path.Combine(DownImageFiles, dr("ImgFile")))

在那之后,我基本上按照这里的 Split() 函数下的解决方案: https://code.msdn.microsoft.com/windowsdesktop/Split-multi-page-tiff-file-058050cc