none
How to create a bitmap image using a text file

    Question

  • Hi Folks,

    I am trying to convert from a text file to an image(bitmap) file.
    For example, test.txt: 01 23 34 56 78 90
                                    00 25 35 45 55 65
                                    55 65 75 25 35 77

    The first step will be reading the data, so I created some VB codes:

    Dim openFileDialog1 As New OpenFileDialog()
            openFileDialog1.Filter = "Text Files (*.txt)|*.txt"
            openFileDialog1.Title = "Select a text File"

            ' Show the Dialog.
            ' If the user clicked OK in the dialog and
            ' an image  file was selected, open it.
            If openFileDialog1.ShowDialog() = DialogResult.OK Then
               ' Open file.
                FileOpen(1, openFileDialog1.FileName, OpenMode.Input)

    The second step will be converting the data to some data format which can be used in a bitmap image. This part  is making me confused. Some codes I made is listed below:

     Dim newimage As bitmap
            Dim x, y, width As Integer
            x = 0
            width = TextBox1.Text
    If openFileDialog1.ShowDialog() = DialogResult.OK Then
                ' Open file.
                FileOpen(1, openFileDialog1.FileName, OpenMode.Input)
                ' Loop until end of file.
                Dim Text, DelimitedText() As String, Delimiter As Char = ControlChars.Tab
                Dim Delimiters() As Char = {ControlChars.Tab, " "}
                While Not EOF(1)
                    For i = 1 To DelimitedText.GetUpperBound(2)
                        ' Get one character.
                        DelimitedText = Text.Split(ControlChars.Tab)
                        x = i Mod width
                        y = i / width
                        Dim pixelcolor As Color = Color.FromArgb(Int(DelimitedText), Int(DelimitedText), Int(DelimitedText))

                        newimage.setPixel(x, y, pixelcolor)
                    Next

                End While
            End If
            Me.PictureBox1.Image = newimage

    The second part made an error message. Please give me your advanced knowledge.
    Thanks in advance.

    Soo

    Saturday, January 09, 2010 8:40 PM

Answers

  • I think that the numbers in the file are shades of grey and you want them painted as pixels in the image successively at (Width) pixels per row.  The following code works, but I can't be sure the parsing of the file is correct because your code suggests that the file may contain tabs as well as (or instead of) spaces.   I have assumed spaces.

            Dim x, y, width As Integer
            x = 0
            width = TextBox1.Text
            Dim newimage As Bitmap = New Bitmap(width, 200, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            Dim openFileDialog1 As New OpenFileDialog()
            If openFileDialog1.ShowDialog() = DialogResult.OK Then
                'Dim Text As String
                Dim Text As String = My.Computer.FileSystem.ReadAllText(openFileDialog1.FileName)
                Dim lines() As String = Text.Split(vbCrLf)
                Dim Counter As Integer = 0
                For Each L As String In lines
                    Dim Atoms() As String = L.Split(" ")
                    For Each S As String In Atoms
                        ' Get one character.
                        Dim i As Integer = CInt(Val(S))
                        x = Counter Mod width
                        y = Counter / width
                        Counter += 1
                        Dim pixelcolor As Color = Color.FromArgb(i, i, i)
                        newimage.SetPixel(x, y, pixelcolor)
                    Next
                Next
            End If
            Me.PictureBox1.Image = newimage

    Saturday, January 09, 2010 10:08 PM
  • You would probably fix the first problem with

        y = counter \ width

    The second error suggests a problem with the file.   If the file follows the format you have indicated, that value for S cannot occur.  L will always be a string of 6 pairs of digits separated by spaces, and S will always be a pair of digits.  If the file does not follow that format the process will fail. If there is a portion of the file that does not contain graphic data then you need to identify it so it can be excluded from the Lines array. I'm not sure what you  mean by  'first three numbers'.  Do you mean the first 8 characters of each line?

        Dim Atoms() As String = L.Substring(0,8).Split(" ")

    That will ignore everything after the 8th character.  But if there is a line like the one you have quoted, that won't help.



    Sunday, January 10, 2010 3:14 AM

All replies

  • Have a look here please:


    Just Be Humble Malange!
    Saturday, January 09, 2010 9:08 PM
  • To start with add a breakpoint to your app and step through it and tell us what line is causing the error . What version of VB are you using and any other information you can provide .
    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
    Saturday, January 09, 2010 9:13 PM
  • You haven't indicated what the error is or where it is occurring, but the problem with your code is that you aren't reading the text file into the variables.

    I think that Text should have the value of the text from the file.  You  need something like

    'Dim Text As String
    Dim Text As String = My.Computer.FileSystem.ReadAllText(openFileDialog1.FileName)
    

    You are using the count of delimited items before you have created the list.   If DelimitedText is supposed to be a list of sections of Text split at the tab then you need:

    'Dim DelimitedText() As String
    Dim DelimitedText As String() = Text.Split(vbTab)

    If you do it like this you do not need to work through the file in the loop.  Instead the loop will work through either the characters in Text or the sections of text in DelimitedText.  But unfortunately I cannot work out what you are trying to do in that section of code so I can't provide an example.





     

    Saturday, January 09, 2010 9:26 PM
  • I think that the numbers in the file are shades of grey and you want them painted as pixels in the image successively at (Width) pixels per row.  The following code works, but I can't be sure the parsing of the file is correct because your code suggests that the file may contain tabs as well as (or instead of) spaces.   I have assumed spaces.

            Dim x, y, width As Integer
            x = 0
            width = TextBox1.Text
            Dim newimage As Bitmap = New Bitmap(width, 200, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            Dim openFileDialog1 As New OpenFileDialog()
            If openFileDialog1.ShowDialog() = DialogResult.OK Then
                'Dim Text As String
                Dim Text As String = My.Computer.FileSystem.ReadAllText(openFileDialog1.FileName)
                Dim lines() As String = Text.Split(vbCrLf)
                Dim Counter As Integer = 0
                For Each L As String In lines
                    Dim Atoms() As String = L.Split(" ")
                    For Each S As String In Atoms
                        ' Get one character.
                        Dim i As Integer = CInt(Val(S))
                        x = Counter Mod width
                        y = Counter / width
                        Counter += 1
                        Dim pixelcolor As Color = Color.FromArgb(i, i, i)
                        newimage.SetPixel(x, y, pixelcolor)
                    Next
                Next
            End If
            Me.PictureBox1.Image = newimage

    Saturday, January 09, 2010 10:08 PM
  • Of course with the r,g,b values all equal you will get a grayscale bitmap .
    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
    Saturday, January 09, 2010 10:32 PM
  • Of course with the r,g,b values all equal you will get a grayscale bitmap .
    That's true, but in the original code OP had:

        DelimitedText = Text.Split(ControlChars.Tab)
        Dim pixelcolor As Color = Color.FromArgb(Int(DelimitedText), Int(DelimitedText), Int(DelimitedText))

    which could easily have been meant as

        DelimitedText = Text.Split(ControlChars.Tab)
        Dim pixelcolor As Color = Color.FromArgb(Int(DelimitedText(0)), Int(DelimitedText(1)), Int(DelimitedText(2)))

    which would suggest that the file values were color triplets, at two pixels per line.  The file vaues don't support this, but then they don't produce much of a monochrome pattern either.
    Sunday, January 10, 2010 1:49 AM
  • Thank you so so so much, especially Acamar.
    It works. I changed only one part  y = CInt(Int(Counter / width)) for my study.
    I also found one more problem. " Dim i As Integer = CInt(Val(s))" made "Arithmetic operation resulted in an overflow." At that time, the s value was "0000000000000000000000000000000464646." I think this problem can be solved if the program can import only fist three numbers as string. Please share your knowledge with me. Thanks in advance.

    Soo 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim x, y, width As Integer
            x = 0
            width = TextBox1.Text
            Dim newimage As Bitmap = New Bitmap(width, 200, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
            Dim openFileDialog1 As New OpenFileDialog()
            If openFileDialog1.ShowDialog() = DialogResult.OK Then
                'Dim Text As String
                Dim Text As String = My.Computer.FileSystem.ReadAllText(openFileDialog1.FileName)
                Dim lines() As String = Text.Split(vbCrLf)
                Dim Counter As Integer = 0
                For Each L As String In lines
                    Dim Atoms() As String = L.Split(" ")
                    For Each S As String In Atoms
                        ' Get one character.
                       Dim i As Integer = CInt(Val(s))
                        x = Counter Mod width
                        y = CInt(Int(Counter / width))
                        Counter += 1
                        Dim pixelcolor As Color = Color.FromArgb(i, i, i)
                        newimage.SetPixel(x, y, pixelcolor)
                    Next
                Next
            End If
            Me.PictureBox1.Image = newimage
    
        End Sub
    Sunday, January 10, 2010 2:56 AM
  • You would probably fix the first problem with

        y = counter \ width

    The second error suggests a problem with the file.   If the file follows the format you have indicated, that value for S cannot occur.  L will always be a string of 6 pairs of digits separated by spaces, and S will always be a pair of digits.  If the file does not follow that format the process will fail. If there is a portion of the file that does not contain graphic data then you need to identify it so it can be excluded from the Lines array. I'm not sure what you  mean by  'first three numbers'.  Do you mean the first 8 characters of each line?

        Dim Atoms() As String = L.Substring(0,8).Split(" ")

    That will ignore everything after the 8th character.  But if there is a line like the one you have quoted, that won't help.



    Sunday, January 10, 2010 3:14 AM