저의는 DB에 사진을 많이 넣어야 하는데요...
그 양이 많다보니 DB에 BMP를 넣기가 힘들어 졌어요...(메모리, 속도 때문...)
그래서 이제는 JPG로 입력을 할려고 하는데, 어떻게 해야할지..
많은 답변이 있지만, 잘 모르겠어요...
저의 DB서버는 MS-SQL이고요,, 입력해서 퀵리포트로 출력까지의 과정을 알려주시면 정말 감사하겠습니다.
BMP를 변환하는것이 아니고, 그냥 jpg화일을 넣고, 불러오고, 출력하고 싶습니다.
알려주세요...
벌써 1달째랍니다.
방법입니다. 밑에 소스는 웹서핑을 하다가 찾은 소스입니다. 보고 사용하세요.
약간만 응용하시면 곧바로 사용하실수 있습니다. 그럼.
Question/Problem/Abstract:
How to get binary data in a workable format into or out of an MSSQL Image (Blob) field using ADO components.
Answer:
The main problem I faced when trying to do this was to deal with the fact that TField.Value returns a varOleStr no matter
what was written into it, so the data needed to be converted into a more usable format.
Note that there is no checking here that the TField is in fact of the correct type, and that the
stream must be created and free-ed elsewhere manually. Also, additional memory equal to the size of the stream/blob is
required, so be cautious if large amounts of data are involved.
For ease of use in my own application, I incorporated this functionality into my descendent of TADOQuery.
function LoadFromBlob(const AField: TField; const Stream: TStream): boolean;
var
ResultStr: string;
PResultStr: PChar;
begin
Result := false;
if (Assigned(AField)) and (Assigned(Stream)) then begin
try
ResultStr := AField.Value;
PResultStr := PChar(ResultStr);
Stream.Write(PResultStr^, Length(ResultStr));
Stream.Seek(0,0);
Result := true;
except
end;
end;
end;
function SaveToBlob(const Stream: TStream; const AField: TField): boolean;
var
FieldStr: string;
PFieldStr: PChar;
begin
Result := false;
if (Assigned(AField)) and (Assigned(Stream)) then begin
try
Stream.Seek(0,0);
SetLength(FieldStr, Stream.Size);
PFieldStr := PChar(FieldStr);
Stream.Read(PFieldStr^, Stream.Size);
AField.Value := FieldStr;
Result := true;
except
end;
end;
end;
-------------------------------------------------------
Examples:
If you have an ADO query "qryBlobTest" with the following fields: nFileIcon: Image; nFileData: Image;
// Store an icon in an Image field
function StoreFileIcon: boolean;
var
AFileIcon: TIcon;
MS: TMemoryStream;
begin
Result := false;
AFileIcon := TIcon.Create;
MS := TMemoryStream.Create;
try
AFileIcon.handle := ExtractAssociatedIcon('c:tempTest.doc'); // Pseudocode !!
AFileIcon.SaveToStream(MS);
Result := SaveToBlob(MS, qryBlobTest.FieldByName('nFileIcon'));
finally
AFileIcon.Free;
MS.Free;
end;
end;
// Load an icon from an Image field
function LoadFileIcon: boolean;
var
AFileIcon: TIcon;
MS: TMemoryStream;
begin
Result := false;
AFileIcon := TIcon.Create;
MS := TMemoryStream.Create;
try
if (LoadFromBlob(qryBlobTest.FieldByName('nFileIcon'), MS)) then begin
AFileIcon.LoadFromStream(MS);
// Do something with the Icon?
Result := true;
end;
finally
AFileIcon.Free;
MS.Free;
end;
end;
// Save a binary file in an Image field
function StoreFileData: boolean;
var
FS: TFileStream;
begin
FS := TFileStream.Create('c:tempTest.doc', fmOpenRead);
Result := SaveToBlob(FS, qryBlobTest.FieldByName('nFileData'));
FS.Free;
end;
// Load a file from an Image field (save it to a file name)
function LoadFileData: boolean;
var
FS: TFileStream;
begin
FS := TFileStream.Create('c:tempTest2.doc', fmCreate);
LoadFromBlob(qryBlobTest.FieldByName('nFileData'), FS);
FS.Free;
end;