Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
MY problem is reading binary file.
i want to read binary file through one byte then next byte and so on.
after getting one byte i need to convert that byte into pixel.
in this way i need to read whole 8mb file and creating one image from byte store in a file.
i am newly in
please help me.
joshrduncan2012 1-Apr-13 12:46pm    
What have you tried? Where is your code? Are you wanting someone to do the work for you?
Vishal Makwana 1-Apr-13 14:30pm    
no. i tried it but i can't get any values from file.
Zoltán Zörgő 1-Apr-13 13:53pm    
Some questions you have to asnwer (for yourself first):
- what image file and pixel format do you intend to use for your output file?
- what pixel format do you have as input?
Just a note: a pixel is a raster image point, that is represented by one or bits in file and GDI memory. These two representations might not be the same (in general they are not the same). So you can not convert a byte to a pixel, since they are from different space.

To Write the image to file use
Private Sub WriteImage(ByVal image As Image, ByVal path As String)
			If image Is Nothing Then
			End If
			Using stream As Stream = File.Create(path)
				Using writer As New BinaryWriter(stream)
					Using bitmap As New Bitmap(image)
						'write the height and width of the image.
						For x As Integer = 0 To image.Width - 1
							For y As Integer = 0 To image.Height - 1
								'write the color data as Int32 ToArgb();
								Dim color As Color = bitmap.GetPixel(x, y)

							Next y
						Next x
					End Using
				End Using
			End Using
End Sub

Now we have the image saved as an array of pixels. Note the width and height are the first values stored for when we read the file back. To read the file back to an image use this.
Private Function ReadImage(ByVal path As String) As Image
			Dim height As Integer
			Dim width As Integer
			Using stream As Stream = File.OpenRead(path)
				Using reader As New BinaryReader(stream)
					width = reader.ReadInt32()
					height = reader.ReadInt32()
					Dim bitmap As New Bitmap(width, height)
						For w As Integer = 0 To width - 1
							For h As Integer = 0 To height - 1
								Dim color As Color = Color.FromArgb(reader.ReadInt32())
								bitmap.SetPixel(w, h, color)
							Next h
						Next w
						Return bitmap
				End Using
			End Using
End Function
Share this answer
Vishal Makwana 10-Apr-13 12:29pm    
Thank you so much.

This code is some what closer to my code.
charles henington 10-Apr-13 16:16pm    
not a problem if this helps please mark the solution as answered :)
This probably isn't as easy as you think: each of the individual parts are pretty easy, but the actual putting-it-together bit needs some thought.

The easiest way to do this is to:
1) Read the whole file into an array of bytes.
2) Run through the array, converting each byte to a pixcel in an Image
3) Output the image as a file.

The first part is trivially easy: File.ReadAllBytes[^] will do that for you.
The last part is trivially easy: Image.Save[^] will do that for you.
The bit in the middle isn't difficult either, but it does take a little thought.
The major decisions you need to make are:
1) How does the byte value relate to a pixcel? Is this monochrome - i.e. zero is black, non-zero is white, or vice versa? Or greyscale - i.e. the byte holds a value which relates to the "greyness" value of the image? Or colour - i.e. a byte value contains a red, a green and a blue element? Or is the data a red byte, a blue byte and a green byte (with a possible Alpha component bytes as well?
2) How big is the output image supposed to be? To an extent, this is influenced by the byte / pixel relationship, but even if one byte == one pixel, the Image itself could be square or rectangular and still use the same number of bytes. If you get it wrong, the image will be be readable!
3) Is (gulp) the binary input data compressed in any way? Did it start life as a JPEG file, or a GIF? because if it did, then this is a very, very poor idea! :laugh:

This sounds like your homework, so I can't give you any code until I know you have tried it yourself, but first think about the above, and try to come up with some answers.
Share this answer
Vishal Makwana 1-Apr-13 14:39pm    
Thanks for your reply.
file is data ie. red byte , green, blue byte.
i have to set the byte values in pixel using SetPixel function in GDI Image.
it will generate rectangular image.
OriginalGriff 1-Apr-13 14:58pm    
Fine - so you need to know the horizontal and vertical sizes of the image.
And if you know how to use SetPixel, then why are you having a problem? Which bit is difficult?
Vishal Makwana 7-Apr-13 13:50pm    
i have 8mb size of file till now i am able to read the byte from file using

red_fm_file = New FileStream(red_fm, FileMode.Open)

Dim red_fm_length As Integer = red_fm_file.Length
Dim i As Integer

For i = 0 To red_fm_length

red_fm_byte = red_fm_file.ReadByte()
Next i

next i am use this method to draw a image from byte.

Public Sub value_to_image()

Dim mybitmap As Bitmap = Nothing
For i = 0 To 2345
For j = 0 To 2741
mybitmap.SetPixel(i, j, Color.FromArgb(red_fm_byte, 150, 250))
Next j
Next i
Image_Veiwer.pic.Image = mybitmap

End Sub

but am not able to draw any image using it..
please help me...
OriginalGriff 8-Apr-13 3:47am    
Do you get an error? Or is the picture distorted?
Is your code exactly as per your code above, or did you remove a lot of stuff?

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