编码问题?

发布于 2024-10-25 08:52:31 字数 807 浏览 0 评论 0原文

我想从网络上获取一张图片,并将其转换为字节数组。但阅读回复时发现有问题。我怀疑这是由编码模式引起的。

        WebRequest request = WebRequest.Create("http://www.waterfootprint.org/images/gallery/original/apple.jpg");
        request.Method = "GET";
        request.Timeout = 10000;
        using (WebResponse response = request.GetResponse())
        {
            Stream stream = response.GetResponseStream();
            Encoding encoding = Encoding.UTF8;
            StreamReader streamReader = new StreamReader(stream, encoding);

            string responseBody = streamReader.ReadToEnd(); //always invalid characters here
            streamReader.Close();
            stream.Dispose();
            byte[] buffer = Convert.FromBase64String(responseBody); 
        }

我尝试过其他编码方式,比如UTF7、Unicode等,但都是徒劳。有人能告诉我为什么吗?谢谢

I want to get a picture from web, and turn that into a byte array. But there is something wrong reading the response. I suspect it caused by encoding modes.

        WebRequest request = WebRequest.Create("http://www.waterfootprint.org/images/gallery/original/apple.jpg");
        request.Method = "GET";
        request.Timeout = 10000;
        using (WebResponse response = request.GetResponse())
        {
            Stream stream = response.GetResponseStream();
            Encoding encoding = Encoding.UTF8;
            StreamReader streamReader = new StreamReader(stream, encoding);

            string responseBody = streamReader.ReadToEnd(); //always invalid characters here
            streamReader.Close();
            stream.Dispose();
            byte[] buffer = Convert.FromBase64String(responseBody); 
        }

I have tried other encoding ways, such as UTF7, Unicode, etc, but all in vain. Could someone tell me why? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

GRAY°灰色天空 2024-11-01 08:52:31
    StreamReader streamReader = new StreamReader(stream, encoding);

StreamReader 是一个 < em>TextReader 以特定编码从字节流中读取字符。

StreamReader 是为字符设计的
以特定编码输入,
而 Stream 类的设计
用于字节输入和输出。使用
StreamReader 用于读取行
来自标准文本文件的信息。

在您的情况下,您收到原始字节 - 您需要直接使用 Stream、使用 BinaryReader 或更高级别的抽象。

        byte[] buffer = Convert.FromBase64String(responseBody); 

您获得的流不是 base64 编码的 - 它是纯图像字节流,因此只需直接分配字节,最简单的方法是使用 WebClient:

using(WebClient wc = new WebClient())
byte[] buffer = wc.DownloadData("http://www.waterfootprint.org/images/gallery/original/apple.jpg");

    StreamReader streamReader = new StreamReader(stream, encoding);

StreamReader is a TextReader that reads characters from a byte stream in a particular encoding.

StreamReader is designed for character
input in a particular encoding,
whereas the Stream class is designed
for byte input and output. Use
StreamReader for reading lines of
information from a standard text file.

In your case you receive raw bytes - You either need to work with the Stream directly, use a BinaryReader or a higher level abstraction.

        byte[] buffer = Convert.FromBase64String(responseBody); 

The stream you are getting is not base64 encoded - it's a pure image byte stream, so just assign the bytes directly, easiest would be with a WebClient:

using(WebClient wc = new WebClient())
byte[] buffer = wc.DownloadData("http://www.waterfootprint.org/images/gallery/original/apple.jpg");

Base 64 encoding is usually used when you must transmit binary data as ASCII text (i.e. as part of an XML CData element or SOAP in general) - but not if you want to transmit a binary file (i.e. an image) over HTTP.

卖梦商人 2024-11-01 08:52:31

它以二进制形式返回,而不是 base-64 编码。例如,如果我更改代码以直接从响应流创建图像,我会看到它出现在 PictureBox 控件中。

    private void button1_Click(object sender, EventArgs e)
    {
        WebRequest request = WebRequest.Create("http://www.waterfootprint.org/images/gallery/original/apple.jpg");
        request.Method = "GET";
        request.Timeout = 10000;
        using (WebResponse response = request.GetResponse())
        {
            Stream stream = response.GetResponseStream();
            Bitmap b = (Bitmap)Bitmap.FromStream(response.GetResponseStream());
            pictureBox1.Image = b;
        }
    }

It comes back as binary, not base-64 encoded. For example, if I change your code to create an image directly from the response stream, I see it appear in a PictureBox control.

    private void button1_Click(object sender, EventArgs e)
    {
        WebRequest request = WebRequest.Create("http://www.waterfootprint.org/images/gallery/original/apple.jpg");
        request.Method = "GET";
        request.Timeout = 10000;
        using (WebResponse response = request.GetResponse())
        {
            Stream stream = response.GetResponseStream();
            Bitmap b = (Bitmap)Bitmap.FromStream(response.GetResponseStream());
            pictureBox1.Image = b;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文