在asp.net的MVC数据库显示图像 [英] Displaying a image from a database in asp.net mvc

查看:168
本文介绍了在asp.net的MVC数据库显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有其中包含一个用户ID和图像列的视图。

I have a view which contains a users id and an image column.

下面就是我试着做检索图像,但我不断收到一箱带有红色的X,而不是实际的图像。

Here's what i've tried doing to retrieve the image but i keep getting a box with an red x instead of the actual image.

查看

<td><img src="<%= Url.Action( "DisplayImage" , "User" , new { id = item.id} ) %>" alt="" /></td>

控制器

  public FileContentResult DisplayImage(string id)
    {
        byte[] image = repository.GetImage(id);
        return File(image, "image/jpg");
    }

我也试过,而不是返回一个ActionResult并没有任何工作。

i've also tried returning an ActionResult instead and that didn't work either.

    public Byte[] GetImage(string id)
    {

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

        if (image == null)
            return null;
        return image.UserImage;
    }

LinqTOSQL类

LinqTOSQL Class

    [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;
			}
		}
	}
}

我已经杀我整天都试图得到这个工作,但它只是不工作。
在存储过程的返回类型是一个整数(ATLEAST当我看看参数
SQL Server Management Studio中它说整数),但我不能重新定义,现在可以吗?

I've been killing myself all day trying to get this to work, but it just isn't working. The return type on the stored procedure is an integer (atleast when i look at parameters in SQL Server Management Studio it says integer), but i can't redefine that now can i?

它实际上击中由于UserController内的正确参数DisplayImage行动并返回文件(imageByteArray,图像/ JPG),但只显示红色的X的框。
任何帮助将大大AP preciated。

It's actually hitting the DisplayImage Action with the correct parameters within the UserController and returning File(imageByteArray, "image/jpg") but only a box with red x is being displayed. Any help would be greatly appreciated.

编辑:我的行动结果中添加Reponse.BinaryWrite(imageByteArray)和goign到的 HTTP://本地主机/用户/ DisplayImage ID = 10101010 以及该用户的图像显示在MSPAINT

edit: I've tried debugging by adding a Reponse.BinaryWrite(imageByteArray) within the action result and hitting the url directly by goign to http://localhost/User/DisplayImage?id=10101010 and the image for that user is displayed in mspaint.

EDIT2:我也做了查看源代码和我对于图像标记HTML来如以下

edit2: I also did a view source and my html for that image tag came out as following.

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

感谢

推荐答案

看看这个问题,我从前阵子有 - 的解决方案是<一个href=\"http://stackoverflow.com/questions/186062/can-an-asp-net-mvc-controller-return-an-image\">special的ActionResult类型的图像

Look at this question I had from a while back - the solution was special ActionResult type for images

编辑:这是我的code。

Here's my code. I'm actually creating an ImageResult class from an Image that I created with GDI+ like this :

 return new ImageResult()
 {
      ImageFormat = spriteInfo.ImageFormat,
      EncodedImageBytes = spriteInfo.GetImageStream()
 };

图片结果类。如果我提供了一个连接codedImageBytes参数将发送到输出流中,你会发现。这看起来像你想要什么。在另一方面,如果你只是传递一个图像,然后它将只写出来的图像到输出流。

The image result class is. You'll notice if I provide an EncodedImageBytes parameter it will send that to the output stream. This looks like exactly what you want. On the other hand if you're just passing in an Image then it will just write that Image out to the output stream.

 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);
            }
        }

    }

这篇关于在asp.net的MVC数据库显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆