Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC提供运行时映像,而不是将其保存在磁盘上到浏览器_C#_Asp.net_Asp.net Mvc - Fatal编程技术网

C# MVC提供运行时映像,而不是将其保存在磁盘上到浏览器

C# MVC提供运行时映像,而不是将其保存在磁盘上到浏览器,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我的MVC应用程序在运行时创建图像,我不需要将其保存在磁盘上 将这些信息发送到请求浏览器的最佳方式是什么? 请注意,映像永远不会相同,因此没有理由先将它们保存在磁盘上 服务器将绘制随机图像,并将图像返回给调用客户端。我试图了解这种类型操作的最佳格式(位图、图像…),以便流回到服务器时尽可能平滑和快速。如果您可以获得位图的字节,那么很容易将其返回到客户端 public ActionResult GetImage() { byte[] byteArray = MagicMethodToGet

我的MVC应用程序在运行时创建图像,我不需要将其保存在磁盘上

将这些信息发送到请求浏览器的最佳方式是什么?
请注意,映像永远不会相同,因此没有理由先将它们保存在磁盘上


服务器将绘制随机图像,并将图像返回给调用客户端。我试图了解这种类型操作的最佳格式(位图、图像…),以便流回到服务器时尽可能平滑和快速。

如果您可以获得位图的字节,那么很容易将其返回到客户端

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    return new FileContentResult(byteArray, "image/jpeg");
}
此外,如果您希望返回图像和一些数据,可以将字节编码为base64,并将其封装在JSON中,如下所示:

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    var results = new 
    {
        Image = Convert.ToBase64String(byteArray),
        OtherData = "some data"
    };

    return Json(results);
}
private FileContentResult getFileContentResult(string name, bool download = true)
{
    if (!string.IsNullOrEmpty(name))
    {
        // don't forget to set the appropriate image MIME type 
        var result = new FileContentResult(System.IO.File.ReadAllBytes(name), "image/png");
        if (download)
        {
            result.FileDownloadName = Server.UrlEncode(name);
        }
        return result;
    }
    return null;
}
public ActionResult GetImage(string name)
{
    return getFileContentResult(name, true);
    // or use the image directly for example in a HTML img tag
    // return getFileContentResult(name);
}

如果您可以获取位图的字节数,那么很容易将其返回到客户端

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    return new FileContentResult(byteArray, "image/jpeg");
}
此外,如果您希望返回图像和一些数据,可以将字节编码为base64,并将其封装在JSON中,如下所示:

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    var results = new 
    {
        Image = Convert.ToBase64String(byteArray),
        OtherData = "some data"
    };

    return Json(results);
}
private FileContentResult getFileContentResult(string name, bool download = true)
{
    if (!string.IsNullOrEmpty(name))
    {
        // don't forget to set the appropriate image MIME type 
        var result = new FileContentResult(System.IO.File.ReadAllBytes(name), "image/png");
        if (download)
        {
            result.FileDownloadName = Server.UrlEncode(name);
        }
        return result;
    }
    return null;
}
public ActionResult GetImage(string name)
{
    return getFileContentResult(name, true);
    // or use the image directly for example in a HTML img tag
    // return getFileContentResult(name);
}

如果您可以获取位图的字节数,那么很容易将其返回到客户端

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    return new FileContentResult(byteArray, "image/jpeg");
}
此外,如果您希望返回图像和一些数据,可以将字节编码为base64,并将其封装在JSON中,如下所示:

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    var results = new 
    {
        Image = Convert.ToBase64String(byteArray),
        OtherData = "some data"
    };

    return Json(results);
}
private FileContentResult getFileContentResult(string name, bool download = true)
{
    if (!string.IsNullOrEmpty(name))
    {
        // don't forget to set the appropriate image MIME type 
        var result = new FileContentResult(System.IO.File.ReadAllBytes(name), "image/png");
        if (download)
        {
            result.FileDownloadName = Server.UrlEncode(name);
        }
        return result;
    }
    return null;
}
public ActionResult GetImage(string name)
{
    return getFileContentResult(name, true);
    // or use the image directly for example in a HTML img tag
    // return getFileContentResult(name);
}

如果您可以获取位图的字节数,那么很容易将其返回到客户端

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    return new FileContentResult(byteArray, "image/jpeg");
}
此外,如果您希望返回图像和一些数据,可以将字节编码为base64,并将其封装在JSON中,如下所示:

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    var results = new 
    {
        Image = Convert.ToBase64String(byteArray),
        OtherData = "some data"
    };

    return Json(results);
}
private FileContentResult getFileContentResult(string name, bool download = true)
{
    if (!string.IsNullOrEmpty(name))
    {
        // don't forget to set the appropriate image MIME type 
        var result = new FileContentResult(System.IO.File.ReadAllBytes(name), "image/png");
        if (download)
        {
            result.FileDownloadName = Server.UrlEncode(name);
        }
        return result;
    }
    return null;
}
public ActionResult GetImage(string name)
{
    return getFileContentResult(name, true);
    // or use the image directly for example in a HTML img tag
    // return getFileContentResult(name);
}

一种可能是使用读取文件内容并直接显示或提供下载。解决方案可能如下所示:

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    var results = new 
    {
        Image = Convert.ToBase64String(byteArray),
        OtherData = "some data"
    };

    return Json(results);
}
private FileContentResult getFileContentResult(string name, bool download = true)
{
    if (!string.IsNullOrEmpty(name))
    {
        // don't forget to set the appropriate image MIME type 
        var result = new FileContentResult(System.IO.File.ReadAllBytes(name), "image/png");
        if (download)
        {
            result.FileDownloadName = Server.UrlEncode(name);
        }
        return result;
    }
    return null;
}
public ActionResult GetImage(string name)
{
    return getFileContentResult(name, true);
    // or use the image directly for example in a HTML img tag
    // return getFileContentResult(name);
}
用户可以在以下操作中使用此方法:

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    var results = new 
    {
        Image = Convert.ToBase64String(byteArray),
        OtherData = "some data"
    };

    return Json(results);
}
private FileContentResult getFileContentResult(string name, bool download = true)
{
    if (!string.IsNullOrEmpty(name))
    {
        // don't forget to set the appropriate image MIME type 
        var result = new FileContentResult(System.IO.File.ReadAllBytes(name), "image/png");
        if (download)
        {
            result.FileDownloadName = Server.UrlEncode(name);
        }
        return result;
    }
    return null;
}
public ActionResult GetImage(string name)
{
    return getFileContentResult(name, true);
    // or use the image directly for example in a HTML img tag
    // return getFileContentResult(name);
}

这个类非常健壮和快速——我已经有了很好的经验,可以完全用于相同的目的。

一种可能是使用来阅读文件内容并直接显示或提供下载。解决方案可能如下所示:

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    var results = new 
    {
        Image = Convert.ToBase64String(byteArray),
        OtherData = "some data"
    };

    return Json(results);
}
private FileContentResult getFileContentResult(string name, bool download = true)
{
    if (!string.IsNullOrEmpty(name))
    {
        // don't forget to set the appropriate image MIME type 
        var result = new FileContentResult(System.IO.File.ReadAllBytes(name), "image/png");
        if (download)
        {
            result.FileDownloadName = Server.UrlEncode(name);
        }
        return result;
    }
    return null;
}
public ActionResult GetImage(string name)
{
    return getFileContentResult(name, true);
    // or use the image directly for example in a HTML img tag
    // return getFileContentResult(name);
}
用户可以在以下操作中使用此方法:

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    var results = new 
    {
        Image = Convert.ToBase64String(byteArray),
        OtherData = "some data"
    };

    return Json(results);
}
private FileContentResult getFileContentResult(string name, bool download = true)
{
    if (!string.IsNullOrEmpty(name))
    {
        // don't forget to set the appropriate image MIME type 
        var result = new FileContentResult(System.IO.File.ReadAllBytes(name), "image/png");
        if (download)
        {
            result.FileDownloadName = Server.UrlEncode(name);
        }
        return result;
    }
    return null;
}
public ActionResult GetImage(string name)
{
    return getFileContentResult(name, true);
    // or use the image directly for example in a HTML img tag
    // return getFileContentResult(name);
}

这个类非常健壮和快速——我已经有了很好的经验,可以完全用于相同的目的。

一种可能是使用来阅读文件内容并直接显示或提供下载。解决方案可能如下所示:

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    var results = new 
    {
        Image = Convert.ToBase64String(byteArray),
        OtherData = "some data"
    };

    return Json(results);
}
private FileContentResult getFileContentResult(string name, bool download = true)
{
    if (!string.IsNullOrEmpty(name))
    {
        // don't forget to set the appropriate image MIME type 
        var result = new FileContentResult(System.IO.File.ReadAllBytes(name), "image/png");
        if (download)
        {
            result.FileDownloadName = Server.UrlEncode(name);
        }
        return result;
    }
    return null;
}
public ActionResult GetImage(string name)
{
    return getFileContentResult(name, true);
    // or use the image directly for example in a HTML img tag
    // return getFileContentResult(name);
}
用户可以在以下操作中使用此方法:

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    var results = new 
    {
        Image = Convert.ToBase64String(byteArray),
        OtherData = "some data"
    };

    return Json(results);
}
private FileContentResult getFileContentResult(string name, bool download = true)
{
    if (!string.IsNullOrEmpty(name))
    {
        // don't forget to set the appropriate image MIME type 
        var result = new FileContentResult(System.IO.File.ReadAllBytes(name), "image/png");
        if (download)
        {
            result.FileDownloadName = Server.UrlEncode(name);
        }
        return result;
    }
    return null;
}
public ActionResult GetImage(string name)
{
    return getFileContentResult(name, true);
    // or use the image directly for example in a HTML img tag
    // return getFileContentResult(name);
}

这个类非常健壮和快速——我已经有了很好的经验,可以完全用于相同的目的。

一种可能是使用来阅读文件内容并直接显示或提供下载。解决方案可能如下所示:

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    var results = new 
    {
        Image = Convert.ToBase64String(byteArray),
        OtherData = "some data"
    };

    return Json(results);
}
private FileContentResult getFileContentResult(string name, bool download = true)
{
    if (!string.IsNullOrEmpty(name))
    {
        // don't forget to set the appropriate image MIME type 
        var result = new FileContentResult(System.IO.File.ReadAllBytes(name), "image/png");
        if (download)
        {
            result.FileDownloadName = Server.UrlEncode(name);
        }
        return result;
    }
    return null;
}
public ActionResult GetImage(string name)
{
    return getFileContentResult(name, true);
    // or use the image directly for example in a HTML img tag
    // return getFileContentResult(name);
}
用户可以在以下操作中使用此方法:

public ActionResult GetImage()
{
    byte[] byteArray = MagicMethodToGetImageData();
    var results = new 
    {
        Image = Convert.ToBase64String(byteArray),
        OtherData = "some data"
    };

    return Json(results);
}
private FileContentResult getFileContentResult(string name, bool download = true)
{
    if (!string.IsNullOrEmpty(name))
    {
        // don't forget to set the appropriate image MIME type 
        var result = new FileContentResult(System.IO.File.ReadAllBytes(name), "image/png");
        if (download)
        {
            result.FileDownloadName = Server.UrlEncode(name);
        }
        return result;
    }
    return null;
}
public ActionResult GetImage(string name)
{
    return getFileContentResult(name, true);
    // or use the image directly for example in a HTML img tag
    // return getFileContentResult(name);
}

这个类非常健壮和快速——我已经有了很好的经验,使用它的目的完全相同。

如果您在创建它的同一页面中发送它们,那么您可以修改您的响应以直接发送它

//After having your bitmap created...

MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.PNG);
bmp.Dispose();
byte[] data = ms.ToArray();
Response.ContentType = "image/png";
Response.ContentLength = data.Length;

using(var str = Response.GetResponseStream())
    str.Write(data, 0, data.Length);

Response.End();

如果在创建它们的同一页面中发送它们,则可以修改响应以直接发送

//After having your bitmap created...

MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.PNG);
bmp.Dispose();
byte[] data = ms.ToArray();
Response.ContentType = "image/png";
Response.ContentLength = data.Length;

using(var str = Response.GetResponseStream())
    str.Write(data, 0, data.Length);

Response.End();

如果在创建它们的同一页面中发送它们,则可以修改响应以直接发送

//After having your bitmap created...

MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.PNG);
bmp.Dispose();
byte[] data = ms.ToArray();
Response.ContentType = "image/png";
Response.ContentLength = data.Length;

using(var str = Response.GetResponseStream())
    str.Write(data, 0, data.Length);

Response.End();

如果在创建它们的同一页面中发送它们,则可以修改响应以直接发送

//After having your bitmap created...

MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.PNG);
bmp.Dispose();
byte[] data = ms.ToArray();
Response.ContentType = "image/png";
Response.ContentLength = data.Length;

using(var str = Response.GetResponseStream())
    str.Write(data, 0, data.Length);

Response.End();

它们是根据浏览器的请求生成的吗?一些代码会有帮助。太宽了。网站上有很多样品-挑一些试试看。常见于从数据库读取图像时…它们是否是浏览器请求的结果?一些代码会有帮助。太宽了。网站上有很多样品-挑一些试试看。常见于从数据库读取图像时…它们是否是浏览器请求的结果?一些代码会有帮助。太宽了。网站上有很多样品-挑一些试试看。常见于从数据库读取图像时…它们是否是浏览器请求的结果?一些代码会有帮助。太宽了。网站上有很多样品-挑一些试试看。常见于从db读取图像…我希望通过ViewBag或类似的方式发送图像,因为我有很多其他信息要发送回浏览器。如果我把bytes[]数据放在一个ViewBag中,你知道在浏览器img标签上显示该数据的代码吗?我本来希望在ViewBag或类似的文件上发送图像,因为我还有很多其他信息要发送回浏览器。如果我把bytes[]数据放在一个ViewBag中,你知道在浏览器img标签上显示该数据的代码吗?我本来希望在ViewBag或类似的文件上发送图像,因为我还有很多其他信息要发送回浏览器。如果我把bytes[]数据放在一个ViewBag中,你知道在浏览器img标签上显示该数据的代码吗?我本来希望在ViewBag或类似的文件上发送图像,因为我还有很多其他信息要发送回浏览器。如果我将bytes[]数据放在一个ViewBag中,您知道在浏览器img标签上显示该数据的代码吗?