locked
How to save a file as string RRS feed

  • Question

  • Hello friends.

    I want to know how to convert any file into string and store it in a text file or a memo field of a database.
    If i can do this, i can convert a image file and store it in a database memo field as text and then retrive the text and create file from it again.

    I have tried using some sort of filestreams and encoders to using getbytes(stringvariable) and getstring(baytearray) but this is not working well with all files. I have converted an HTML page to string and created a text file. Then I converted it back to html page. but it is not working with all file types.

    If anyone has the solution please mail me to abhi_thatsme@hotmail.com or abhi_mailmeat@yahoo.com


    Thanks a lot in advance.

    A vb.net developer
    Friday, May 30, 2008 8:19 AM

Answers

  • Read the Image file or Any file to a Byte()
    You can Simply use 
        
        Public Function FileToString(ByVal pFileName As StringAs String 
            Dim bytData As Byte() = System.IO.File.ReadAllBytes(pFileName)  
            Dim strText As String = System.Convert.ToBase64String(bytData)  
            Return strText  
        End Function
     

     


    Muthu Krishnan.R
    Friday, May 30, 2008 9:27 PM
  • Is your question really about how to store images in SQL?

    If it is then you create a table with an image field and read the images in and out as required

    To store the image - this stores the image into sql field based on a selected source document

    ' Read Image Bytes into a byte array
    Dim imageData As Byte() = ReadFile(txtImagePath.Text)

    Dim qry As String = "insert into FAImagesStore (FAID,ImageDesc,OriginalPath,ImageData,ImageType) values(@FAID, @ImageDesc, @OriginalPath, @ImageData, @ImageType)"

    Dim SqlCom As SqlCommand = New SqlCommand(qry, CN)

    ' sql image insert

    ' We are passing Original Image Path and Image byte data as sql parameters.
    SqlCom.Parameters.Add(New SqlParameter("@OriginalPath", CType(txtImagePath.Text, Object)))
    SqlCom.Parameters.Add(
    New SqlParameter("@ImageData", CType(imageData, Object)))
    SqlCom.Parameters.Add(
    New SqlParameter("@ImageDesc", CType(TextBoxDescription.Text, Object)))
    SqlCom.Parameters.Add(
    New SqlParameter("@FAID", CType(lID, Object)))
    SqlCom.Parameters.Add(
    New SqlParameter("@ImageType", CType(Me.imageTypeComboBox.SelectedIndex, Object)))

    CN.Open()
    SqlCom.ExecuteNonQuery()
    CN.Close()





    To retrieve the image - this code is situated in the row enter event of a gridview on a form with a picture box as the user scrolls the table the image is displayed from the database field

    ' Get image data from gridview column.
    Dim imageData As Byte() = CType(DataGridView1.Rows(e.RowIndex).Cells("ImageData").Value, Byte())
    ' Initialize image variable
    Dim newImage As Image
    ' Read image data into a memory stream
    Using ms As MemoryStream = New MemoryStream(imageData, 0, imageData.Length)
        ms.Write(imageData, 0, imageData.Length)
        ' Set image variable value using memory stream.
        newImage = Image.FromStream(ms, True)
        ' set picture
        DefaultImagePictureBox.Image = newImage
    End Using


    the problem is not what you don't know it's what you think you know that's wrong
    Monday, June 2, 2008 12:55 AM

All replies

  • Read the Image file or Any file to a Byte()
    You can Simply use 
        
        Public Function FileToString(ByVal pFileName As StringAs String 
            Dim bytData As Byte() = System.IO.File.ReadAllBytes(pFileName)  
            Dim strText As String = System.Convert.ToBase64String(bytData)  
            Return strText  
        End Function
     

     


    Muthu Krishnan.R
    Friday, May 30, 2008 9:27 PM
  • Is your question really about how to store images in SQL?

    If it is then you create a table with an image field and read the images in and out as required

    To store the image - this stores the image into sql field based on a selected source document

    ' Read Image Bytes into a byte array
    Dim imageData As Byte() = ReadFile(txtImagePath.Text)

    Dim qry As String = "insert into FAImagesStore (FAID,ImageDesc,OriginalPath,ImageData,ImageType) values(@FAID, @ImageDesc, @OriginalPath, @ImageData, @ImageType)"

    Dim SqlCom As SqlCommand = New SqlCommand(qry, CN)

    ' sql image insert

    ' We are passing Original Image Path and Image byte data as sql parameters.
    SqlCom.Parameters.Add(New SqlParameter("@OriginalPath", CType(txtImagePath.Text, Object)))
    SqlCom.Parameters.Add(
    New SqlParameter("@ImageData", CType(imageData, Object)))
    SqlCom.Parameters.Add(
    New SqlParameter("@ImageDesc", CType(TextBoxDescription.Text, Object)))
    SqlCom.Parameters.Add(
    New SqlParameter("@FAID", CType(lID, Object)))
    SqlCom.Parameters.Add(
    New SqlParameter("@ImageType", CType(Me.imageTypeComboBox.SelectedIndex, Object)))

    CN.Open()
    SqlCom.ExecuteNonQuery()
    CN.Close()





    To retrieve the image - this code is situated in the row enter event of a gridview on a form with a picture box as the user scrolls the table the image is displayed from the database field

    ' Get image data from gridview column.
    Dim imageData As Byte() = CType(DataGridView1.Rows(e.RowIndex).Cells("ImageData").Value, Byte())
    ' Initialize image variable
    Dim newImage As Image
    ' Read image data into a memory stream
    Using ms As MemoryStream = New MemoryStream(imageData, 0, imageData.Length)
        ms.Write(imageData, 0, imageData.Length)
        ' Set image variable value using memory stream.
        newImage = Image.FromStream(ms, True)
        ' set picture
        DefaultImagePictureBox.Image = newImage
    End Using


    the problem is not what you don't know it's what you think you know that's wrong
    Monday, June 2, 2008 12:55 AM