图像检索提供错误索引超出了数组的范围。 [英] Image retreive gives error Index was outside the bounds of the array.

查看:84
本文介绍了图像检索提供错误索引超出了数组的范围。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将页面加载中的图像从数据库转换为img控件但我收到此错误。

索引超出了数组的范围。



I am trying to get image at pageload from database to a img control but i receive this error.
"Index was outside the bounds of the array."

<img  runat="server" id="image" alt="" height="100" width="100"/>







protected void LoadImages()
    {
        SqlCommand cmd = new SqlCommand("sps_getimage", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@ad_id", 10010);
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess);
        if (reader.HasRows)
        {
            reader.Read();
            MemoryStream memory = new MemoryStream();
            long startIndex = 0;
            const int ChunkSize = 256;
            while (true)
            {
                byte[] buffer = new byte[ChunkSize];
                long retrievedBytes = reader.GetBytes(1, startIndex, buffer, 0, ChunkSize);
                memory.Write(buffer, 0, (int)retrievedBytes);
                startIndex += retrievedBytes;
                if (retrievedBytes != ChunkSize)
                    break;
            }

            byte[] data = memory.ToArray();
            memory.Dispose();
            image.Src = "data:image/png;base64," + Convert.ToBase64String(data);
        }
        con.Close();
    }

推荐答案

GetBytes方法中的startIndex参数是指目标数组中的起始索引,而不是源。只需在那里使用0。
The startIndex parameter in the GetBytes method is referring to the starting index in the target array, not the source. Just use 0 there.


这篇关于图像检索提供错误索引超出了数组的范围。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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