none
Vb.net - convert Raw-Image-Data into Image File (*.jpg) but....

    Question

  • The coding below is to retrieve the Raw Image Data from the Database and then write as a JPG image file.  The problem is the image file (image.jpg) is "nothing" after file created.  I guess there is something wrong in the following coding.

    fs.Write(rawData, 0, fileSize)

    No any runtime errors I can find, and I double check rawData (i.e. Buffer) contains data.  But don't know why there is "nothing" inside the image (image.jpg).  Please help :-)


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            Dim fileSize As Integer
            Dim rawData() As Byte
            Dim fs As FileStream

            Dim myDAL As New ImagingDAL
            Dim data As New ImagingData

            data = myDAL.SelectOneImage()

            If Not data Is Nothing Then
                With data
                    fileSize = .ImageSize
                    rawData = .ImageRawData
                End With
            End If

            rawData = New Byte(fileSize) {}
            fs = New FileStream("c:\temp\image.jpg", FileMode.OpenOrCreate, FileAccess.Write)
            fs.Write(rawData, 0, fileSize)
            fs.Close()

        End Sub

    Wednesday, April 28, 2010 9:47 AM

Answers

  • Hi iHandler,

    There is a logic mistake in the code. In the Button2_Click event handler, first you assign the image data to rawData, but after that you assign a new byte array to rawData again which makes rawData empty. You do not need "rawData = New Byte(fileSize) {}" statement.

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            Dim fileSize As Integer
            Dim rawData() As Byte
            Dim fs As FileStream

            Dim myDAL As New ImagingDAL
            Dim data As New ImagingData

            data = myDAL.SelectOneImage()

            If Not data Is Nothing Then
                With data
                    fileSize = .ImageSize
                    rawData = .ImageRawData
                End With
            End If

            'rawData = New Byte(fileSize) {} ' Do not need this, delete it
            fs = New FileStream("c:\temp\image.jpg", FileMode.OpenOrCreate, FileAccess.Write)
            fs.Write(rawData, 0, fileSize)
            fs.Close()

        End Sub

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by iHandler Saturday, May 01, 2010 6:09 AM
    Friday, April 30, 2010 3:20 AM
    Moderator

All replies

  • No that code is to retrieve data from a file, why don't you look at the answer I've given you on the same previous question from you?

    Is there something technical wrong in these forums?

     

     


    Success
    Cor
    Wednesday, April 28, 2010 12:07 PM
  • Hi iHandler,

    There is a logic mistake in the code. In the Button2_Click event handler, first you assign the image data to rawData, but after that you assign a new byte array to rawData again which makes rawData empty. You do not need "rawData = New Byte(fileSize) {}" statement.

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

            Dim fileSize As Integer
            Dim rawData() As Byte
            Dim fs As FileStream

            Dim myDAL As New ImagingDAL
            Dim data As New ImagingData

            data = myDAL.SelectOneImage()

            If Not data Is Nothing Then
                With data
                    fileSize = .ImageSize
                    rawData = .ImageRawData
                End With
            End If

            'rawData = New Byte(fileSize) {} ' Do not need this, delete it
            fs = New FileStream("c:\temp\image.jpg", FileMode.OpenOrCreate, FileAccess.Write)
            fs.Write(rawData, 0, fileSize)
            fs.Close()

        End Sub

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by iHandler Saturday, May 01, 2010 6:09 AM
    Friday, April 30, 2010 3:20 AM
    Moderator
  • Alex,

    Why are you given this sample, I've showed in another question from the OP with the same question a sample how to retrieve data from a database, whatever times an OP tells that.

    You cannot get an image from a database by using code to get an image from a disk (or other medium which has files).

     


    Success
    Cor
    Friday, April 30, 2010 6:53 AM
  • Dear Alex,

    Thanks a lot, your answer is correct.  Appreicate that a lot ~~

    Saturday, May 01, 2010 6:10 AM