Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 VB.NET
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 14-Mar-13 10:33am
Comments
ThePhantomUpvoter at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

"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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

 
 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
 
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 245
1 Kamal Rocks 184
2 Sergey Alexandrovich Kryukov 175
3 BillWoodruff 173
4 PIEBALDconsult 160
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 12 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100