我想做的是将图片从服务器发送到客户端(Web浏览器)。因此,当我在浏览器中打开链接时,例如https://localhost:8080/geoserver/(将端口开头设置为8080),它将显示消息“ hello world”,这很好,但是现在我尝试使用StreamWriter和我所得到的只是诸如System.Drawing.Bitmap
之类的文本,并且浏览器中没有显示图片。我正在使用c#控制台应用程序。
我的代码:
static void Main(string[] args)
{
HttpListener listen = new HttpListener();
string url = "http://localhost";
string port = "";
Console.Write("Nastavite port:");
port = Console.ReadLine();
url = url + ":" + port + "/geoserver/";
listen.Prefixes.Add(url);
listen.Start();
while (true)
{
Console.WriteLine("Cakam...");
HttpListenerContext kontekst = listen.GetContext();
string msg = "hello world";
kontekst.Response.ContentLength64 = Encoding.UTF8.GetByteCount(msg);
kontekst.Response.StatusCode = (int)HttpStatusCode.OK;
using(Stream stream = kontekst.Response.OutputStream)
{
using(StreamWriter writer = new StreamWriter(stream))
{
writer.Write(msg);
}
}
Console.WriteLine("Sporočilo poslano");
}
}
答案 0 :(得分:0)
使代码正常工作所需的最小更改是使用Save方法将图像写入流中,而不是Writer.Write()
,它将调用对象的ToString()
(如果(还不是字符数组)导致发送您遇到的类名。
//Can't set the response length upfront, if you really need to set it you need to
//calculate it from the size of the image.
//kontekst.Response.ContentLength64 = Encoding.UTF8.GetByteCount(msg);
//Most browsers figure it out without this, but good practice to set the type:
kontekst.Response.ContentType = "image/bmp";
kontekst.Response.StatusCode = (int)HttpStatusCode.OK;
var img = Image.FromFile(@"some.bmp");
using (Stream stream = kontekst.Response.OutputStream)
{
img.Save(stream, ImageFormat.Bmp);
}
我还将考虑更改其他一些内容:
如果不需要在服务器上编辑图像,请像我一样使用System.Drawing.Bitmap
来代替System.Drawing.Image
。甚至更好,如果您不需要在服务器端将其视为映像,只需使用FileStream
读入并将其写出到Output流。
您真的要在服务器上实现并处理低级别的HttpContext /侦听器吗?也许您可以将ASP.Net Core与Kestrel结合使用。
答案 1 :(得分:0)
您可以通过将图像转换为base64并将其显示在html img
标签中来实现。
1)使用System.Drawing
库将图片作为字节数组获取
Image image = Image.FromFile("test-img.jpg");
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] imgBytes = ms.ToArray();
2)需要将其转换为base64字符串后
string base64 = Convert.ToBase64String(imgBytes);
3)然后创建html响应文本
string html = $"<html><img src=\"data: image / png; base64, {base64} \"></html>";
4)现在您可以将此文本写到输出流中
Stream stream = kontekst.Response.OutputStream;
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine(html);
因此完整的工作代码如下:
static void Main(string[] args)
{
HttpListener listen = new HttpListener();
string url = "http://localhost";
string port = "";
Console.Write("Nastavite port:");
port = Console.ReadLine();
url = url + ":" + port + "/geoserver/";
listen.Prefixes.Add(url);
listen.Start();
while (true)
{
Console.WriteLine("Cakam...");
HttpListenerContext kontekst = listen.GetContext();
kontekst.Response.StatusCode = (int)HttpStatusCode.OK;
using (Stream stream = kontekst.Response.OutputStream)
using (Image image = Image.FromFile("test-img.jpg"))
using (MemoryStream ms = new MemoryStream())
using (StreamWriter writer = new StreamWriter(stream))
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
string base64 = Convert.ToBase64String(ms.ToArray());
writer.WriteLine($"<html><img src=\"data: image / png; base64, {base64} \"></html>");
}
Console.WriteLine("Sporočilo poslano");
}
}