逐字节读取文件,然后以像素为单位转换一个字节 [英] Read file byte by byte and then converting one byte in pixel

查看:104
本文介绍了逐字节读取文件,然后以像素为单位转换一个字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是读二进制文件。

i希望通过一个字节然后下一个字节读取二进制文件,依此类推。

获取一个字节后我需要转换它字节到像素。

这样我需要读取整个8mb文件并从文件中的字节存储创建一个图像。

i am new in vb.net

请帮助我。

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 vb.net
please help me.

推荐答案

将图像写入文件使用

To Write the image to file use
Private Sub WriteImage(ByVal image As Image, ByVal path As String)
			If image Is Nothing Then
				Return
			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.
						writer.Write(bitmap.Width)
					   writer.Write(bitmap.Height)
						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)

								writer.Write(color.ToArgb())
							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)
					Do
						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
					Loop
				End Using
			End Using
End Function


这可能并不像你想象的那么容易:每个部分都非常简单,但实际的拼凑位需要一些思考。



最简单的方法是:

1)阅读将整个文件转换为一个字节数组。

2)运行数组,将每个字节转换为图像中的pixcel

3)将图像输出为文件。



第一部分非常简单: File.ReadAllBytes [ ^ ] w生病了。

最后一部分很简单: Image.Save [ ^ ]会做对你而言。

中间的位置也不难,但确实需要一点点思考。

你需要做出的主要决定是:

1)字节值与pixcel有什么关系?这是单色 - 即零是黑色,非零是白色,反之亦然?或者灰度 - 即字节是否包含与图像的灰度值相关的值?或颜色 - 即字节值包含红色,绿色和蓝色元素?或者数据是红色字节,蓝色字节和绿色字节(也可能是Alpha组件字节?

2)输出图像应该有多大?在某种程度上,这受字节/像素关系的影响,但即使一个字节==一个像素,图像本身也可以是正方形或矩形,并且仍然使用相同数量的字节。如果你弄错了,图像将是可读的!

3)是否(gulp)以任何方式压缩的二进制输入数据?它是以JPEG文件还是GIF开始生活的?因为如果确实如此,那么这是一个非常非常糟糕的主意! :笑:



这听起来像你的作业,所以我不能给你任何代码,直到我知道你自己尝试过,但首先考虑以上,并尝试提出一些答案。
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.


这篇关于逐字节读取文件,然后以像素为单位转换一个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆