【问题标题】:How to convert memory stream to string array and vice versa如何将内存流转换为字符串数组,反之亦然
【发布时间】:2014-01-19 11:23:13
【问题描述】:

我有一个代码,我想从图像中获取流并将内存流转换为字符串数组并存储在变量中。但我的问题是我也想从字符串变量中获取图像并在图片框上绘制。

如果我这样使用 PictureBox1.Image = image.FromStream(memoryStream) 我可以在图片框上打印图片。但这不是我的需要。我只想从文件中获取图像流并将流转换为文本并将其存储到某个字符串变量中,我想再次使用字符串变量并将其转换为流以在图片框上打印图像。

这是我的代码。(Vb Express 2008)

 Public Function ImageConversion(ByVal image As System.Drawing.Image) As String
        If image Is Nothing Then Return ""

        Dim memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream
        image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Gif)

        Dim value As String = ""


        For intCnt As Integer = 0 To memoryStream.ToArray.Length - 1
            value = value & memoryStream.ToArray(intCnt) & "  "
        Next

        Dim strAsBytes() As Byte = New System.Text.UTF8Encoding().GetBytes(value)
        Dim ms As New System.IO.MemoryStream(strAsBytes)


        PictureBox1.Image = image.FromStream(ms)

        Return value
    End Function

【问题讨论】:

  • 我只是好奇-为什么需要将图像转换/加载到内存流才能在图片框中显示它,而不能直接将图像保存到文件系统然后使用标准方法加载它?
  • 感谢您的回复。但我知道标准方法。我只想在富文本框中加载字符串变量。我得到像“71 70 56 255 240 15 ....”(取决于图片)rgb值的地方,我想手动更改值只是为了实验目的并重新打包到图像中,我想看看变化. :)

标签: vb.net memorystream


【解决方案1】:

这不会以您发布它的方式工作(至少是重新创建图像的一部分)。 看到这个:

Public Function ImageConversion(ByVal image As System.Drawing.Image) As String
    If image Is Nothing Then Return ""

    Dim memoryStream As System.IO.MemoryStream = New System.IO.MemoryStream
    image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Gif)

    Dim value As String = ""

    Dim content As Byte() = memoryStream.ToArray()
    ' instead of repeatingly call memoryStream.ToArray by using
    ' memoryStream.ToArray(intCnt)
    For intCnt As Integer = 0 To content.Length - 1
        value = value & content(intCnt) & "  "
    Next
    value = value.TrimEnd()

    Return value
End Function

要使用创建的字符串重新创建图像,您不能像以前那样使用 Encoding.GetBytes(),因为您会得到一个代表字符串的字节数组。例如 "123 32 123" 你不会得到一个包含元素 123、32、123 的字节数组

Public Function ImageConversion(ByVal stringRepOfImage As String) As System.Drawing.Image
    Dim stringBytes As String() = stringRepOfImage.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
    Dim bytes As New List(Of Byte)
    For intCount = 0 To stringBytes.Length - 1
        Dim b As Byte
        If Byte.TryParse(stringBytes(intCount), b) Then
            bytes.Add(b)
        Else
            Throw new FormatException("Not a byte value")
        End If
    Next
    Dim ms As New System.IO.MemoryStream(bytes.ToArray)
    Return Image.FromStream(ms)
End Function

参考:Byte.TryParse

【讨论】:

  • 对不起,这部分我看不懂,你能解释一下吗? ' Dim b As Byte If Byte.TryParse(stringBytes(intCount), b) Then bytes.Add(b) End If'
  • 假设您已将 Richtextbox 中的值更改为可以t be converted to a byte because e.g the value is 1234 so Byte.TryParse tries to parse the value to a byte and returns true if succeeding and false otherwise. Maybe it would be better to throw an exception because it wouldnt 生成有效图像的值。
  • 现在我明白了,当我从 rtf 更改一个值并将 rtf 值传递给它抛出异常的函数时。
  • 不,因此你需要一个 else 语句。 Byte.TryParse 尝试将字符串值解析为字节值。如果这是不可能的,因为它不能被解析,你应该抛出一个异常。我已经更新了代码。
  • 谢谢你。但是你能在转换为字符串并显示为 rtf 后告诉我吗,我的字符串看起来像“71 73 70 56 57 97 125 0 127 0 247 0 0 .....”但是当我将值更改为 71 到 97 之间时,它会抛出一个例外。但是在 97 之后,如果我更改值 125 以使图片在图片框上很好地显示。我不知道这是一个 gif 文件结构,我在不知道格式的情况下进行了更改。虽然有点离题,但你能解释一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
相关资源
最近更新 更多