Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need To Read a Image file which is Binary File in to a Byte and then Convert that byte data values in to an Bitmap Image.

Here is my code i get error "File Not Found "when i will call it in my button click

as:- bitmap_image()

---------------------------------------

my function is here to create bitmap image.

Public Sub bitmap_image() '(ByVal sender As Object, ByVal e As System.EventArgs) '(ByVal open_file_name As String)


Dim total_byte As Long
Dim l As Long = 0
Dim n As Integer = 0

Dim image_file As FileStream = New FileStream(open_file_name, FileMode.Open, FileAccess.Read)

total_byte = New System.IO.FileInfo(open_file_name).Length

Dim ms As MemoryStream = New MemoryStream()

Dim i As Integer = 0


image_file.Seek(0, SeekOrigin.Begin)
'Seek(strat)
For i = 0 To total_byte
n = image_file.ReadByte()
ms.WriteByte(n)
's = s + Convert.ToChar(n)

Next
'Return s
Dim bit_map As New Bitmap(ms)
pic.Image = bit_map
image_file.Close()

End Sub

----------------------------------------

Any thing wrong???
Posted
Comments
[no name] 14-Mar-13 15:53pm    
Well obviously there is something wrong.... you are getting a "File not found" exception are you not? That error and the title of your posting have nothing to do with each other. What line is the error occurring on? The error message is telling you exactly what the problem is, you are trying to access a file that does not exist.
Sergey Alexandrovich Kryukov 14-Mar-13 19:22pm    
What "binary file"? Is it not an any of the well-known bitmap formats .NET can read without your code? If so, isn't it obvious that you need to tell us this format?
Overall, the whole post makes no sense.
—SA

First of all Read the file using FileStream and ReadByte method and store the byte value in any variable as Integer Type.

Then take object of bitmap and use "SetPixel" function to convert the byte value into a color
and then draw image using this bitmap.
as in my case i have to read three file for to get Red , Green ,Blue color

see the code below.

Dim red_fm_file, green_fm_file, blue_fm_file As FileStream

Dim mybitmap As New Bitmap(img_width, img_height)
red_fm_file = New FileStream(Module1.red_band, FileMode.Open, FileAccess.Read)
green_fm_file = New FileStream(Module1.green_band, FileMode.Open, FileAccess.Read)
blue_fm_file = New FileStream(Module1.blue_band, FileMode.Open, FileAccess.Read)

Dim red_fm_length As Integer = red_fm_file.Length
Dim green_fm_length As Integer = green_fm_file.Length
Dim blue_fm_length As Integer = blue_fm_file.Length

Dim i As Integer
Dim j As Integer
'Dim k As Integer
red_fm_file.Seek(0, SeekOrigin.Current)
green_fm_file.Seek(0, SeekOrigin.Current)
blue_fm_file.Seek(0, SeekOrigin.Current)


For i = 0 To 100 '2741 '501
For j = 0 To 100 '2345 '2345 '500

red_fm_byte = red_fm_file.ReadByte()
green_fm_byte = green_fm_file.ReadByte()
blue_fm_byte = blue_fm_file.ReadByte()


mybitmap.SetPixel(i, j, Color.FromArgb(red_fm_byte, green_fm_byte, blue_fm_byte))

Next j
Next i
red_fm_file.Close()
green_fm_file.Close()
blue_fm_file.Close()

pic.Image = mybitmap
 
Share this answer
 
"File not found" has a very simple cause: the file name you have given does not exist.

Since you don't show where the open_file_name variable is set, You need to look at that code to work out why the name is not a good path to the file. It could be anything at this stage:
The file name could be pathless - in which case I would expect the file in the application current directory (for a web page that is the folder holding the page)
The file name could have a relative path, but the folder is not where you expect.
The file could have an absolute path, but be spelled wrong.

Look at the variable content first: then look at where you think the file should be from that.
 
Share this answer
 
VB
Private Function GetByteFromBitmap(oImg As Bitmap) As Byte()
       Dim buffer() As Byte = Nothing
       Try
           If Not IsNothing(oImg) Then
               Using ms As New IO.MemoryStream()
                   oImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
                   buffer = ms.GetBuffer
                   ms.Close()
               End Using
           End If
       Catch ex As Exception
           MsgBox(ex.Message)
       End Try
       Return buffer
   End Function

   Private Function GetBitmapFromByte(oImg As Byte()) As Bitmap
       Dim buffer As Bitmap
       Using ms As New IO.MemoryStream(oImg)
           buffer = New Bitmap(ms)
           ms.Close()
       End Using
       Return buffer
   End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900