我试图加载一个图像作为一个基本的64字符串,以便我可以在像这样的HTML中显示它:
<html><body><img src="data:image/jpeg;base64,/></img></body></html>继到目前为止我的代码,但它并没有真正起作用:
public async static Task<string> getImage(string url) { var request = (HttpWebRequest)WebRequest.Create(url); request.Accept = "data:image/jpg;charset=base64"; request.Credentials = new NetworkCredential(user, pw); using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null))) { StreamReader sr = new StreamReader(response.GetResponseStream()); return sr.ReadToEnd(); }我尝试使用这种方法,我发现在其他地方编码返回字符串为base64,但是当它放在一个HTML中的图像只显示了典型的占位符。
public static string Base64Encode(string plainText) { var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); return System.Convert.ToBase64String(plainTextBytes); }编辑:
以下是html的外观:
I am trying to load an image as a base 64 string so that i can show it in a html like this:
<html><body><img src="data:image/jpeg;base64,/></img></body></html>Heres my code so far, but it does not really work:
public async static Task<string> getImage(string url) { var request = (HttpWebRequest)WebRequest.Create(url); request.Accept = "data:image/jpg;charset=base64"; request.Credentials = new NetworkCredential(user, pw); using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null))) { StreamReader sr = new StreamReader(response.GetResponseStream()); return sr.ReadToEnd(); }I tried using this method i found elsewhere to encode the return-String as base64, but when placing it in a html the image just shows the typical placeholder.
public static string Base64Encode(string plainText) { var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText); return System.Convert.ToBase64String(plainTextBytes); }EDIT:
Here is how the html looks:
最满意答案
在我看来,您需要将base64部分(仅在您的HTML中需要)与获取响应中的数据分开。 只需从URL中获取数据作为二进制数据并将其转换为base64。 使用HttpClient使这很简单:
public async static Task<string> GetImageAsBase64Url(string url) { var credentials = new NetworkCredential(user, pw); using (var handler = new HttpClientHandler { Credentials = credentials }) using (var client = new HttpClient(handler)) { var bytes = await client.GetByteArrayAsync(url); return "image/jpeg;base64," + Convert.ToBase64String(bytes); } }这假设图像始终是JPEG。 如果它有时可能是不同的内容类型,您可能希望将响应作为HttpResponse获取并使用它来传播内容类型。
我怀疑你可能也希望在这里添加缓存:)
It seems to me that you need to separate the base64 part, which is only needed in your HTML, from fetching the data from the response. Just fetch the data from the URL as binary data and convert that to base64. Using HttpClient makes this simple:
public async static Task<string> GetImageAsBase64Url(string url) { var credentials = new NetworkCredential(user, pw); using (var handler = new HttpClientHandler { Credentials = credentials }) using (var client = new HttpClient(handler)) { var bytes = await client.GetByteArrayAsync(url); return "image/jpeg;base64," + Convert.ToBase64String(bytes); } }This assumes the image always will be a JPEG. If it could sometimes be a different content type, you may well want to fetch the response as an HttpResponse and use that to propagate the content type.
I suspect you may want to add caching here as well :)
更多推荐
base,request,string,image,var,电脑培训,计算机培训,IT培训"/> <meta name="
发布评论