Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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
在asp.net mvc中显示数据库中的图像_Asp.net_Asp.net Mvc_Database_Image - Fatal编程技术网

在asp.net mvc中显示数据库中的图像

在asp.net mvc中显示数据库中的图像,asp.net,asp.net-mvc,database,image,Asp.net,Asp.net Mvc,Database,Image,我有一个包含用户id和图像列的视图 下面是我试图检索图像的步骤,但我一直得到一个带有红色x的框,而不是实际图像 看法 我也尝试过返回一个ActionResult,但也没有成功 存储库 public Byte[] GetImage(string id) { var image = db.GetImage(id).First<GetImageResult>(); if (image == null) return

我有一个包含用户id和图像列的视图

下面是我试图检索图像的步骤,但我一直得到一个带有红色x的框,而不是实际图像

看法

我也尝试过返回一个ActionResult,但也没有成功

存储库

    public Byte[] GetImage(string id)
    {

        var image = db.GetImage(id).First<GetImageResult>();

        if (image == null)
            return null;
        return image.UserImage;
    }
public Byte[]GetImage(字符串id)
{
var image=db.GetImage(id).First();
if(image==null)
返回null;
返回image.UserImage;
}
LinqTOSQL类

    [Function(Name="dbo.GetImage")]
public ISingleResult<GetImageResult> GetImage([Parameter(DbType="VarChar(8)")] string id)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), id);
    return ((ISingleResult<GetImageResult>)(result.ReturnValue));
}

public partial class GetImageResult
{
    private System.Byte[] _userImage;

    public GetImageResult()
    {
    }


    [Column(Storage="_userImage", DbType="Image")]
    public System.Byte[] UserImage
    {
        get
        {
            return this._userImage;
        }
        set
        {
            if ((this. _userImage!= value))
            {
                this. _userImage = value;
            }
        }
    }
}
[函数(Name=“dbo.GetImage”)]
public ISingleResult GetImage([Parameter(DbType=“VarChar(8)”)]string id)
{
IExecuteResult=this.ExecuteMethodCall(this,((MethodInfo)(MethodInfo.GetCurrentMethod()),id);
返回((IsingResult)(result.ReturnValue));
}
公共部分类GetImageResult
{
private System.Byte[]\u userImage;
公共GetImageResult()
{
}
[Column(Storage=“\u userImage”,DbType=“Image”)]
公共系统。字节[]用户映像
{
得到
{
返回此。\u userImage;
}
设置
{
if((this.\u userImage!=值))
{
这是。_userImage=value;
}
}
}
}
我一整天都在拼命想让它工作,但就是不起作用。 存储过程上的返回类型是整数(至少当我查看 SQLServerManagementStudio(它说的是整数),但我现在不能重新定义它,可以吗

它实际上在UserController中使用正确的参数点击DisplayImage操作并返回文件(imageByteArray,“image/jpg”),但只显示一个带有红色x的框。 任何帮助都将不胜感激

编辑:我尝试过在操作结果中添加一个Reponse.BinaryWrite(imageByteArray)并通过goign to直接点击url进行调试,该用户的图像显示在mspaint中

edit2:我还做了一个视图源代码,我的图像标签的html如下所示

<td>
    <img src='/User.mvc/GetImage?id=U00915441' alt="" />
</td>


谢谢

看看我刚才问的这个问题,答案是

编辑:这是我的代码。我实际上是从我用GDI+创建的图像创建ImageResult类,如下所示:

 return new ImageResult()
 {
      ImageFormat = spriteInfo.ImageFormat,
      EncodedImageBytes = spriteInfo.GetImageStream()
 };
图像结果类为。您会注意到,如果我提供一个EncodedImageBytes参数,它会将其发送到输出流。这看起来正是你想要的。另一方面,如果您只是传入一个图像,那么它只会将该图像写入输出流

 public class ImageResult : ActionResult
    {
        public ImageResult() { }
        public int? Quality { get; set; }
        public Image Image { get; set; }
        public ImageFormat ImageFormat { get; set; }
        public byte[] EncodedImageBytes { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            // verify properties 
            if (EncodedImageBytes == null)
            {
                if (Image == null)
                {
                    throw new ArgumentNullException("Image");
                }
            }
            if (ImageFormat == null)
            {
                throw new ArgumentNullException("ImageFormat");
            }
            // output 
            context.HttpContext.Response.Clear();

            if (ImageFormat.Equals(ImageFormat.Bmp)) context.HttpContext.Response.ContentType = "image/bmp";
            if (ImageFormat.Equals(ImageFormat.Gif)) context.HttpContext.Response.ContentType = "image/gif";
            if (ImageFormat.Equals(ImageFormat.Icon)) context.HttpContext.Response.ContentType = "image/vnd.microsoft.icon";
            if (ImageFormat.Equals(ImageFormat.Jpeg)) context.HttpContext.Response.ContentType = "image/jpeg";
            if (ImageFormat.Equals(ImageFormat.Png)) context.HttpContext.Response.ContentType = "image/png";
            if (ImageFormat.Equals(ImageFormat.Tiff)) context.HttpContext.Response.ContentType = "image/tiff";
            if (ImageFormat.Equals(ImageFormat.Wmf)) context.HttpContext.Response.ContentType = "image/wmf";

            // output stream
            Stream outputStream = context.HttpContext.Response.OutputStream;
            if (EncodedImageBytes != null)
            {
                outputStream.Write(EncodedImageBytes, 0, EncodedImageBytes.Length);
            }
            else
            {
                ImageUtil.SaveImageToStream(outputStream, Image, ImageFormat, Quality);
            }
        }

    }

我遇到过这样的问题,它只是试图通过路径而不是从路径中找到图像。您是否尝试过调试应用程序以查看是否进入控制器?它确实命中了控制器操作DisplayImage(字符串id),并且没有任何问题。使用fiddler也可以查看到底返回了什么。它在fiddler的imageview选项卡中显示了实际图像。当我转到textview时,它会显示一个奇怪的字符串,以数据技术公司的ImageMan结尾(1$Þ当我在我的web浏览器中查看源代码时,如果这没有帮助,我会得到回复,我会挖掘我使用的代码。我实际上是动态创建图像并毫无问题地提供它们-所以我是从字节流执行的好吧,我没有ImageUtil.SaveImageToStream函数,所以我评论说t并运行它,我仍然得到相同的错误。以下是我在控制器var imageResult=new imageResult();imageResult.EncodedImageBytes=repository.GetImage(id)中所做的操作;imageResult.ImageFormat=ImageFormat.Bmp;返回imageResult。我使用了与先前发布的相同的视图,但仍然得到一个红色的x。在fiddler中,我确实在imageview选项卡中看到了实际图像,但它没有显示.try System.IO.File.WriteAllBytes(“test.Bmp”,字节)在action方法中,然后验证您是否可以将该图像直接加载到浏览器中。查看Fiddler并查看返回的内容-包括标题。他们有一个ImageView选项卡-查看您是否在那里看到任何内容。所有浏览器都可以加载BMP吗?我不确定。我想可以,但也要检查一下。BMP似乎是一种不好的格式,无论如何都可以使用好吗是的,我照你说的做了。它会将图像写入文件,但如果我尝试使用IE或Chrome打开图像,它不会显示。如果我使用paint或任何其他图片应用程序打开图像,它会显示。我还尝试将图像类型更改为jpeg,并遇到相同的结果。在fiddler的imageview选项卡中,我可以看到图像。然后你的问题与MVC无关。这是因为浏览器无法加载图像。你需要将图像重新编码为JPG或PNG。因为它已经是BMP,只有1712字节,我假设它不是照片。那么我建议使用PNG。你应该将图像编码为JPEG到数据库中。现在你说你已经将图像类型更改为JPEG。你怎么办你的意思是?如果你想改变内容类型的话,你就不能这么做。在担心MVC相关的问题之前,你需要找到一个可以将.writealBytes()归档并加载到IE中的点
 return new ImageResult()
 {
      ImageFormat = spriteInfo.ImageFormat,
      EncodedImageBytes = spriteInfo.GetImageStream()
 };
 public class ImageResult : ActionResult
    {
        public ImageResult() { }
        public int? Quality { get; set; }
        public Image Image { get; set; }
        public ImageFormat ImageFormat { get; set; }
        public byte[] EncodedImageBytes { get; set; }

        public override void ExecuteResult(ControllerContext context)
        {
            // verify properties 
            if (EncodedImageBytes == null)
            {
                if (Image == null)
                {
                    throw new ArgumentNullException("Image");
                }
            }
            if (ImageFormat == null)
            {
                throw new ArgumentNullException("ImageFormat");
            }
            // output 
            context.HttpContext.Response.Clear();

            if (ImageFormat.Equals(ImageFormat.Bmp)) context.HttpContext.Response.ContentType = "image/bmp";
            if (ImageFormat.Equals(ImageFormat.Gif)) context.HttpContext.Response.ContentType = "image/gif";
            if (ImageFormat.Equals(ImageFormat.Icon)) context.HttpContext.Response.ContentType = "image/vnd.microsoft.icon";
            if (ImageFormat.Equals(ImageFormat.Jpeg)) context.HttpContext.Response.ContentType = "image/jpeg";
            if (ImageFormat.Equals(ImageFormat.Png)) context.HttpContext.Response.ContentType = "image/png";
            if (ImageFormat.Equals(ImageFormat.Tiff)) context.HttpContext.Response.ContentType = "image/tiff";
            if (ImageFormat.Equals(ImageFormat.Wmf)) context.HttpContext.Response.ContentType = "image/wmf";

            // output stream
            Stream outputStream = context.HttpContext.Response.OutputStream;
            if (EncodedImageBytes != null)
            {
                outputStream.Write(EncodedImageBytes, 0, EncodedImageBytes.Length);
            }
            else
            {
                ImageUtil.SaveImageToStream(outputStream, Image, ImageFormat, Quality);
            }
        }

    }