如何转换图像 [英] How to I convert image
本文介绍了如何转换图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我没有一点长的错误我知道
我尝试过:
i dont some lenth error i dint understood
What I have tried:
Imports System.IO
Imports System.Text
Public Class Form1
Dim path As String = Directory.GetCurrentDirectory()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
''First i choose a img to convert to byte
Dim img As Image = Image.FromFile("C:\Users\admin\Desktop\1550157567366_1-xlg400x400.jpg")
''Second i convertexd the image to byte
Dim bArr As Byte() = imgToByteArray(img)
''third i converted byte to string
Dim istring As String = Bytes_To_String2(bArr)
'just exported to a text file in same path folder
'----------------------------------
ExportTextFile(istring)
' Dim bArr2 As Byte() = stringToByteArray(istring)
'----------------------------------
'Step four back to back from string to byte
Dim bArr2 As Byte() = ToByteArray(istring)
'step five bytes to img last stage is confusion
Dim img1 As Image = byteArrayToImage(bArr2)
'dint no see any picture in the picturebox1
PictureBox1.Image = img1
End Sub
Public Function imgToByteArray(ByVal img As Image) As Byte()
'Fisrt step
Using mStream As New MemoryStream()
img.Save(mStream, img.RawFormat)
Return mStream.ToArray()
End Using
End Function
Private Function Bytes_To_String2(ByVal bytes_Input As Byte()) As String
'Second Step conversion of byte to sring
Dim strTemp As New StringBuilder(bytes_Input.Length * 2)
For Each b As Byte In bytes_Input
strTemp.Append(Conversion.Hex(b))
Next
MsgBox(strTemp.ToString())
Return strTemp.ToString()
End Function
Sub ExportTextFile(ByVal revokedlinks As String)
'thrid Step Convesion of string to text file
Dim RevokedTextFile As String = path & "\StringToTextFile.txt"
If File.Exists(RevokedTextFile) = True Then
Dim appendText As String = revokedlinks
File.AppendAllText(RevokedTextFile, appendText)
Dim readText As String = File.ReadAllText(RevokedTextFile)
End If
If File.Exists(RevokedTextFile) = False Then
Dim createText As String = revokedlinks
File.WriteAllText(RevokedTextFile, createText)
End If
End Sub
Public Function ToByteArray(ByVal someString As String) As Byte()
'step four
Dim byteList As New List(Of Byte)
Try
Dim tempString As String = someString.Replace("&H", "")
If tempString.Length Mod 2 = 1 Then tempString = "0" & tempString
For index As Integer = 0 To tempString.Length Step 2
byteList.Add(Convert.ToByte(Val("&H" & tempString.Substring(index, 2))))
Next
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Return byteList.ToArray
End Function
'convert bytearray to image
Public Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
'step 5 dint work
Using mStream As New MemoryStream(byteArrayIn)
Return Image.FromStream(mStream)
End Using
End Function
End Class
推荐答案
dint work不是一个有用的错误报告,甚至没有一点点。
但这种方法不起作用。
问题 - 或者至少其中一个问题 - 是Conversion.Hex没有返回相同数量的数字每次x值:如果值低于16,则返回一位数,如果它在上面则返回两位 - 你无法告诉你的ToByteArray
方法。因此,如果您有两个4彼此相邻,则将其作为单个字节44处理,这意味着您从文件中读回的数据长度与原始数据不同。
零重要!
而不是像这样做 - 这根本不是一个好的解决方案 - 考虑使用Base64数据存储您的图像 - 它是文本,但比您的文本更紧凑,并且它不像您那样丢弃数据。
"dint work" is not a helpful error report, not even slightly.
But that approach will not work.
The problem - or at least one of the problems - is that Conversion.Hex does not return the same number of digits of hex value each time: if the value is below 16, it returns one digit, if it's above it returns two - and you can't tell in yourToByteArray
method which it was. So if you have two "4"s next to each other, you will process it as a single byte "44" which means that the length of the data you read back from your file is not the same as the original.
Zeros are important!
Instead of faffing about like this - which isn't a good solution at all - consider using Base64 data to store your you image - it's text, but more compact than yours, and it doesn't discard data like yours does.
如何转换回图像?
How to convert back to image ?
Imports System.IO
Public Class Form1
Dim path As String = Directory.GetCurrentDirectory()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Dim img As Image = Image.FromFile("C:\Users\admin\Desktop\1550157567366_1-xlg400x400.jpg")
Dim istring As String = ConvertFileToBase64("C:\Users\admin\Desktop\1550157567366_1-xlg400x400.jpg")
ExportTextFile(istring)
End Sub
Public Function ConvertFileToBase64(ByVal fileName As String) As String
Dim ReturnValue As String = ""
If My.Computer.FileSystem.FileExists(fileName) Then
Using BinaryFile As FileStream = New FileStream(fileName, FileMode.Open)
Dim BinRead As BinaryReader = New BinaryReader(BinaryFile)
Dim BinBytes As Byte() = BinRead.ReadBytes(CInt(BinaryFile.Length))
ReturnValue = Convert.ToBase64String(BinBytes)
BinaryFile.Close()
End Using
End If
Return ReturnValue
End Function
Sub ExportTextFile(ByVal revokedlinks As String)
'thrid Step Convesion of string to text file
Dim RevokedTextFile As String = Path & "\StringToTextFile.txt"
If File.Exists(RevokedTextFile) = True Then
Dim appendText As String = revokedlinks
File.AppendAllText(RevokedTextFile, appendText)
Dim readText As String = File.ReadAllText(RevokedTextFile)
End If
If File.Exists(RevokedTextFile) = False Then
Dim createText As String = revokedlinks
File.WriteAllText(RevokedTextFile, createText)
End If
End Sub
End Class
最后它工作
finally it work
Imports System.IO
Public Class Form1
Dim path As String = Directory.GetCurrentDirectory()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Dim img As Image = Image.FromFile("C:\Users\admin\Desktop\1550157567366_1-xlg400x400.jpg")
Dim istring As String = ConvertFileToBase64("C:\Users\admin\Desktop\1550157567366_1-xlg400x400.jpg")
ExportTextFile(istring)
Dim b As Byte() = Convert.FromBase64String(istring)
Dim img1 As Image = byteArrayToImage(b)
PictureBox1.Image = img1
End Sub
Public Function byteArrayToImage(ByVal byteArrayIn As Byte()) As Image
'step 5 dint work
Using mStream As New MemoryStream(byteArrayIn)
Return Image.FromStream(mStream)
End Using
End Function
Public Function ConvertFileToBase64(ByVal fileName As String) As String
Dim ReturnValue As String = ""
If My.Computer.FileSystem.FileExists(fileName) Then
Using BinaryFile As FileStream = New FileStream(fileName, FileMode.Open)
Dim BinRead As BinaryReader = New BinaryReader(BinaryFile)
Dim BinBytes As Byte() = BinRead.ReadBytes(CInt(BinaryFile.Length))
ReturnValue = Convert.ToBase64String(BinBytes)
BinaryFile.Close()
End Using
End If
Return ReturnValue
End Function
Sub ExportTextFile(ByVal revokedlinks As String)
'thrid Step Convesion of string to text file
Dim RevokedTextFile As String = Path & "\StringToTextFile.txt"
If File.Exists(RevokedTextFile) = True Then
Dim appendText As String = revokedlinks
File.AppendAllText(RevokedTextFile, appendText)
Dim readText As String = File.ReadAllText(RevokedTextFile)
End If
If File.Exists(RevokedTextFile) = False Then
Dim createText As String = revokedlinks
File.WriteAllText(RevokedTextFile, createText)
End If
End Sub
End Class
这篇关于如何转换图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文