Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

c# - How to read a image by IDataReader

I work on window C# vs05. i want to read a image by IDataReader....in oledb....how can i do it suppose i want to read int value from database column name student id then i write code in bellow way

private IDataReader _reader;

public int GetInt32(String sFieldName)
        {
            return (_reader[sFieldName]==DBNull.Value)? (Int32) 0 :Convert.ToInt32(_reader[sFieldName]);
        }

To save the image i use bellow code

     DataTable oTable = new DataTable();
                   oTable.Columns.Add("BoardImage");
                   DataRow oRow = oTable.NewRow();
                   oRow["BoardImage"] = oItem.BoardImage;
               SQL= Insert into table oRow["BoardImage"];



Now i want to read image BY Idatareader ......to do that i write bellow code but i get error message "Parameter is not valid."



    public Image GetImage(String sFieldName)
        {
            try
            {
                //MemoryStream stream = new MemoryStream();
                //stream.Write(image, 0, image.Length);
                //Bitmap bitmap = new Bitmap(stream);

                byte[] imageData = null;
                long byteSize = 0;
                byteSize = _reader.GetBytes(_reader.GetOrdinal(sFieldName), 0, null, 0, 0);  **//May be error occur here**

                imageData = new byte[byteSize];
                long bytesread = 0;
                int curpos = 0, chunkSize = 500;
                while (bytesread < byteSize)
                {
                    // chunkSize is an arbitrary application defined value 
                    bytesread += _reader.GetBytes(_reader.GetOrdinal(sFieldName), curpos, imageData, curpos, chunkSize);
                    curpos += chunkSize;
                }

                byte[] imgData = imageData;

                MemoryStream ms = new MemoryStream(imgData); **//May be error occur here**
                Image oImage = Image.FromStream((Stream)ms);
                return oImage;    



            }
            catch(Exception ex)
            {
                throw new Exception("Failed to get Image: " + ex.Message);
            }
        }

can any one help me ?Where is my bug.....How to solve this problem

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try something like (ensuring that the reader is in sequential mode):

using(MemoryStream ms = new MemoryStream()) {
    const int BUFFER_SIZE = 1024;
    byte[] buffer = new byte[BUFFER_SIZE];

    int col = reader.GetOrdinal(fieldName), bytesRead, offset = 0;
    while((bytesRead = (int)reader.GetBytes(col,offset,buffer,0,BUFFER_SIZE)) > 0) {
        ms.Write(buffer, 0, bytesRead);
        offset += bytesRead;
    }
    ms.Position = 0;
    return Image.FromStream(ms);
}

The biggest problm I can see in the original code is not looking too much at the number of bytes returned per call to GetBytes (i.e. assuming you got a whole chunk).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...