c# - 渲染没有 1-1 效果的图像

标签 c# asp.net-mvc asp.net-mvc-3

我有几个显示图片的页面,他们调用 Controller Action ,其代码显示在[art of post.问题是从粉碎中加载的图像,瞬间加载不出来而是先显示文本,然后显示图像,我尝试了 2 种更精确的加载图像方法,但效果相同......所以一个一个地加载, 请帮忙 当前代码操作

    public FileContentResult ImageVew(string sourcePath)
    {

        string location = (string)Session["TicketFilePath"] + sourcePath;



        byte[] myByte = System.IO.File.ReadAllBytes(location);

        return File(myByte, "image/jpeg");


        Image i;
        using (MemoryStream ms = new MemoryStream())
        {
            ms.Write(myByte, 0, myByte.Length);
            i = Image.FromStream(ms);
        }
        return File(imageToByteArray(i.GetThumbnailImage(100, 100, () => false, IntPtr.Zero)), "image/png");
    }

    public byte[] imageToByteArray(System.Drawing.Image imageIn)
    {
        MemoryStream ms = new MemoryStream();
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        return ms.ToArray();
    }

此方法的最新版本,但不是结果

    public ActionResult ImageVew(string sourcePath)
    {
        FileInfo fi = new FileInfo((string)Session["TicketFilePath"] + sourcePath);

        return File(fi.FullName, Utilities.MimeType(fi.Name));
    }

    public ActionResult ImageVew(string sourcePath)
    {
        FileInfo fi = new FileInfo((string)Session["TicketFilePath"] + sourcePath);

        byte[] imageFile = System.IO.File.ReadAllBytes(fi.FullName);
        return new FileContentResult(imageFile, Utilities.ImageType(fi.Name));


    }

    public ActionResult ImageVew(string sourcePath)
    {

        FileInfo fi = new FileInfo((string)Session["TicketFilePath"] + sourcePath);
        if (fi.Exists)
        {
            byte[] imageFile = System.IO.File.ReadAllBytes(fi.FullName);

            return File(imageFile, Utilities.ImageType(fi.Name));
        }
        else return null;
    }

图像渲染 1 对 1 并从网络路径加载图像...但是在 1. 加载图像兑现之后,立即加载 请开发人员帮助我

f部分代码然后调用action 带渲染控制

 private void WriteDataRow(Class item, HtmlTextWriter writer) 
    {
 writer.RenderBeginTag(HtmlTextWriterTag.Td);
        writer.RenderBeginTag(HtmlTextWriterTag.Center);
        writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "20px");
        string src = Url.Action("ImageVew", new { sourcePath = item.Status.Marker });
        writer.AddAttribute(HtmlTextWriterAttribute.Src, src );
        writer.AddAttribute(HtmlTextWriterAttribute.Alt, item.Status.StatusName);
        writer.RenderBeginTag(HtmlTextWriterTag.Img);
        writer.RenderEndTag();
        writer.RenderEndTag();
        writer.RenderEndTag();
}

最佳答案

Asp.net mvc 永远不会同时处理来自同一用户(实际上是同一 session )的多个请求,除非您关闭 session 状态或将其设为只读以执行该操作。

到你需要添加的 Controller 或 Action

[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]

现在这不适用于 Cassini(内置在 visual studio 中的网络服务器),因为它一次仅服务于单个请求,但如果您使用 IIS express 或 IIS 进行测试,您应该看到它们使用第一种方法并行加载。

关于c# - 渲染没有 1-1 效果的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12295609/

相关文章:

asp.net - web.config <add key ="webpages:Enabled"value ="true"/> 使自定义成员资格提供程序不起作用?

c# - 带有 cookie 身份验证的 ASP.Net Core 2.2 : how to avoid page redirect when not authorized for API only controllers

c# - 如何使用 FirstOrDefault 避免 Object reference not set to instance of object 错误?

asp.net-mvc - 转换常规 MVC 站点以在 phonegap 中使用

entity-framework - 将 DbContext 注入(inject) FluentValidation 验证器

asp.net-mvc - 没有特定 Controller 的 ASP.NET MVC 3 路由

C# - 使用 BackgroundWorker.ReportProgress 以自定义最大值更新 ProgressBar

c# - 如果在例程中多次访问对象的属性,创建变量是最佳做法吗?

c# - 为什么在 Javascript 中附加事件处理程序会阻止我的服务器端事件触发?

c# - 无限范围变量将分配在哪里?