c# - Image retrieve gives error Index was outside the bounds of the array -
I am trying to get the image in IMG control from the database on pageload but I get this error. "Index was outside the bounds of the array." "Long-retrieved bits = Reader. Gates (1, Beginning INDEX, buffer, 0, chunky);"
& lt; Img runat = "server" id = "image" alt = "" height = "100" width = "100" /> Protected Zero Load Image () {SqlCommand CMD = New SQL Commands ("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 beginningindexx = 0; Const int chancisy = 256; While (true) {byte [buffer = new byte [chunky]]; Long-lasted Boets = Reader Getbytes (1, StartIndex, Buffer, 0, Chunky's); Memory. Write (buffer, 0, (integer were retrieved); StartIndex + = Recovered Bytes; If (reclaimed bwight! = Chunk season) brakes; } Byte [] data = memory Toure (); Memory.Dispose (); Image.Src = "Data: Image / PNG; Base 64," + Convert. ToBase64String (Data); } Con.Close (); }
get started parameter GetBytes method is talking about the initial index in the target array, not the source. Just use 0 .
// var ms = new MemoryStream ()) {// function as usual} // execution is automatically called, even if an uncontrolled exception
Comments
Post a Comment