I am trying to read image ( binary data type) directly from database. I can read if the image is single ,but if there are multiple images put together( inside one column) , I am not be able to read it. I am getting an error when try to debug:
System.ArgumentOutOfRangeException was unhandled by user code
Message="Index and count must refer to a location within the buffer.\r\nParameter name: bytes"
Source="mscorlib"
ParamName="bytes"
StackTrace:
at System.Text.ASCIIEncoding.GetString(Byte[] bytes, Int32 byteIndex, Int32 byteCount)
at GetImage.Page_Load(Object sender, EventArgs e) in c:\Documents and Settings\jc\My Documents\Visual Studio 2005\WebSites\CashApp\Cash\GetImage.aspx.cs:line 50
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
Can you take a look at below code and let me know what I am doing wrong?
there are columns called FrontImageOffset, FrontImageSize that tells me where an image's byte stream ends and a new image begins
so, I am using a sql command like below to figure out what I am supposed to looking for..
sql = "select FrontImageOffset, FrontImageSize from CDData where TransactionId=" + TransactionId;
using (SqlCommand cmd = new SqlCommand(sql, cn))
Below is code that I am using it.
using
using
System.Data;using
System.Configuration;using
System.Collections;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
System.Web.UI.HtmlControls;using
System.Drawing;using
System.Data.SqlClient;using
System.IO;using
System.Drawing.Imaging;using
System.Collections.Generic;public
partial class GetImage : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e){
const int oleOffset = 78; const int oleTypeStart = 20; const int oleTypeLength = 12; string TransactionId; string sql; Bitmap bmp; byte[] imageBytes;TransactionId = (
string)this.Request.QueryString["TransactionId"]; if (TransactionId == null) return;TransactionId =
int.Parse(TransactionId).ToString();sql =
"Select Image from IDXImage where TransactionId=" + TransactionId; ConnectionStringSettings cnSetting = ConfigurationManager.ConnectionStrings["CashAppConnectionString6"]; using (SqlConnection cn = new SqlConnection(cnSetting.ConnectionString)){
using (SqlCommand cmd = new SqlCommand(sql, cn)){
cn.Open();
using (SqlDataReader dr = cmd.ExecuteReader()){
dr.Read();
imageBytes = (
byte[])dr.GetValue(0);}
}
}
if (imageBytes == null || imageBytes.Length == 0) return; MemoryStream tempStream; string type = System.Text.Encoding.ASCII.GetString(imageBytes, oleTypeStart, oleTypeLength);
if (type == "Bitmap Image"){
tempStream =
new MemoryStream(imageBytes, oleOffset, imageBytes.Length - oleOffset);
}
else{
tempStream =
new MemoryStream(imageBytes, 0, imageBytes.Length);
}
bmp =
new Bitmap(tempStream); // bmp = new Bitmap(bmp, bmp.Height / 3 , bmp.Width / 2);Response.ContentType =
"Image/gif";bmp.Save(Response.OutputStream,
ImageFormat.Gif);Response.End();
}
private void _GetMultipleImages ()
{
const int oleOffset = 78; const int oleTypeStart = 20; const int oleTypeLength = 12; string TransactionId; string sql; byte[] imageBytes;TransactionId = (
string)this.Request.QueryString["TransactionId"]; if (TransactionId == null) return;TransactionId =
int.Parse(TransactionId).ToString();sql =
"Select Image from IDXImage where TransactionId=" + TransactionId; ConnectionStringSettings cnSetting = ConfigurationManager.ConnectionStrings["CashAppConnectionString6"]; using (SqlConnection cn = new SqlConnection(cnSetting.ConnectionString)){
using (SqlCommand cmd = new SqlCommand(sql, cn)){
cn.Open();
using (SqlDataReader dr = cmd.ExecuteReader()){
dr.Read();
imageBytes = (
byte[])dr["Image"];}
}
}
if (imageBytes == null || imageBytes.Length == 0) return; ArrayList imgList = new ArrayList(); using (SqlConnection cn = new SqlConnection(cnSetting.ConnectionString)){
sql =
"select FrontImageOffset, FrontImageSize from IDXCDData where TransactionId=" + TransactionId; using (SqlCommand cmd = new SqlCommand(sql, cn)){
cn.Open();
using (SqlDataReader dr = cmd.ExecuteReader()){
if (dr.HasRows){
if (dr.Read()){
// // Make a static copy of the buffer information // long imagebytesLen = imageBytes.Length; List<Byte> imgBytes = new List<byte>(imageBytes); do{
long offset = (long)dr["FrontImageOffset"]; long length = (long)dr["FrontImageSize"]; // // Determine if we have enough buffer to read the bytes // if (imagebytesLen >= (offset + length)){
// // allocate a new buffer for our image and then // make a copy of it from our image buffer. // byte[] newimage = new byte[length];imgBytes.CopyTo(
Convert.ToInt32 (offset), newimage, 0, newimage.Length); // // Now add us to our list //imgList.Add(newimage);
}
}
while (dr.NextResult());}
}
dr.Close();
}
}
}
// // Do we have any images? // if (imgList.Count > 0){
// // Loop through the list and concatenate all the images // into one huge image. // List<byte> bigImage = new List<byte>(); for (int i = 0; i < imgList.Count; ++i){
bigImage.AddRange((
byte[])imgList[i]);}
// // Create hte bitmap from the buffered data // byte[] data = bigImage.ToArray(); MemoryStream tempStream = new MemoryStream(imageBytes);tempStream.Position = 0;
System.Drawing.
Image bmp = System.Drawing.Image.FromStream(tempStream); // bmp = new Bitmap(bmp, bmp.Height / 3 , bmp.Width / 2);Response.ContentType =
"Image/gif";bmp.Save(Response.OutputStream,
ImageFormat.Gif);Response.End();
}
}
}
Replies
Know the answer? Post it — somebody with the same question will find it here.