Dealing with some legacy code that parses .TIF images (front and back images for checks) from an .IMG file. For example, I have the following file: 05090001.IMG and then I have the following values about that file:
FrontStart: 8 | FrontLength: 10600 | RearStart: 10608 | RearLength: 6372
The size of 05090001.IMG is 16980 bytes, so it seems that the front image should be 10600 bytes and does in fact create a valid .TIF file while the rear image always ends up corrupt.
This is the existing code that retrieves the front .TIF file:
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()
I'm trying to add similar code to access the rear image file:
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()
This generates an image, but Windows says it is "damaged, corrupted, or is too large".
Any ideas what I'm missing? Am I using the Seek method properly? Am I somehow reading the first 6372 bytes again instead of skipping the first 10600 and starting there? Any help greatly appreciated!
Thanks to several people in the comments above, I found that simply loading the .IMG file in as a System.Drawing.Image, I could then split it into individual page files.
Dim tiffCheck As Image = Image.FromFile(Path.Combine(DownImageFiles, dr("ImgFile")))
After that, I essentially followed the solution under the Split() function here:
https://code.msdn.microsoft.com/windowsdesktop/Split-multi-page-tiff-file-058050cc
Related
I am trying to take a database binary entry where images are stored and convert it back to display in a PictureBox on a Windows Form. I have looked around and tried to piece a couple methods together however i get no errors and also no image in the PictureBox when trying to use the below code.
Dim MyByte = varReader("BinaryData")
Dim MyImg As Image
If MyByte IsNot Nothing Then
MyImg = bytesToImage(MyByte)
PictureBox1.Image = MyImg
End If
Public Function bytesToImage(ByVal byteArrayIn As Byte()) As Image
Dim ms As MemoryStream = New MemoryStream(byteArrayIn)
Dim returnImage As Image = Image.FromStream(ms)
Return returnImage
End Function
I know the Binary data is good as i have a different piece of code that has created it in the first place to store in the DB and the image displays fine on the Website which uses this data.
Any help or advice where i have gone wrong would be much appreciated!!
Many Thanks
EDITED::
The database column holding the information is varbinary(MAX)
This is the code i use to convert the image to the binary (I won't include the DB update part as this is just a basic update to the DB Column)
Dim varPictureBinary As Byte()
Dim filePath As String = "ImageFilePath"
Dim fStream As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fStream)
Dim fileInfo As FileInfo = New FileInfo(filePath)
varPictureBinary = br.ReadBytes(fileInfo.Length)
I then want to take a binary entry from this column and turn it back to an image and display it in a picture box in my program.
I am having problems trying to read a text file which is open by another process.
After searching SO I have found a few similar questions albeit in C# and nor VB.Net which seem to refer to the fact that FileShare.ReadWrite is the key to getting this to work but yet I am still struggling with it.
This is what I have so far but nothing is appearing in TextBox1.
Dim logFileStream As FileStream = New FileStream("C:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim logFileReader As StreamReader = New StreamReader(logFileStream)
While Not logFileReader.EndOfStream
Dim line As String = logFileReader.ReadLine()
TextBox1.Text = line
End While
logFileReader.Close()
logFileStream.Close()
My goal is to just use the last 2 lines of what's in the file c:\test.txt and display those contents into a Label but I guess I first need to read and show the content before I can start to look at just extracting the last 2 lines.
Update:
After re-visiting the MS Docs, I have rearranged the code as below and I can now seem to read the open file into a TextBox
Dim strLogFilePath As String
Dim LogFileStream As FileStream
Dim LogFileReader As StreamReader
Dim strRowText As String
strLogFilePath = "C:\DSD\DSDPlus.srt"
LogFileStream = New FileStream(strLogFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
LogFileReader = New StreamReader(LogFileStream)
strRowText = LogFileReader.ReadToEnd()
TextBox1.Text = strRowText
LogFileReader.Close()
LogFileStream.Close()
I have an xps file. When I try to print this file directly, I can do it without any error with my below code:
Dim defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue
Dim xpsPrintJob As PrintSystemJobInfo = defaultPrintQueue.AddJob("test", "C:\Temp\test.xps", False)
However, If I get this file from a web service as a byte array and save it as an xps file I cannot print it.
My save byte array codes are below:
FS = New IO.FileStream("C:\Temp\test.xps", FileMode.Create)
FS.Write(arrayByte, 0, arrayByte.Length)
FS.Close()
or this code:
File.WriteAllBytes("c:\Temp\test.xps", arrayByte)
When I try to print test.xps, I'm getting the error:
An unhandled exception of type 'System.Printing.PrintJobException'
occurred in System.Printing.dll
Additional information: An exception occurred while creating print job
information. Check inner exception for details.
Have can I handle this problem? Is anyone has any idea?
By the way, there is no need a web service.
Please see my below code.
You can try this any xps file.
Firstly, I'm assinging the file as byte array
Then, I'm saving the byte array as an xps file.
First XPS file is working but second one isn't working
Dim FS As FileStream
FS = File.Open("C:\Temp\test2.xps", FileMode.Open, FileAccess.Read)
Dim bByte(FS.Length) As Byte
FS.Read(bByte, 0, FS.Length)
FS.Close()
File.WriteAllBytes("c:\Temp\test2byte.xps", bByte)
Dim defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue
'This is working
Dim xpsPrintJob1 As PrintSystemJobInfo = defaultPrintQueue.AddJob("Test", "C:\Temp\test2.xps", False)
'This is not working
Dim xpsPrintJob2 As PrintSystemJobInfo = defaultPrintQueue.AddJob("Test", "C:\Temp\test2byte.xps", False)
I've solve the problem.
I've changed the below code:
Dim FS As FileStream
FS = File.Open("C:\Temp\test2.xps", FileMode.Open, FileAccess.Read)
Dim bByte(FS.Length) As Byte
FS.Read(bByte, 0, FS.Length)
FS.Close()
as
Dim bByte() As Byte = File.ReadAllBytes("C:\Temp\test2.xps")
and the problem has been solved.
I have a resource in named StoreCode, I can't seem to read the file using:
Dim readBinaryFile As BinaryReader
readBinaryFile = New BinaryReader(My.Resources.StoreCode)
There is an error:
Value of type:'1-dimensional array of Byte' cannot be converted to 'System.IO.Stream'
How do I correctly read the Binary File?
Seems My.Resources.StoreCode is a byte[] for that reason you need to copy it to a MemoryStream object and then use a BinaryReader to read the stream's content.
Using ms As New MemoryStream(My.Resources.StoreCode)
Using readBinaryFile As New BinaryReader(ms)
'read operations
End Using
End Using
I hope it helps.
My.Resources.StoreCode is probably a an array of bytes. Instead, it needs to be a file stream, similar to this:
Dim readBinaryFile As BinaryReader
Dim fs As System.IO.Stream = File.Open(pathstring, FileMode.Open)
readBinaryFile = New BinaryReader(fs)
I am working on vb.net win form. My task is display the file names from a folder onto gridview control. when user clicks process button in my UI, all the file names present in gridview, the corresponding file has to be loaded onto memory stream buffer one after another and append the titles to the content of the file and save it in hard drive with _ed as a suffix to the file name.
I am very basic programmer. I have done the following attempt and succeeded in displaying filenames onto gridview. But no idea of later part. Any suggestions please?
'Displaying files from a folder onto a gridview
Dim inqueuePath As String = "C:\Users\Desktop\INQUEUE"
Dim fileInfo() As String
Dim rowint As Integer = 0
Dim name As String
Dim directoryInfo As New System.IO.DirectoryInfo(inqueuePath)
fileInfo = System.IO.Directory.GetFiles(inqueuePath)
With Gridview1
.Columns.Add("Column 0", "FileName")
.AutoResizeColumns()
End With
For Each name In fileInfo
Gridview1.Rows.Add()
Dim filename As String = System.IO.Path.GetFileName(name)
Gridview1.Item(0, rowint).Value = filename
rowint = rowint + 1
Next
Thank you very much for spending your valuable time to read this post.
to read a file into a memorystream is quite easy, just have a look at the following example and you should be able to convert it to suite your needs:
Dim bData As Byte()
Dim br As BinaryReader = New BinaryReader(System.IO.File.OpenRead(Path))
bData = br.ReadBytes(br.BaseStream.Length)
Dim ms As MemoryStream = New MemoryStream(bData, 0, bData.Length)
ms.Write(bData, 0, bData.Length)
then just use the MemoryStream ms as you please. Just to clearify Path holds the full path and filename you want to read into your memorystream.