views:

157

answers:

1

Hello!

I'm developing a WinForm app for Windows Mobile 6.0 with C#, .NET Compact Framework 2.0 SP2 and SqlServer CE 3.1.

I have this code that is not working:

using (SqlCeDataReader reader = cmd.ExecuteReader())
{
    if (reader.Read())
    {
        //read the signature from the database
        long imgSize = reader.GetBytes(0, 0, null, 0, 0);
        image= new byte[imgSize];
        reader.GetBytes(0, 0, image, 0, 0);
    }
}

I think there is a problem obtaining all data stored on the column containing bytes from the image.

When I do this:

bitmapImage = new Bitmap(new MemoryStream(image));

I'm getting an OutOfMemoryException.

But if I use a TableAdapter to obtain the image it works perfectly.

What I'm doing wrong?

Thanks.

+1  A: 

Try this instead:

var ms = new MemoryStream(image)
bitmapImage = new Bitmap(ms);
// dont close the memorystream

Update

The problem lies with

reader.GetBytes(0, 0, buffer, 0, 0);

Clearly it needs to be more than 0 bytes in length (last parameter).

leppie
No, it doesn't work.
VansFannel
Yes, you are right. I've changed that line of code to: reader.GetBytes(0, 0, buffer, 0, imgSize);And, it works!!!
VansFannel
Cool, glad it was easy, sometimes these things just happen :)
leppie