read multiple image from dataabse

 have two table: data and image talbe.

Data table
Transactionid documenttype frontimage offset frontimage size
1934318415 CHECK 1 11264
1934318415 COUP 18433 27648
1934318415 COUP 57345 39936

The front/rear offset tell you where the image data in image table begins and the front/rear length tell you how many bytes from the offset to extract to get the image out of the .
Image table..

Image table
Transaction ID, Image
1934318415


Below id thecode that I am going to use to retrive the Coup images. Can you help me out what I am doing wrong

using System;

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)

{

}

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 Image a join Data b on a.transactionid = b.transactionid where documenttype = 'CHECK' and b.TransactionId=" + TransactionId;

ConnectionStringSettings cnSetting =

ConfigurationManager.ConnectionStrings["AppConnectionString6"];

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,rearimageoffset, rearimagesize from mage a join Data b on a.transactionid = b.transactionid where documenttype = 'CHECK' and b.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[])imgListIdea);

}

//

// 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();

}

}

}

 

 



Answer this question

read multiple image from dataabse

  • PennyP

    This is the logic that I am going to use to retrive the coup images. Can you help me out writing a code

    //
    // Write the logic to retrieve the byte array from the Image table
    // … … … …
    MemoryStream ImageStream = new MemoryStream (Byte_Array_From_Image_Table);

    // You allocate a buffer to hold the image in
    Byte[] bytes = new Byte[FrontImageSize + RearImageSize];
    int offset = Convert.ToInt32(FrontImageOffset) - 1;

    // Move the pointer in your stream to offset position and then read
    ImageStream.Seek(offset, SeekOrigin.Begin);
    ImageStream.Read(bytes, 0, bytes.Length);

    // Create a image object from that buffer
    Bitmap bmp = new Bitmap(new MemoryStream(bytes));

    // retrive image
     -- modified at 16:55 Thursday 12th January, 2006


  • read multiple image from dataabse