Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    Unire files binari

    Volevo dire che ero riuscito a farmi un codice sorgente per unire dei file immagini in un unico file contenitore.
    Ma ho un problema.

    Per ogni file che crea il programma ci mette prima questo testo
    codice:
    ......................QSystem.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a......System.Drawing.Bitmap.....Data.................'...
    e dopo aggiunge il testo dei files da unire

    Io voglio che tale testo non venga aggiunto, il codice del mio programma è

    codice:
    Imports System.IO
    
    Public Class Form1
    
    Private Sub btn_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_add.Click
    
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    
    Dim S() As String = OpenFileDialog1.FileNames 'un array che contiene i nomi dei file scelti 
    
    Dim File As String
    
    Dim sr As New IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open)
    
    Dim fmt As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    
    For Each File In S
    
    ListBox1.Items.Add(File)
    
    Next
    
    sr.Close()
    
    End If
    
    End Sub
    
    Private Sub btn_create_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_create.Click
    
    
    If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    
    Dim sw As New IO.FileStream(SaveFileDialog1.FileName, IO.FileMode.Create)
        Dim fmt As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        Dim bmp As Bitmap
        For Each item As String In ListBox1.Items
          bmp = CType(Bitmap.FromFile(item), Bitmap)
          fmt.Serialize(sw, bmp)
        Next
        sw.Close()
    End If
    
    End Sub
    
    End Class
    Spero possiate aiutarmi nonostante i riproverevoli precedenti con "alka"

    flash.tato

  2. #2
    Per ogni file che crea il programma ci mette prima questo testo
    codice:
    ......................QSystem.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a......System.Drawing.Bitmap.....Data.................'...
    Mi stupirei del contrario! Questa roba è semplicemente l'intestazione delle tue bitmap serializzate...
    Scusa, mi spieghi cosa dovrebbe fare esattamente questo codice?
    codice:
        Dim fmt As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter 'Cosa c'entra la serializzazione?!
        Dim bmp As Bitmap 'e le bitmap?!!
        For Each item As String In ListBox1.Items
          bmp = CType(Bitmap.FromFile(item), Bitmap) Queste due righe poi non le capisco assolutamente...
          fmt.Serialize(sw, bmp)
        Next
        sw.Close()
    E questo codice? È il delirio!
    codice:
    Private Sub btn_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_add.Click
    
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    
    Dim S() As String = OpenFileDialog1.FileNames 'un array che contiene i nomi dei file scelti 
    
    Dim File As String
    
    Dim sr As New IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open) Perché apri adesso lo stream in lettura?
    
    Dim fmt As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter E questo adesso cosa c'entra?
    
    For Each File In S
    
    ListBox1.Items.Add(File)
    
    Next
    
    sr.Close()
    
    End If
    
    End Sub
    Ora non ho sottomano .NET (sono sulla mia macchina Linux), ma provo a buttarti giù del codice corretto:
    codice:
    'La sub btn_add_Click si salva quasi completamente
    Private Sub btn_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_add.Click
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    
    Dim S() As String = OpenFileDialog1.FileNames 'un array che contiene i nomi dei file scelti 
    
    Dim File As String
    
    For Each File In S
    
    ListBox1.Items.Add(File)
    
    Next
    
    End If
    
    End Sub
    codice:
    'La sub btn_create_Click invece va riscritta quasi in blocco.
    Private Sub btn_create_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_create.Click
    If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    Dim sw As FileStream
    Dim sr As FileStream
    Dim buffer(32767) As Byte 'Dimensioni aumentabili o diminuibili a piacere
    Dim bytecount As Integer
    Try
        sw=New FileStream(SaveFileDialog1.FileName, FileMode.Create)
    Catch ex As Exception
        MessageBox.Show("Impossibile creare il file di destinazione.")
        Exit Sub
    End Try
        For Each item As String In ListBox1.Items
    Try
           sr=new FileStream(item, IO.FileMode.Open)
    Catch ex As Exception
        MessageBox.Show("Impossibile aprire il file sorgente.")
        Continue For 'Salta al file successivo
    End Try
           Do
           sr.Read(buffer,0,buffer.Length)
           If bytecount = 0 Then Exit Do 'Lo so, è brutto.
           sr.Write(buffer,0,buffer.Length)
           Loop
           sr.Close()
        Next
        sw.Close()
    End If
    End Sub
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    MItaly mi crea un file con 0 Byte

    E nn capisco perchè

  4. #4
    Leggendo in Internet ho trovato una cosa che sembra fare al caso mio

    codice:
        Private Sub btn_create_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_create.Click
            If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                For Each item As String In ListBox1.Items
                    Dim New_file As StreamWriter
                    New_file = File.AppendText(SaveFileDialog1.FileName)
                    New_file.Write(item)
                    New_file.Close()
                Next
            End If
        End Sub
    Il WriteLine non mi serve deve scrivere tutto sulla stessa linea.

    Il file me lo crea solo che il testo che aggiunge sono le Path dei file (che sono il testo delle Item) ma come faccio a dire al programma che quelle sono solo le path e non il testo che deve incollare.

    Infatti se le ListBox1.Items sono:
    codice:
    C:/ciao.txt
    C:/bye.txt
    Il testo del file che mi crea è
    codice:
    C:/ciao.txtC:/bye.txt
    Mentre quelle sono solo le path dei file.
    Quindi direi che il codice qua sopra presentato mi basterebbe solo che nn so come modificarlo al caso mio.

  5. #5
    Ci sono riuscito però non và bene per unire dei files binari

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.