在PictureBox中显示来自HTTP响应的图像(image/jpeg)



所以我需要向一个PHP页面发送一个请求,该页面返回一个无法使用URL直接读取的图像(当然可以,但从技术上讲,它只包含图像数据,而不是以.jpg结尾的页面)。。。

因此,我需要从响应中读取图像(它返回页面给我的Content-Type: image/jpeg,它直接显示图像数据(就像你在记事本中打开一个由符号组成的图像一样……)

如何将此图像数据转换为有效图像?

我尝试过使用响应中的字节将它们转换为图像;使用这个:

    private Bitmap ByteToImage(byte[] blob)
    {
        MemoryStream mStream = new MemoryStream();
        byte[] pData = blob;
        mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
        Bitmap bm = new Bitmap(mStream, false);
        mStream.Dispose();
        return bm;
    }

byte[] imageBytes = Encoding.UTF8.GetBytes(post(imagelink, ""));

但这只给了我一个论点例外。

您可以使用WebClient、HttpWebRequest或HttpClient来下载图像

using (WebClient wc = new WebClient())
{
    var img = ByteToImage(wc.DownloadData(url));
}

最新更新