Creating an image from a TextStream

dgorka

Well-known member
Joined
Dec 27, 2006
Messages
88
Programming Experience
5-10
So I'm trying to read a file, line by line, then insert those onto a bitmap and convert that to a tiff file. Logically, to me anyway, my code should work. but when I run it, i just get a blank page.

VB.NET:
Public Function AddCode() As Boolean
        Try
            enParams.Param(0) = New EncoderParameter(Encoder.Compression, EncoderValue.CompressionCCITT4)

            tiff = Image.FromFile("C:\TestTIFF.tif")
            Dim tempBM As New Bitmap(1080, 1778, PixelFormat.Format64bppArgb)

            Using sr As New StreamReader("C:\TextFile.txt")
                Dim x As Integer
                Dim line As String
                Do
                    x = x + 10
                    Using g As Graphics = Graphics.FromImage(tempBM)
                        line = sr.ReadLine
                        'g.FillRectangle(Brushes.White, New Rectangle(0, 0, tiff.Width, tiff.Height))
                        g.DrawImage(tiff, 0, 0)
                        g.DrawString(line, displayFont, Brushes.Black, x, 10)
                        
                        ''test to make sure its reading all the text properly
                        'Console.WriteLine(line)
                    End Using
                Loop Until line Is Nothing

            End Using

            tiff.Dispose()

            'convert to a Group4 TIFF
            tiff = convertToG4tiff(tempBM)

            'save
            tiff.Save("C:\TestTiffFinal.tif", tifCodec, enParams)

            tiff.Dispose()

            Return True

        Catch ex As Exception
            MsgBox("Error Creating TIFF", MsgBoxStyle.Critical, "Error Creating Image")
            Return False
        End Try
    End Function

But if I get rid of the loop for the streamreader and just try to add the string "TEST" to my image, it works. Anyone have any ideas? Thanks in advance.

And for those wondering what the function convertToG4tiff is, its explained here

Oh, and the contents of the text file is just the code from another project that I've made.
 
Ok, I got it figured out, I had my loop wrong. I was working, but it was always writing the last line of the file over the others, and the last line was a blank line. My loop should have looked like this:

VB.NET:
Using g As Graphics = Graphics.FromImage(tempBM)
    Using sr As New StreamReader("C:\TextFile.txt")
        Dim x As Integer
        Dim line As String
        g.DrawImage(tiff, 0, 0)
        Do
            x = x + 14
            line = sr.ReadLine
            g.DrawString(line, displayFont, Brushes.Black, 10, x)
            'test to make sure its reading all the text properly
            Console.WriteLine(line)
        Loop Until line Is Nothing
    End Using
End Using

So for anyone who looked into this, thank you for your time.
 
Back
Top