Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.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# 在mvc3中将blob图像对象从列表显示到网页_C#_Asp.net_Asp.net Mvc 3_Blob - Fatal编程技术网

C# 在mvc3中将blob图像对象从列表显示到网页

C# 在mvc3中将blob图像对象从列表显示到网页,c#,asp.net,asp.net-mvc-3,blob,C#,Asp.net,Asp.net Mvc 3,Blob,我正在做一个项目,从数据库中加载12个长blob图像,并将它们保存在列表中 在html页面中,我必须显示图像,但在尝试将blob转换为图像时出错 当使用内存流时,我得到错误参数无效。无论我做什么改变,都无法摆脱那个错误 代码如下: public Image getProduct_Image(byte[] imagebytes) { byte[] byteArray = new byte[imagebytes.Length]; MemoryStre

我正在做一个项目,从数据库中加载12个长blob图像,并将它们保存在列表中

在html页面中,我必须显示图像,但在尝试将blob转换为图像时出错

当使用内存流时,我得到错误
参数无效
。无论我做什么改变,都无法摆脱那个错误

代码如下:

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

我看不到你在任何地方用数据填充你的byteArray

另外,为什么首先要创建byteArray变量?在输入变量imagebytes中,blob数据已经作为字节[]存在。删除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
    }
}

我看不到你在任何地方用数据填充你的byteArray

另外,为什么首先要创建byteArray变量?在输入变量imagebytes中,blob数据已经作为字节[]存在。删除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
    }
}

关于我的评论:

您可以使用.ashx处理程序在几行代码中将图像从字节写入HTML,但由于您使用的是MVC,所以实际上非常简单

首先,您只需设置一个控制器操作—假设您的图像基于整数ID是可识别的。只需一行即可将这些字节作为内容返回

public FileContentResult SomeImage(int id)
{
    byte[] bytes = GetImageBytesFromDatabase(id);
    return File(bytes, "image/jpeg");
}
您的标记只是一个图像标记,其源为此控制器操作:

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

这实际上会创建以下内容,具体取决于您是否对路由进行了特殊处理:

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

或者可能

展开我的评论:

您可以使用.ashx处理程序在几行代码中将图像从字节写入HTML,但由于您使用的是MVC,所以实际上非常简单

首先,您只需设置一个控制器操作—假设您的图像基于整数ID是可识别的。只需一行即可将这些字节作为内容返回

public FileContentResult SomeImage(int id)
{
    byte[] bytes = GetImageBytesFromDatabase(id);
    return File(bytes, "image/jpeg");
}
您的标记只是一个图像标记,其源为此控制器操作:

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

这实际上会创建以下内容,具体取决于您是否对路由进行了特殊处理:

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

或者可能

使用
图像
对象有什么原因吗?如果您在处理web上的图像,我希望有一个常规的
标记指向一个处理程序(ashx或FileContentResult),该处理程序从数据库检索字节并将它们直接转储到HTTP输出。在你的场景中可能吗?哦!!坦白说,我还没试过!你有关于如何使用它的文章吗?你使用
图像
对象有什么原因吗?如果您在处理web上的图像,我希望有一个常规的
标记指向一个处理程序(ashx或FileContentResult),该处理程序从数据库检索字节并将它们直接转储到HTTP输出。在你的场景中可能吗?哦!!坦白说,我还没试过!你有关于如何使用它的文章吗?我尝试了相同的方法,但仍然在ms.System.Drawing.Image returnImage=Image.FromStream((Stream)ms)中得到错误,即“参数无效”;嗯。你能提供一些样本数据吗?blob是一个转换成字节[]数组的图像还是仅仅是原始RGB值?我尝试了相同的方法,但仍然得到错误,因为在ms.System.Drawing.image returnImage=image.FromStream((Stream)ms)处“parameter is not valid”;嗯。你能提供一些样本数据吗?blob是一个转换成字节[]数组的图像还是仅仅是原始RGB值?伙计们,我的一个朋友找到了答案!嗯,这是我的错。插入不正确!谢谢你的帮助。伙计们,我的一个朋友发现了!嗯,这是我的错。插入不正确!谢谢你的帮助。