简体   繁体   English

在MVC3中从列表到Web页面显示Blob图像对象

[英]Displaying blob images objects from a list to a web page in mvc3

I am working on a project where I load 12 long blob images from a database and save them in a list. 我正在一个项目中,该项目从数据库加载12个长斑点图像并将其保存在列表中。

In the html page, i have to display the images but i am getting errors when trying to convert a blob to an image. 在html页面中,我必须显示图像,但是在尝试将Blob转换为图像时出现错误。

I get the error Parameter is not valid when using memory stream. 我收到错误消息,在使用内存流时Parameter is not valid No matter whatever change I make, unable to get rid of that error. 无论我进行了什么更改,都无法消除该错误。

Below is the code: 下面是代码:

    public Image getProduct_Image(byte[] imagebytes)
    {
        byte[] byteArray = new byte[imagebytes.Length];

        MemoryStream ms = new MemoryStream(byteArray);
        ms.Position = 0;

        ms.Read((byteArray, 0, byteArray.Length);
        ms.ToArray();
        ms.Seek(0, SeekOrigin.Begin);
        System.Drawing.Image returnImage = Image.FromStream((Stream) ms);
             Bitmap bmp = new Bitmap(returnImage);
             return bmp;
    }

Expanding on my comment: 扩展我的评论:

You can write images from bytes into HTML in a few lines of code with an .ashx handler, but since you're using MVC, it's actually really easy. 您可以使用.ashx处理程序通过几行代码将字节中的图像写入HTML,但是由于您使用的是MVC,因此实际上非常简单。

First you just set up a controller action - assume your image is identifiable based on an integer ID. 首先,您只需设置一个控制器操作-假设您的图像是基于整数ID即可识别的。 It's just a single line to return those bytes as content. 将这些字节作为内容返回只是一行。

public FileContentResult SomeImage(int id)
{
    byte[] bytes = GetImageBytesFromDatabase(id);
    return File(bytes, "image/jpeg");
}

Your markup is just an image tag with the source as this controller action: 您的标记只是一个图像标签,其中的源代码是此控制器的操作:

<img src="@Url.Action("SomeImage", "Home", new { id = 123 })" />

This actually creates the following, depending on whether you're doing anything special with your routing: 这实际上会创建以下内容,具体取决于您是否对路由进行了特殊处理:

<img src="/Home/SomeImage/123" />
or possibly
<img src="/Home/SomeImage?id=123" />

I dont see you actually filling your byteArray with data anywhere! 我看不到您实际上在任何地方用数据填充了byteArray!

Also, why do you create the byteArray variable in the first place? 另外,为什么首先要创建byteArray变量? you already have the blob data as a byte[] in the input variable imagebytes. 您已经在输入变量imagebytes中将blob数据作为byte []了。 Remove byteArray and use imagebytes. 删除byteArray并使用imagebytes。

public Image getProduct_Image(byte[] imagebytes)
{
    try
    {
        if(imagebytes == null || imagebytes.Length == 0)
            throw new InvalidDataException("The blob does not contain any data");  

        MemoryStream ms = new MemoryStream(imagebytes);
        ms.Position = 0;
        ms.Read((imagebytes, 0, imagebytes.Length);
        ms.ToArray();
        ms.Seek(0, SeekOrigin.Begin);

        System.Drawing.Image returnImage = Image.FromStream((Stream) ms);
        return new Bitmap(returnImage);
    }
    catch(Exception ex)
    {
        // deal with the exception
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM