将字符串转换为流 [英] Convert a String to Stream

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

问题描述

我已从Internet下载图像并将其转换为字符串(这是不可更改的)

I've download an image from the Internet and converted to a String (This is not changeable)

Dim Request As System.Net.WebRequest = _
  System.Net.WebRequest.Create( _
  "http://www.google.com/images/nav_logo.png")

Dim WebResponse As System.Net.HttpWebResponse = _
  DirectCast(Request.GetResponse(), System.Net.HttpWebResponse)

Dim Stream As New System.IO.StreamReader( _
  WebResponse.GetResponseStream, System.Text.Encoding.UTF8)

Dim Text as String = Stream.ReadToEnd

如何将字符串转换回流?

How can I convert the String back to the Stream?

因此,我可以使用该流来获取图像。

So I can use that stream to get the image.

就像这样:

Dim Image As New Drawing.Bitmap(WebResponse.GetResponseStream)

但是现在我只有文本字符串,所以我需要这样的东西:

But now I've only the Text String, so I need something like this:

Dim Stream as Stream = ReadToStream(Text, System.Text.Encoding.UTF8)
Dim Image As New Drawing.Bitmap(Stream)

编辑:

此引擎主要用于下载网页,但我也尝试使用它来下载图片。
字符串的格式为UTF8,如示例代码中所示...

This engine was primarily used for downloading web pages but I'm trying to use it for downloading images too. The format of the string is UTF8, as given in the example code...

我尝试使用 MemoryStream (Encoding.UTF8.GetBytes(Text)),但是在将流加载到图像时出现此错误:

I've tried to use the MemoryStream(Encoding.UTF8.GetBytes(Text)), but I got this error when loading the stream to the image:


GDI +中发生了一般性错误。

A generic error occurred in GDI+.

转换中丢失了什么?

推荐答案

为什么要将二进制(图像)数据转换为字符串?除非您使用的是base-64,否则这没有任何意义。

Why have you converted binary (image) data to a string? This makes no sense... unless you are using base-64?

无论如何,要撤消所做的工作,您可以尝试使用 new MemoryStream(Encoding.UTF8.GetBytes(text))

Anyway, to reverse what you have done, you could try using new MemoryStream(Encoding.UTF8.GetBytes(text))?

这将创建一个新的以字符串开头的MemoryStream(通过UTF8)。就我个人而言,我怀疑它是否会起作用-您将遇到很多将原始二进制文件视为UTF8数据的编码问题...我希望读取或写入(或两者)都抛出异常。

This will create a new MemoryStream primed with the string (via UTF8). Personally, I doubt it will work - you are going to run into a lot of encoding issues treating raw binary as UTF8 data... I expect either the read or write (or both) to throw an exception.

(编辑)

我应该添加它以使用base-64,只需将数据作为 byte [] ,然后调用 Convert.ToBase64String(...);并返回数组,只需使用 Convert.FromBase64String(...)

I should add that to work with base-64, simply get the data as a byte[], then call Convert.ToBase64String(...); and to get back the array, just use Convert.FromBase64String(...).

重新进行编辑,这正是我在上方警告的内容...在.NET中,字符串不仅是 byte [] ,因此您不能简单地用二进制图像数据填充它。很多数据根本就没有意义,因此可能会被悄悄删除(或引发异常)。

Re your edit, this is precisely what I tried to warn about above... in .NET, a string is not just a byte[], so you can't simply fill it with binary image data. A lot of the data simply won't make sense to the encoding, so might be quietly dropped (or an exception thrown).

处理原始二进制文件(例如图像) )作为字符串,您需要使用base-64编码;但是,这增加了尺寸。请注意, WebClient 可能会使其更简单,因为它直接公开了 byte [] 功能:

To handle raw binary (such as images) as strings, you need to use base-64 encoding; this adds size, however. Note that WebClient might make this simpler, as it exposes byte[] functionality directly:

using(WebClient wc = new WebClient()) {
    byte[] raw = wc.DownloadData("http://www.google.com/images/nav_logo.png")
    //...
}

无论如何,使用标准的 Stream 方法,以下是对base-64进行编码和解码的方法:

Anyway, using a standard Stream approach, here's how to encode and decode the base-64:

        // ENCODE
        // where "s" is our original stream
        string base64;
        // first I need the data as a byte[]; I'll use
        // MemoryStream, as a convenience; if you already
        // have the byte[] you can skip this
        using (MemoryStream ms = new MemoryStream())
        {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = s.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, bytesRead);
            }
            base64 = Convert.ToBase64String(ms.GetBuffer(), 0, (int) ms.Length);
        }

        // DECODE
        byte[] raw = Convert.FromBase64String(base64);
        using (MemoryStream decoded = new MemoryStream(raw))
        {
            // "decoded" now primed with the binary
        }

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

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