Tolong pilih kategori sesuai, jenis posting (diskusi atau bukan) dan sertakan tag/topik yang sesuai seperti komputer, java, php, mysql, dll. Promosi atau posting tidak pada tempatnya akan kami hapus!
- Bagi Anda yang ingin mendaftar, baca link berikut:
http://diskusiweb.com/discussion/50491/how-to-registrasi-diskusiweb-com-baca-ini-terlebih-dahulu
- Cara menyisipkan kode program supaya tampil rapi dan terformat dengan baik di diskusiweb.com: http://www.diskusiweb.com/discussion/50415/cara-menyisipkan-kode-program-di-diskusiweb-com
- Cara posting gambar/image di post Anda: http://www.diskusiweb.com/discussion/47345/cara-menyisipkan-menyertakan-image-pada-posting/p1

[tanya] convert image ke byte array

tanya donk
apakah tiap kali convert picture file ke byte array itu... byte nya bakal berubah2 terus ya?

krn stiap kali saya compare image yg sama...
koq slalu beda ya ?

Comments

  • Harusnya byte-nya tetep sama...

    contoh mengubah image ke byte array:
        Public Sub Convert(ByVal From As System.Drawing.Image, ByRef Result() As Byte)
            Dim b() As Byte
            Dim mem As New System.IO.MemoryStream
            From.Save(mem, System.Drawing.Imaging.ImageFormat.Jpeg)
            b = mem.ToArray()
            Result = b
        End Sub
    
        Sub Main
            Dim byteArray() As Byte
            Convert(PictureBox1.Image, byteArray)
            ' byteArray telah terisi
        End Sub
    
  • hmm..
    husni, itu formatnya harus jpeg ya?

    bs tolong lihat, klo ini apa yg hrus d ganti?

        Private Sub m_LoadPicture(ByVal bPicture As Byte())
            '----------------------------------------------------------------------------
            ' Aim:              Load the picture from the byte array
            ' Name:		    m_LoadPicture
            ' Created:          17/02/2007              Last Modified:    17/02/2007
            '----------------------------------------------------------------------------
            ' Incoming Parameters:
            '                   bPicture    - Picture that is stored using byte array
            '----------------------------------------------------------------------------
            ' Return Value: None
            '----------------------------------------------------------------------------
            Dim picNew As New PictureBox
            Dim memoryStream As MemoryStream
            Dim pics As Image
    
            Try
                memoryStream = New System.IO.MemoryStream(bPicture)
                pics = System.Drawing.Image.FromStream(memoryStream)
                picPhoto.Image = pics
            Catch ex As Exception
                picPhoto.Image = picNew.Image
            End Try
        End Sub
        
        
        Private Sub picPhoto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picPhoto.Click
            Try
                sPicFile = g_sBrowsePic()
                picPhoto.Image = System.Drawing.Image.FromFile(sPicFile)
            Catch ex As Exception
                m_FillData()
            End Try
        End Sub
    
    Public Function g_bUpdatePic(ByVal sPicFileA As String, ByVal sTableA As String, ByVal sIDA As String) As Boolean
            '----------------------------------------------------------------------------
            ' Aim:              Update the picture record
    	' Name:             m_bUpdatePic
            ' Created:          26/02/2007              Last Modified:    20/05/2007
            '----------------------------------------------------------------------------
            ' Incoming Parameters:
            '            sPicFileA  - The picture file name
            '            sTableA    - The table whereby the picture will be saved
            '----------------------------------------------------------------------------
            ' Return Value: True if image successfully saved or False if not successful
            '----------------------------------------------------------------------------
            Dim fsPic As FileStream
            Dim bPic() As Byte
            Dim dbConnection As OleDbConnection
            Dim sSql As String
            Dim dbAdapter As OleDbDataAdapter
            Dim dbCommand As OleDbCommandBuilder
            Dim ds As DataSet
    
            Try
                fsPic = New FileStream(sPicFileA, FileMode.OpenOrCreate, FileAccess.Read)
                bPic = New Byte(fsPic.Length) {}
                fsPic.Read(bPic, 0, System.Convert.ToInt32(fsPic.Length))
                fsPic.Close()
    
                dbConnection = g_oConnection(DATABASE_NAME)
    
                ' Construct a SQL string and a connection object
                sSql = "SELECT * FROM " & sTableA & " WHERE " & sTableA & "_ID = " & sIDA.Replace("S", "").Replace("P", "")
    
                ' Create a data adapter and data set
                dbAdapter = New OleDbDataAdapter(sSql, dbConnection) ' Get the queried record
                dbCommand = New OleDbCommandBuilder(dbAdapter)
                ds = New DataSet(sTableA)
                dbAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
                ' Fill data adapter
    
                dbAdapter.Fill(ds, sTableA)
                ' Create a new row
                ds.Tables(sTableA).Rows(0)(sTableA & "_Picture") = bPic
                ' Save changes to the database
                dbAdapter.Update(ds, sTableA)
    
                ' Clean up connection
                If dbConnection Is Nothing Then
                    If dbConnection.State = ConnectionState.Open Then
                        dbConnection.Close()
                    End If
                    ' Dispose connection
                    dbConnection.Dispose()
                End If
                Return True
            Catch ex As Exception
                Return False
            End Try
        End Function
    
  • lalu code utk cek image nya berubah atau ga.. gw pake ini.. dan hasilnya selalu beda.. jadi mau ga mau gw tambahin supaya ga cek image
    Private Function m_bIsRecordChangedAndProceed(ByVal htOldDataImageA As Hashtable, ByVal oRecordA As Record) As Boolean
            '----------------------------------------------------------------------------
            ' Aim:              Check whether the record that viewed by the user before
            '                   they manipulate the database is the last updated record.
            '                   If something had changed, it will inform the user,
            '                   and user can choose either want to proceed the changes
            ' Name:		    m_bIsRecordChangedAndProceed
            ' Created:          26/02/2007              Last Modified:    27/02/2007
            '----------------------------------------------------------------------------
            ' Incoming Parameters:
            '               htOldDataImage - The old data image
            '               oRecordA - The record information
            '----------------------------------------------------------------------------
            ' Return Value: True to proceed changes or False to cancel changes
            '----------------------------------------------------------------------------
            Dim sChanged As String = ""
            Dim oOldEntry As DictionaryEntry
            Dim sSql As String = "SELECT * FROM " & oRecordA.Table & " WHERE " & oRecordA.PK & " = "
            Dim htLatestDataImage As New Hashtable
            Dim oLatestEntry As DictionaryEntry
    
            If oRecordA.PK <> "Supplier_Name" Then
                sSql &= oRecordA.PKValue
            Else
                sSql &= "'" & oRecordA.PKValue & "'"
            End If
            htLatestDataImage = g_htGetRecord(sSql)
    
            For Each oOldEntry In htOldDataImageA
                For Each oLatestEntry In htLatestDataImage
                    'Check and compare all data
                    sChanged &= m_IsRecordChangedByOtherUser(oOldEntry, oLatestEntry)
                Next
            Next
    
            'If something had changed, inform the user and let the user to
            'choose either want to overwrite or refresh the data
            If sChanged.Length > 0 Then
                If MsgBox("Some value have been changed as the following :" & vbCr & sChanged & vbCr & "Do you want to overwrite the latest data with your data?" & vbCr & vbCr & "Yes, to overwrite" & vbCr & "No, to discard changes and refresh the data.", MsgBoxStyle.YesNo, "Proceed Updating Data") = 6 Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return True
            End If
        End Function
    
        Private Function m_IsRecordChangedByOtherUser(ByVal oFirstEntry As DictionaryEntry, ByVal oSecondEntry As DictionaryEntry) As String
            '----------------------------------------------------------------------------
            ' Aim:              Check whether the first and second entry are same
            ' Name:		    m_IsRecordChangedByOtherUser
            ' Created:          01/03/2007              Last Modified:    04/03/2007
            '----------------------------------------------------------------------------
            ' Incoming Parameters:
            '               oFirstEntry - The first entry
            '               oSecondEntry - The second entry
            '----------------------------------------------------------------------------
            ' Return Value: String that store the key name that changed
            '----------------------------------------------------------------------------
            'If same key found, compare it. Password will not be checked
            If oFirstEntry.Key = oSecondEntry.Key And Not oFirstEntry.Key = "Staff_Password" Then
                If Not IsDBNull(oFirstEntry.Value) And Not IsDBNull(oSecondEntry.Value) Then
                    'If old and new value is not empty, compare the value
                    If Not oFirstEntry.Value.GetType.ToString = "System.Byte[]" Then
                        'Picture can't be compare as the byte may changed -_-"
                        If Not oFirstEntry.Value.Equals(oSecondEntry.Value) Then
                            Return " - [" & oFirstEntry.Key.ToString.Replace("_", " ") & "] : " & oFirstEntry.Value.ToString & " -> " & oSecondEntry.Value.ToString & vbCr
                        End If
                    End If
                ElseIf IsDBNull(oFirstEntry.Value) And Not IsDBNull(oSecondEntry.Value) Or (Not IsDBNull(oFirstEntry.Value) And IsDBNull(oSecondEntry.Value)) Then
                    'If old value is empty and new value is not empty or vice versa, the record marked as changed
                    Return oFirstEntry.Key & " "
                End If
            End If
            Return ""
        End Function
    
  • Originally posted by chronorick
    hmm..
    husni, itu formatnya harus jpeg ya?
    bs tolong lihat, klo ini apa yg hrus d ganti?

    ndak harus JPEG kok... cuma asumsi aja...

    sepertinya code-mu untuk load picture ga ad kesalahan...

    ___
    Originally posted by chronorick
    lalu code utk cek image nya berubah atau ga.. gw pake ini.. dan hasilnya selalu beda.. jadi mau ga mau gw tambahin supaya ga cek image
    Coba yang seperti ini, mungkin bisa membantu...
    Public Class FormTestCompare
    
        Dim d1, d2 As DictionaryEntry
    
        Sub Inisialisasi()
            d1.Key = "A"
            d1.Value = New Byte() {1, 2, 3, 4, 5}
            d2.Key = "B"
            d2.Value = New Byte() {1, 2, 3, 4, 5}
        End Sub
    
        Sub TidakSesuaiHarapan()
            MessageBox.Show(d1.Value.Equals(d2.Value))
        End Sub
    
        Sub SesuaiHarapan()
            MessageBox.Show(Sama(d1.Value, d2.Value))
        End Sub
    
        Function Sama(ByVal o1 As Object, ByVal o2 As Object) As Boolean
            Dim b1(), b2() As Byte
            b1 = CType(o1, Byte())
            b2 = CType(o2, Byte())
            If b1.Length <> b2.Length Then Return False
            For i As Integer = 0 To b1.Length - 1
                If b1(i) <> b2(i) Then Return False
            Next
            Return True
        End Function
    
        Private Sub Loading(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Inisialisasi()
            TidakSesuaiHarapan()
            SesuaiHarapan()
        End Sub
    End Class
    

    Apakah ada pikiran bahwa : metode tersebut tidak efektif dan boros resources! ?
  • [/code]
    For i As Integer = 0 To b1.Length - 1

    If b1(i) <> b2(i) Then Return False

    Next
    [/code]

    hmm kalo filenya gede apakah jg gpp husni ? misalnya 2-3.5MB ... btw knp equal lgsg.. koq ga bisa yah?

    terus mau nanya lagi nih.. koq kalo saya coba upload picture dari win application.. kualitas image ga turun.. sedangkan kalo dari web application kualitas nya turun jauh.. jadi pecah2

    yg win app.. saya lewat web service utk upload.. dan web app.. saya pake code yg dipake oleh web service tersebut utk upload pic.. code yg saya pake kaya gini... :
        Sub UploadPic()
            Dim picfile As String
            If flepic.PostedFile.ContentLength &gt; 0 Then
                picfile = flepic.PostedFile.FileName
                Try
                    flepic.PostedFile.SaveAs(Server.MapPath(&quot;Images\Userpic\&quot;) + Session.Item(&quot;loguser&quot;) + &quot;.jpg&quot;)
                    LoadPic()
                Catch Exp As Exception
                    Alert(&quot;There is problem occured when uploading your picture&quot;)
                End Try
            End If
        End Sub
    
    
        Function UploadPic(ByVal thepath As String, ByVal theitem As String, ByVal userpic As Byte())
            Dim picfile As String = System.Web.HttpContext.Current.Server.MapPath(&quot;Images/&quot; &amp; thepath &amp; &quot;/&quot; &amp; theitem &amp; &quot;.bmp&quot;)
            Dim serverpic As New FileInfo(picfile)
            If serverpic.Exists() Then
                serverpic.Delete()
            End If
            Dim filestream As FileStream
            filestream = serverpic.Open(IO.FileMode.OpenOrCreate)
            filestream.Write(userpic, 0, userpic.Length)
    
            filestream.Flush()
            filestream.Close()
            filestream = Nothing
            serverpic = Nothing
    
            &#039; Get a Bitmap.
            Dim bmppic As System.Drawing.Bitmap = System.Drawing.Bitmap.FromFile(picfile)
            Dim jpgfile As String = System.Web.HttpContext.Current.Server.MapPath(&quot;Images/&quot; &amp; thepath &amp; &quot;/&quot; &amp; theitem &amp; &quot;.jpg&quot;)
    
            bmppic.Save(jpgfile, System.Drawing.Imaging.ImageFormat.Jpeg)
            bmppic.Dispose()
            bmppic = Nothing
    
            Dim serverpicfile As String = System.Web.HttpContext.Current.Server.MapPath(&quot;Images/&quot; &amp; thepath &amp; &quot;/&quot; &amp; theitem &amp; &quot;.bmp&quot;)
    
            Dim serverpic2 As New IO.FileInfo(serverpicfile)
            If serverpic2.Exists() Then
                serverpic2.Delete()
            End If
        End Function
    

    untuk form HTML nya
    &lt;form id=&quot;frmProfile&quot; action=&quot;&quot; method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; runat=&quot;Server&quot;&gt;
    

    untuk yg web serv code yg d pake
    
        &lt;WebMethod(MessageName:=&quot;Upload User Picture&quot;, Description:=&quot;Upload a user picture to the server&quot;)&gt; _
        Public Function UploadUserPic(ByVal theuser As String, ByVal userpic As Byte()) As Boolean
            File.UploadPic(&quot;Userpic&quot;, theuser, userpic)
            Return True
        End Function
    

    untuk win app yg panggil web serv tersebut code yg dipake
    
        Function UpdateCustPic()
            If tempcustid &lt;&gt; cmbcustid.Text And customeraddclicked = False Then
                wsfileDB.UpdateUserPic(tempcustid, cmbcustid.Text)
            End If
    
            If tempcustpic &lt;&gt; &quot;from server&quot; Then
                Try
                    Dim sourcepic As New IO.FileInfo(tempcustpic)
                    Dim filestream As IO.FileStream
    
                    filestream = sourcepic.OpenRead
                    Dim convertpic(filestream.Length) As Byte
                    filestream.Read(convertpic, 0, filestream.Length)
                    filestream.Close()
                    If customeraddclicked = True Then
                        tempcustid = cmbcustid.Text
                    End If
    
                    If wsfileDB.UploadUserPic(tempcustid, convertpic) = False Then
                        DisplayError(programname, 1, &quot;- Customer picture can&#039;t be uploaded.&quot;)
                    End If
    
                    filestream = Nothing
                    sourcepic = Nothing
                Catch ex As Exception
                    DisplayError(programname, 1, &quot;- The picture is being used, please close it first.&quot;)
                End Try
            End If
        End Function
    
  • hmm kalo filenya gede apakah jg gpp husni ? misalnya 2-3.5MB
    Tak apa apa..
    Saya coba proses gambar BMP dengan ukuran 2.25 MB, di komputer saya waktu proses tidak ada satu detik..
    btw knp equal lgsg.. koq ga bisa yah?
    Saya juga kurang tahu yang ini. Mungkin mas dede atau mas vberror13 tahu.. Masuk sini dong...
    Saya kira method Equals di dalamnya ada algoritma tertentu, entah itu mambandingkan per byte, atau cara seperti apa kita tidak tahu. Tapi kalau dia membandingkan per byte, harusnya hasilnya sama dengan prosedur yang saya buat itu.
    terus mau nanya lagi nih.. koq kalo saya coba upload picture dari win application.. kualitas image ga turun.. sedangkan kalo dari web application kualitas nya turun jauh.. jadi pecah2
    Sepertinya kamu mengkonversi gambar yang sudah diupload ke format JPEG. Mungkin hal itu yang membuat kualitas gambar turun. Saya lihat kode yang untuk win app tidak ada perintah untuk konversi ke JPEG tuh...
  • Dim convertpic(filestream.Length) As Byte
    filestream.Read(convertpic, 0, filestream.Length)

    eh... apa nggak bikin bytenya bertambah satu ?
    Dim convertpic(filestream.Length-1) As Byte
    filestream.Read(convertpic, 0, filestream.Length-1)
  • ini pindahin ke topik terjawab aja...

    trims atas bimbingan husni... n pmbtulan mas vberror =D
Sign In or Register to comment.