将图像转换为二进制数组 [英] Converting an image into binary array
本文介绍了将图像转换为二进制数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好我如何将图像转换为二进制数组?
解决方案
如果你使用System.Drawing,你可以试试这个代码
Imports System.Drawing
导入 System.Drawing.Imaging
公共 共享 功能 GetBinary( ByVal image 作为图片, ByVal format As ImageFormat) As 字节()
使用 ms 作为 新 System.IO.MemoryStream
如果(格式是 Nothing )然后
format = image.RawFormat ' 使用图像原始格式
结束 如果
image.Save(ms,format)
返回 ms.ToArray()
结束 使用
结束 功能
例如:
Dim binaryArray As Byte ()
' 从PictureBox和GetBinary获取图像(使用原始格式)
binaryArray = GetBinary(PictureBox1.Image, Nothing )
' 从文件加载图片GetBinary(格式化为png)
binaryArray = GetBinary(Image.FromFile(sourceImageFileName),ImageFormat.Png)
' 将当前binaryArray保存到映像文件:
System.IO.File.WriteAllBytes(destinationImageFileName,binaryArray)
您可以在上面的搜索框中搜索文章。像将图像文件转换为字节和背面 [ ^ ],for示例
Hello How can i convert an image into binary array ??
解决方案
If you use System.Drawing, you can try this code
Imports System.Drawing Imports System.Drawing.Imaging Public Shared Function GetBinary(ByVal image As Image, ByVal format As ImageFormat) As Byte() Using ms As New System.IO.MemoryStream If (format Is Nothing) Then format = image.RawFormat ' use image original format End If image.Save(ms, format) Return ms.ToArray() End Using End Function
Example:
Dim binaryArray As Byte() ' Get image from PictureBox and GetBinary (use original format) binaryArray = GetBinary(PictureBox1.Image, Nothing) ' load image from file and GetBinary (formatted to png) binaryArray = GetBinary(Image.FromFile(sourceImageFileName), ImageFormat.Png) ' To save current binaryArray to image file: System.IO.File.WriteAllBytes(destinationImageFileName, binaryArray)
You can search for articles in the search box above. Like Convert Image File to Bytes and Back [^], for example.
这篇关于将图像转换为二进制数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文