Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 将字符串转换为流_.net_Vb.net_Stream - Fatal编程技术网

.net 将字符串转换为流

.net 将字符串转换为流,.net,vb.net,stream,.net,Vb.net,Stream,我已经从Internet下载了一个图像并转换为字符串(这是不可更改的) 如何将字符串转换回流 所以我可以使用这个流来获取图像 像这样: Dim Image As New Drawing.Bitmap(WebResponse.GetResponseStream) 但现在我只有文本字符串,所以我需要这样的内容: Dim Stream as Stream = ReadToStream(Text, System.Text.Encoding.UTF8) Dim Image As New Drawing.

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

如何将字符串转换回流

所以我可以使用这个流来获取图像

像这样:

Dim Image As New Drawing.Bitmap(WebResponse.GetResponseStream)
但现在我只有文本字符串,所以我需要这样的内容:

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

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

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

GDI+中发生一般性错误


转换过程中会丢失什么?

为什么要将二进制(图像)数据转换为字符串?这毫无意义。。。除非您使用的是base-64

无论如何,要逆转您所做的,您可以尝试使用
newmemoryStream(Encoding.UTF8.GetBytes(text))

这将创建一个新的内存流,并使用字符串(通过UTF8)进行初始化。就个人而言,我怀疑它会起作用-您将遇到许多编码问题,将原始二进制文件视为UTF8数据。。。我希望读或写(或两者)都会抛出异常

(编辑)

我应该补充一下,要使用base-64,只需将数据作为
字节[]
,然后调用
Convert.ToBase64String(…)
;要返回数组,只需使用
Convert.FromBase64String(…)


重新编辑,这正是我试图在上面警告的。。。在.NET中,字符串不仅仅是一个
字节[]
,因此不能简单地用二进制图像数据填充它。许多数据对编码毫无意义,所以可能会被悄悄删除(或引发异常)

要将原始二进制(如图像)作为字符串处理,需要使用base-64编码;然而,这增加了规模。请注意,
WebClient
可能会简化此操作,因为它直接公开了
byte[]
功能:

using(WebClient wc = new WebClient()) {
    byte[] raw = wc.DownloadData("http://www.google.com/images/nav_logo.png")
    //...
}
无论如何,使用标准的
方法,下面介绍如何对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
        }

这样行吗?我不知道你的字符串是什么格式的,所以一些按摩可能是必要的

Dim strAsBytes() as Byte = new System.Text.UTF8Encoding().GetBytes(Text)
Dim ms as New System.IO.MemoryStream(strAsBytes)

将二进制数据按所示方式转换为字符串将使其变得无用。你不能把它拿出来。文本编码将其隐藏


您需要使用Base64类节目。

我将重新编辑您的节目
Dim strAsBytes() as Byte = new System.Text.UTF8Encoding().GetBytes(Text)
Dim ms as New System.IO.MemoryStream(strAsBytes)
var bytes = new byte[contents.Length * sizeof( char )];
Buffer.BlockCopy( contents.ToCharArray(), 0, bytes, 0, bytes.Length );
using( var stream = new MemoryStream( bytes ) )
{
    // do your stuff with the stream...
}