Lectura y Modificación de bytes

(1/1)

ramonpsx:
Hola como están

soy nuevo y recién me registro aquí, apenas me estoy pasando a Vb.NET y no logro entender algunas cosas, maneje mucho el Vb 6.0 y se muy bien todo , pero ahora que ya estoy en NET se me dificulta un poco.

en mis ratos libres hacia aplicaciones en VB6 para juegos  y usaba este fácil código :

Código
Dim textura() as byte
Dim file as string
Dim tamaño as long
Dim ruta_textura as string
Dim offset as integer
 
file = "C:/DP00.dat"
ruta_textura = "C:/Player.bin"
 
tamaño = FileLen (ruta_textura)
 
ReDim textura (tamaño) as byte
 
open ruta_textura for binary as #1
Get#1, ,textura
close#1
 
open file for binary as #1
offset = 2068
Put#1, offset,textura
close#1
 
Msgbox"La Textura se ha insertado correctamente en el archivo",vbinformation
 

Aquí lo que leía era un toda una imagen o cualquier archivo completo y lo insertaba al otro archivo en en el offset que se le asigno  

pero en .NET no encuentro la manera de hacerlo , o por lo menos modificar aunque sea un solo byte

espero y puedan ayudarme con esto ,gracias

Mod: los códigos deben ir en etiquetas GeSHi

Eleкtro:
Lee la documentación oficial en MSDN para aprender cómo manejar Streams en .Net, en especial los miembros de las siguientes clases:

➢ Stream
➢ FileStream
➢ BinaryReader
➢ BinaryWriter

Este podría ser el equivalente que buscas:

Código
Imports System.IO

Código
Dim datFilePath As String = "C:/DP00.dat"
Dim texFilePath As String = "C:/Player.bin"
 
Dim datOffset As Integer = 2068I
Dim texData As Byte() = File.ReadAllBytes(texFilePath)
 
Using datStream As New FileStream(datFilePath, FileMode.Open)
 
   Using datWriter As New BinaryWriter(datStream)
 
       datWriter.Seek(offset:=datOffset, origin:=SeekOrigin.Begin)
       datWriter.Write(buffer:=texData)
       datWriter.Flush()
 
   End Using
 
End Using
 
MessageBox.Show("La Textura se ha insertado en el archivo.", "Título",
               MessageBoxButtons.OK, MessageBoxIcon.Information)

Nota: La class BinaryWriter no es del todo necesaria en el código de arriba ya que puedes escribir los bytes directamente con la class FileStream, era solo para mostrarte un modo de empleo.

Saludos

ramonpsx:
Muchas gracias por contestar

el código funciono correctamente

una ultima pregunta para terminar este tema. al momento de insertar la textura  también necesito insertar los colores de la imagen conocidas como paletas , que van de 32 y 512 bytes , que se leen a partir del offset 20 del archivo

Código
Dim textura() as byte
Dim file as string
Dim tamaño as long
Dim ruta_textura as string
Dim offset as integer
Dim ruta_paleta as string
 
Dim paleta (32)  as byte
 
 
file = "C:/DP00.dat"
ruta_textura = "C:/Player.bin"
ruta_paleta = "C:/Player.tim"
 
 
tamaño = FileLen (ruta_textura)
 
ReDim textura (tamaño) as byte
 
 
open ruta_paleta for binary as #1
Get#1, 20 , paleta
close#1
 
open ruta_textura for binary as #1
Get#1, ,textura
close#1
 
open file for binary as #1
offset = 2068
Put#1, offset, textura
close#1
 
open file for binary as #1
offset = 100144
Put#1, offset, paleta
close#1
 
 
Msgbox"La Textura se ha insertado correctamente en el archivo",vbinformation
 
 


y acá en NET ando algo perdido , me podría ayudar a hacer el código ?


Código
Imports System.IO


Código
Dim datFilePath As String = "D:/GDC_GDJ.bin"
       Dim texFilePath As String = "D:/balon rojo.bin"
       Dim palFilePath As String = "D:/balon rojo.tim"
 
 
       Dim datOffset As Integer = Val(TextBox3.Text)
       Dim palOffset As Integer = Val(TextBox4.Text)
 
       Dim pal As Integer = 20
 
 
       'Leyendo e insertando la Textura ----------------------------------------------------
       Dim texData As Byte() = File.ReadAllBytes(texFilePath)
 
 
       Using datStream As New FileStream(datFilePath, FileMode.Open)
 
           Using datWriter As New BinaryWriter(datStream)
 
               datWriter.Seek(offset:=datOffset, origin:=SeekOrigin.Begin)
               datWriter.Write(buffer:=texData)
               datWriter.Flush()
 
           End Using
 
       End Using
 
       'leyendo e insertando la paleta -------------------------------------------
 
       Dim paleta(32) As Byte
 
       Using datStream As New FileStream(palFilePath, FileMode.Open)
 
           Using datReader As New BinaryReader(datStream)
 
 
               datStream.Seek(offset:=pal, origin:=SeekOrigin.Begin)
               datStream.Read(paleta, 0, 0)
               datStream.Flush()
 
           End Using
 
       End Using
 
       Using datStream As New FileStream(datFilePath, FileMode.Open)
 
           Using datwriter As New BinaryWriter(datStream)
 
               datwriter.Seek(offset:=palOffset, origin:=SeekOrigin.Begin)
               datwriter.Write(buffer:=paleta)
               datwriter.Flush()
 
           End Using
 
       End Using
 
 
       MessageBox.Show("La Textura se ha insertado en el archivo.", "Título",
                       MessageBoxButtons.OK, MessageBoxIcon.Information)
 
   End Sub
 

aun no lo entiendo muy bien , no se si sea con BinaryReader o con cual
Muchas Gracias por todo , Saludos desde Monterrey , Mexico     :D

Navegación

[0] Índice de Mensajes