Question Replace Image File In a File

ChronoCross

New member
Joined
Aug 27, 2011
Messages
1
Programming Experience
1-3
hi all, I am new member in here, I want asking, I have program that read the image from file , but I can show that image file into picturebox, Here my code to read the image:

VB.NET:
Try
            Dim path As String
            path = OpenFileDialog1.FileName
            Using fs As New FileStream(path, FileMode.Open)
                Using rdr As New BinaryReader(fs)
                    fs.Position = 0
                    Dim Image As System.Drawing.Image = Nothing
                    fs.Seek(268, System.IO.SeekOrigin.Begin)

                    Dim BinaryReader As System.IO.BinaryReader = New System.IO.BinaryReader(fs)
                    Dim ImageLength As System.Int32 = BinaryReader.ReadInt32

                    fs.Seek(296, System.IO.SeekOrigin.Begin)

                    Dim ImageOffset As System.Int32 = BinaryReader.ReadInt32
                    fs.Seek(ImageOffset, System.IO.SeekOrigin.Begin)

                    Dim Buffer(ImageLength - 1) As System.Byte
                    Dim BytesRead As System.Int32 = 0

                    BytesRead = fs.Read(Buffer, 0, Buffer.Length)

                    If BytesRead = Buffer.Length Then
                        Dim MemoryStream As System.IO.MemoryStream = New System.IO.MemoryStream(Buffer)
                        Image = System.Drawing.Image.FromStream(MemoryStream).Clone
                        PictureBox1.Image = Image
                        MemoryStream.Close()
                        MemoryStream.Dispose()
                    End If

                End Using
            End Using
            
        Catch ex As Exception
            MsgBox(ex.Message , vbCritical, "Error!")
        End Try

268 is offset that contain image size and 296 is offset that contains the image.
I want user can replace image on offset 296 and write size of selected image to 268.
Sorry If My English so bad, Thanks!
 
Last edited:
Back
Top