I have a simple little Qt/MySql program that puts a JPG image in a MySQL database and then retrieves it. It looks like it "works", but when I save out the image that I retrieve, the size of the two files (the one I read from disk originally and the one I save out) are different sizes (original is ~69Kb and the saved outr is ~14Kb). Now I can't tell any difference in the images in an image viewer but I cant tell why the sizes are different. But if I do this method for a .bmp file the sizes are the same. Anyone have an explanation?

The database only has 2 columns
id - integer
image - mediumblob

Here is the code:

Qt Code:
  1. QVariant id(0);
  2. QByteArray bytes;
  3. QBuffer buffer(&bytes);
  4. QImage testImage;
  5. QImageWriter writer(&buffer, "JPG");
  6.  
  7. bool test = testImage.load("testOut.jpg","JPG");
  8. int numBytes = testImage.numBytes();
  9. writer.write(testImage);
  10. QByteArray data = buffer.data();
  11.  
  12. //Put image in database as byteArray
  13. if(!query->prepare("INSERT into testBlob (id,image) VALUES (?,?)"))
  14. qDebug() << query->lastError().text();
  15.  
  16. query->addBindValue(id);
  17. query->addBindValue(data);
  18.  
  19. if(!query->exec())
  20. {
  21. qDebug() << query->lastError().text();
  22. }
  23.  
  24. //Now get image back out of database
  25. if(!query->prepare("SELECT * FROM testBlob where id = 11 "))
  26. qDebug() << query->lastError().text();
  27.  
  28. if(!query->exec())
  29. {
  30. qDebug() << query->lastError().text();
  31. }
  32.  
  33. QByteArray image;
  34. if(query->first() == true)
  35. {
  36. QSqlRecord rec = query->record();
  37. int id = query->value(rec.indexOf("id")).toInt();
  38. image = query->value(rec.indexOf("image")).toByteArray();
  39. }
  40.  
  41. //Write retrieved image out to disk to compare to original
  42. QImage imageWrite;
  43. imageWrite.loadFromData(image,"JPG");
  44. imageWrite.save("savedImage.jpg","JPG");
To copy to clipboard, switch view to plain text mode