Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Jpegs in a SQL Server database 1

Status
Not open for further replies.

StevenK

Programmer
Jan 5, 2001
1,294
GB
Can anyone point me to (or provide me with) a simple use of Delphi code that will allow me to have a SQL Server table that contains JPEGS, I need to be able to display and change these through means of a standard Delphi component (probably a TDBImage).
Anyone offer me an assist ?
Thanks in advance.
Steve
 
Don't database fields that store images have to be BLOB fields? Could be wrong, but that was the impression I was under. Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
No BLOB fields as such in SQL Server. I'm unclear whether I should be using 'image' / 'binary' data-types ?
Does anyone have a simple example they can provide me with or point me to ?
Thanks in advance.
Steve
 
Sorry, no examples, just comments.

Looking at the "SQL Server Books Online", it seems that BLOB's are supported, but it is called "image" data type. The problem with the "binary" data type is that it can store a maximum of 8000 bytes, which might be a little small when saving pictures.

As for the changing and displaying, my guess would be to use TBlobField typecast on the table/query field.

Hope this helps.
Cheers,
Nico
 
Thanks the Access example looks to be a good starting point example for us. We'll be giving it some further attention tomorrow back in the office.
I know I've seen the 'TBlob is not valid' message before it states about - so we can only have been on the right 'wrong' track.
:)
Thanks again - this should get us started off.
However, it doesn't mean we won't be back for more of an assist.

Steve
 
Can this be done with existing components ?
What datatype should I be aiming my images at in the SQL Server database ?
Thanks again.
Steve
 
Isn't that link for an example of a 'non-standard components' scenario ?
I need to be able to achieve this through conventional Delphi 5 component usage (if at all possible).
Anyone offer an assist ?
Steve
 
Steve I am also a fan of using conventional components, do you have the datapump utility?
My approach would be upscaling animals.db in the BDEDEMOS to sql-server. The datapump will select the right datafield. Steven van Els
SAvanEls@cq-link.sr
 
OK - I've tried that (and many other variations).
When I try to use datapump I get the error (when feeding the picture field) that the BLOB length is not valid.
This is the same message I get if trying to run syntax like :
Table1.Append;
Table1PicField.LoadFromFile('C:\pic1.jpg');
Table1.Post;

I've tried various data-types for the target SQL Server table field and keep hitting this same problem.

Can anyone suggest where I'm going wrong ?
Thanks in advance
Steve
 
With Delphi 7, you can display JPEG images with the TImage component. Here are some examples of using JPEG files with BLOB SQL fields. I found these concepts in an article from the web.

This code assumes that you have a TImage on a form, and a TTable to get to an Interbase server.

//
// Put this code in the Table's AfterScroll event to update the image when the table's current record changes.
//

Jpeg := TJpegImage.Create;
try
if not Table1.Fields[XXX].IsNull then
begin
BLS := TBLOBStream.Create(tblPiece.Fields[XXX] as TBLOBField, bmRead);
try
BLS.Position := 0;
try
Jpeg.LoadFromStream(BLS);
Image1.Picture.Assign(JPEG);
except
end;
finally
BLS.Free;
end;
end
else
Image1.Picture.Assign(Nil);
finally
Jpeg.Free;
end;

//
// End of snippet
//


This code loads a JPEG image from a file and stores it to a BLOB field.

procedure TForm1.Load1Click(Sender: TObject);
var
Jpeg: TJpegImage;
BLS: TBlobStream;
begin
if Table1.RecordCount = 0 then
Exit;
if OpenDialog1.Execute = True then
begin
Jpeg := TJpegImage.Create;
try
Jpeg.LoadFromFile(OpenDialog1.FileName);
Table1.Edit;
// The XXX is the numeric index of the blob field.
// FieldByName can also be used . . .
BlS := TBlobStream.Create(Table1.Fields[XXX] as TBLOBField, bmReadWrite);
try
BLS.Position := 0;
Jpeg.SaveToStream(BLS);
Image1.Picture.Assign(JPEG);
finally
BLS.Free;
end;
finally
Jpeg.Free;
end;
end;
end;

Happy Coding,
Flubbo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top