Your code tells me that the images stored in the DB can be of any format, right? Then, of course, you cannot load load them into a bitmap in the first place, because a bitmap accepts only bmp files. You determine the file format from reading the header. So, if the header is that of a bmp load the stream into a TBitmap, if it is that of a jpeg load the stream into a TJpegImage etc.