将字节数据值转换为位图图像 [英] Converting byte data values in to an Bitmap Image

查看:209
本文介绍了将字节数据值转换为位图图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将二进制文件的图像文件读入字节,然后将该字节数据值转换为位图图像。



这是我的代码我收到错误找不到文件,我将在我的按钮中调用它点击



as: - 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 =新的System.IO.FileInfo(open_file_name).Length



Dim ms作为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)



下一页

''返回s

Dim bit_map As New Bitmap(ms)

pic.Image = bit_map

image_file.Close()



结束次级



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



有什么问题???

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???

推荐答案

首先使用FileStream和ReadByte方法读取文件,并将字节值存储在任何变量中作为整数类型。



然后取对象位图并使用SetPixel函数将字节值转换为颜色

,然后绘制图像使用这个位图。

在我的情况下我必须阅读三个文件才能获得红色,绿色,蓝色



查看代码下面。



Dim red_fm_file,green_fm_file,blue_fm_file As FileStream



将mybitmap调暗为新位图(img_width, img_height)

red_fm_file =新FileStream(Module1.red_band,FileMode.Open,FileAccess.Read)

green_fm_file =新FileStream(Module1.green_band,FileMode.Open,FileAccess。阅读)

blue_fm_file =新FileStream(Module1.blue_band,FileMode.Open,FileAccess.Read)



Dim red_fm_length As Integer = red_fm_file。长度

Dim green_fm_length As Integer = green_fm_file.Length

Dim blue_fm_length As Integer = blue_fm_file.Length



Dim我作为整数

Dim j作为整数

''Dim k As Integer

red_fm_file.S eek(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))



下一页j

下一页我

red_fm_file.Close()

green_fm_file。关闭()

blue_fm_file.Close()



pic.Image = mybitmap
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


文件找不到有一个非常简单的原因:你给出的文件名不存在。



由于你没有显示设置 open_file_name 变量的位置,你需要查看该代码,找出为什么名称不是文件的好路径。在这个阶段它可能是任何东西:

文件名可能是无路径的 - 在这种情况下,我希望应用程序当前目录中的文件(对于作为持有页面的文件夹的网页)

文件名可以有一个相对路径,但文件夹不在你预期的位置。

文件可能有一个绝对路径,但拼写错误。



首先查看变量内容:然后查看文件应该从哪里开始。
"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.


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


这篇关于将字节数据值转换为位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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