Storing and Retreiving JPG images using ADO and StoredProcedures
- From: Bill <dontwantnospam@xxxxxxxxxx>
- Date: Sat, 23 Jul 2005 13:00:48 -0500
I have scoured the newsgroups and read several articles and tried
several things but all to no avail.
No matter what I do, it seems that an image is stored in the database
as a BMP and thus causes problems when I try to retrieve and display
the image.
D7, ADO, SQL Server 2000
All data is passed to and from the database via Stored Procedures.
As a test, I wrote the following two methods to see what exactly would
occur. I save the scanned image to an actual JPG file on disk. Then
I load this image directly into the parameter via a loadfromfile.
When I retrieve the data, if I read from the save disk file(s) all is
well. If I read the data from the database and save it to a file
directly, the file format is a BMP, not a JPG and therefore causes an
error when I try to load it into the TImage. I have tried to load it
directly into the TImage and that does not work either, thus the disk
file test to see exactly what is going on.
When I examine the file created by the SaveImageData method, the file
has the appropriate JPG file signature ($FF$D8) as the first two bytes
of the file. When I examine the file created in the
BrokenLoadImageData method, the file has a BMP file signature (BM).
Stored procedures are at the end of the message.
We will be saving thousands upon thousands of rows of data and we
really need to use as little space as possible, thus the need to save
the images as JPEG. Since the image is actually being saved as BMP we
are using 20x the storage space. We could save the images directly to
disk but that is not really a desired option.
Any assistance would be greatly appreciated.
Bill
// Save the scanned image to a JPG file and then use that file to
write to the data table
// ???Full.JPG = 93K in size and appears to be a valid JPG file
procedure TdlgPlayer.SaveImageData;
var
FilePath: string;
begin
FilePath := AddBackSlashL(g_TempFolder);
ADOStoredproc1.Parameters.ParamByName('@PlayID').Value :=
sPlayer.PlayID;
if assigned(imgFullIdImage.Picture.Graphic) then
begin
TJPEGImage(imgFullIdImage.Picture.Graphic).SaveToFile(format('%s%dfull.jpg',[filepath,
splayer.PlayID]));
ADOStoredProc1.Parameters.ParamByName('@playidFullImage').LoadFromFile(format('%s%dfull.jpg',[filepath,
splayer.PlayID]), ftBlob);
end;
if assigned(imgFaceIdImage.Picture.Graphic) then
begin
TJPEGImage(imgFaceIdImage.Picture.Graphic).SaveToFile(format('%s%dface.jpg',[filepath,
splayer.PlayID]));
ADOStoredProc1.Parameters.ParamByName('@playidFaceImage').LoadFromFile(format('%s%dface.jpg',[filepath,
splayer.PlayID]), ftBlob);
end;
ADOStoredProc1.ExecProc;
end;
procedure TdlgPlayer.WorkingLoadImageData;
var
Filepath : string;
begin
FilePath := AddBackSlashL(g_TempFolder);
if FileExists(format('%s%dfull.jpg',[filepath, splayer.PlayID]))
then
imgFullIdImage.Picture.LoadFromFile(format('%s%dfull.jpg',[filepath,
splayer.PlayID]))
else
imgFullIdImage.Picture.Assign(nil);
if FileExists(format('%s%dface.jpg',[filepath, splayer.PlayID]))
then
imgFaceIdImage.Picture.LoadFromFile(format('%s%dface.jpg',[filepath,
splayer.PlayID]))
else
imgFullIdImage.Picture.Assign(nil);
end;
// ???Full_a.JPG = 2,045K in size and appears to be a BMP file
procedure TdlgPlayer.BrokenLoadImageData;
var
Filepath : string;
begin
FilePath := AddBackSlashL(g_TempFolder);
ADOStoredproc2.Parameters.ParamByName('@PlayID').Value :=
sPlayer.PlayID;
ADOStoredProc2.Open;
if ADOStoredProc2.FieldByName('playidFullImage').value <> null then
begin
TBlobField(ADOStoredProc2.FieldByName('playidFullImage')).SaveToFile(format('%s%dfull_a.jpg',[filepath,
splayer.PlayID]));
imgFullIdImage.Picture.LoadFromFile(format('%s%dfull_a.jpg',[filepath,
splayer.PlayID]));
end
else
imgFullIdImage.Picture.Assign(nil);
if FileExists(format('%s%dface.jpg',[filepath, splayer.PlayID]))
then
imgFaceIdImage.Picture.LoadFromFile(format('%s%dface.jpg',[filepath,
splayer.PlayID]))
else
imgFullIdImage.Picture.Assign(nil);
end;
CREATE procedure sp_PutPlayerIDData
@PlayID int,
@playidFullImage image,
@playidFaceImage image
as
-- Determine if we need to insert or update the data....
if exists(Select * from PlayerID where playid = @playID)
begin
-- Update the players data
Update playerid
set playID = @playID,
playIDFullImage = @playidFullImage,
playIDFaceImage = @playidFaceImage
end
else
begin
-- Insert a new row of data
insert into
PlayerID (playID,
playIDFullImage,
playIDFaceImage)
values (@playID,
@playidFullImage,
@playidFaceImage);
end
Return @@Error;
GO
CREATE PROCEDURE sp_rds_GetPlayerID
@PlayID integer
AS
select * from
PlayerID
where
playid = @playid;
Return @@error;
GO
.
- Follow-Ups:
- Re: Storing and Retreiving JPG images using ADO and StoredProcedures
- From: Del M
- Re: Storing and Retreiving JPG images using ADO and StoredProcedures
- From: Brian Bushay TeamB
- Re: Storing and Retreiving JPG images using ADO and StoredProcedures
- Prev by Date: Re: IDE no response
- Next by Date: Re: Performance issue
- Previous by thread: Help! changed data type and now cannot run project
- Next by thread: Re: Storing and Retreiving JPG images using ADO and StoredProcedures
- Index(es):