-2

I would like load JPEG image into a blob field, is it possible? I search a lot but I don't find a clear answer.

I use this code:

var
  BlobField : TBlobField;
  Stream : TMemoryStream;
begin
  BlobField := ClientDataSet1.FieldByName('image');
  // Img is TImage and contain a JPEG image 
  Img.Picture.Graphic.SaveToStream(Stream); 
  Stream.Position := 0;
  BlobField.LoadFromStream(Stream);  // <-- Error: "Bitmap image is not valid"

I have to use bitmap image?

SOLVED: The problem was a TDBImage component on the form!. This component was linked to the field only for testing and since it work only with bitmat it give the error

Marco Andreolli
  • 316
  • 5
  • 19
  • 1
    related: http://stackoverflow.com/questions/18655031/how-to-display-blob-image-from-database-in-the-tadvstringgrid-with-the-help-of-d – bummi Sep 12 '13 at 09:59
  • @David Heffernan the field is TBlobField and the blob type is ftBlob – Marco Andreolli Sep 12 '13 at 10:14
  • @David Heffernan the table is not defined in the database! I use a ClientDataSet, the data are local – Marco Andreolli Sep 12 '13 at 10:26
  • Are you sure it is `ftBlob`. If it is then it should be able to take any data. It should not care about the contents. Are you sure that the image field is not in fact `ftGraphic`? – David Heffernan Sep 12 '13 at 10:32
  • @MarcoAndreolli: sorry for asking that, but is the exception message precisely *"Bitmap image is not valid"*? I ask it because once inside the stream, it´s impossible to the field to know that the stream content is an image. `TBlobField` if a general blob container, it does not know anything about image formats. – AlexSC Sep 12 '13 at 11:00
  • another related including links to different duplicates: http://stackoverflow.com/questions/14726756/retrieve-image-saved-on-database – bummi Sep 12 '13 at 11:25
  • @AlexSC @MarcoAndreolli this exception is occured on string `Img.Picture.Graphic.SaveToStream(Stream)`, and not on `BlobField.LoadFromStream(Stream)` – vladon Sep 12 '13 at 13:04

2 Answers2

0

Try to use

Img.Picture.Bitmap.SaveToStream(Stream)

I.e. Bitmap property instead of Graphic

--

And if you do not want use bitmap of image, you can use TJPEGImage.

So, for example:

var
  MyJPEGImage: TJPEGImage;

[ ... ]

MyJPEGImage := TJPEGImage.Create;
MyJPEGImage.Assign(Img.Picture.Bitmap);
MyJPEGImage.SaveToStream(Stream);
Stream.Position := 0;
BlobField.LoadFromStream(Stream);

Try this.

vladon
  • 8,158
  • 2
  • 47
  • 91
  • One would imagine that the idea is to store the compress JPEG data rather than uncompressed BMP – David Heffernan Sep 12 '13 at 09:30
  • @vladon I must have a JPEG or PNG image stored in the blob, not a Bitmap – Marco Andreolli Sep 12 '13 at 10:08
  • @MarcoAndreolli but you are using abstract method. Take a look at source\vcl\Vcl.Graphics.pas, procedure TGraphic.SaveToStream defined as abstract. And Img.Picture.Graphic is of type TGraphic, so, you must create TJPEGImage, load bitmap into it, and save it to stream. – vladon Sep 12 '13 at 12:53
  • 1
    @vladon The instance is not TGraphic, just the reference. It's a virtual method. The instance will be a class that does overrides and implements the abstract methods. – David Heffernan Sep 12 '13 at 13:48
0

I use code like this

image1.Picture.LoadFromFile (filename);
fs:= TFileStream.Create (filename, fmOpenRead);
try
 blobfield.LoadFromStream (fs);
finally
 fs.Free;
end;

And having written that, I wonder what the first line is for. Presumably for displaying the image on the screen whilst saving it to the database (or more accurately, to the blobfield).

No'am Newman
  • 6,395
  • 5
  • 38
  • 50
  • That's the same code as in the question. How does it solve the problem? What is the problem? – David Heffernan Sep 12 '13 at 09:35
  • 1
    In the original question, it looks like the file (or memory) stream is not created (unless the 'savetostream' call does this) which might well be the problem. – No'am Newman Sep 12 '13 at 09:38
  • I see what you mean, but I guess that's just a transliteration error. If `Stream` was invalid then `SaveToStream` would fail. The call to `Position := 0` would fail. – David Heffernan Sep 12 '13 at 09:44
  • Your code don't solve the problem, It give me the same error!, I don't know if matter but I use a ClientDataSet – Marco Andreolli Sep 12 '13 at 09:55
  • @MarcoAndreolli: maybe the bitmap image is indeed not valid which would explain why it is not being saved. Does the image display on the screen? – No'am Newman Sep 12 '13 at 09:57
  • @MarcoAndreolli I asked a question. Do you have an answer? – David Heffernan Sep 12 '13 at 09:58
  • @No'am Newman The image are correct, I put a image on the form because I don't want load from a file. The must be a JPEG or GPG not a bitmap!. When I call "LoadFromStream" or "LoadFromFile" I give the error "Bitmap image is not valid" – Marco Andreolli Sep 12 '13 at 10:01
  • I found the trouble! I had put a TDBImage component (linked to the field) on the form for testing. This component work only with bitmap and so return the error "Bitmap image is not valid". Thanks @No'am Newman – Marco Andreolli Sep 12 '13 at 13:25