Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

.net - Convert a String to Stream

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.

Like this:

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)

EDIT:

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...

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

A generic error occurred in GDI+.

What gets lost in the conversions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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.

(edit)

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(...).


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).

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")
    //...
}

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
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...