How to save jpg image
Posted: 27.09.2005, 08:10
Part 1: Insert Image to Blob Field Database,
Use Mysql 4.0.26 +zeoslib 6.15
----------
[syntax="delphi"]with dmdbConnPool.q2 do begin
SQL.clear;
SQL.Add('update employee set PHOTO=:iPic');
SQL.Add('where employeeid=:iEmp');
ParamByName('iEmp').AsString:=dbEmployeeID.text;
if Uppercase(ExtractFileExt(DBEdit14.Text))='.JPG' then begin
Picture:=TPicture.Create;
Picture.LoadFromFile(DBEDIT14.text);
Stream:=TMemoryStream.Create;
TJPEGImage(Picture.Graphic).SaveToStream(Stream);
ParamByName('iPic').SetBlobData(Stream.Memory,Stream.Size);
ExecSQL;
end
else if Uppercase(ExtractFileExt(DBEdit14.Text))='.BMP' then begin
JPG.Assign(Image1.Picture.Graphic);
JPG.CompressionQuality:=70;
JPG.SaveToStream(Stream);
ParamByName('iPic').SetBlobData(Stream.Memory,Stream.Size);
ExecSQL;
Showmessage('BITMAP are saved success : '+DBEdit14.Text);
end;
end;
end; // end of the If Course
Except
on E : Exception do begin
ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
ShowMessage('NOT SUCCESS TO UPLOAD IMAGE');
Exit;
end; // end of the Exception
end;
finally
JPG.Free;
Stream.Free;
end; // end of the finally try ( First Try )[/syntax]
==========
Part 2: query afterscroll method, to view image
[syntax="delphi"]with dmdbConnPool.q3 do begin
SQL.clear;
SQL.Add('select * from employee ');
SQL.Add('where Employeeid=:Employeeid');
ParamByName('Employeeid').AsString := EmpAdmin.dbEmployeeID.text;
ExecSQL;
open;
if not FieldByName('photo').IsNull then
try
MS:=TStringStream.Create('');
TBlobField(FieldByName('PHOTO')).SaveToStream(MS);
MS.Position:=0;
jpeg:=TJPEGImage.Create;
jpeg.LoadFromStream(MS);
EmpAdmin.Image1.Picture.Bitmap.Assign(jpeg);
finally
jpeg.Free;
ms.Free;
end // end of the try block
else begin
EmpAdmin.image1.Picture :=nil;
end // end of the begin
end[/syntax]
==========
If i use blobstream to save image to mysql, I don't know how to programming. If possible give me a hint, Why I save jpg while get
half of image, but bitmap now problem. I useing BLOB filed in my database. I think I part 1 have problem during save jpg, any change I can fix the jpg problem.
Thank you very much and.
[edit="Michael"]I put your code into syntax-tags - looks better now [/edit]
Use Mysql 4.0.26 +zeoslib 6.15
----------
[syntax="delphi"]with dmdbConnPool.q2 do begin
SQL.clear;
SQL.Add('update employee set PHOTO=:iPic');
SQL.Add('where employeeid=:iEmp');
ParamByName('iEmp').AsString:=dbEmployeeID.text;
if Uppercase(ExtractFileExt(DBEdit14.Text))='.JPG' then begin
Picture:=TPicture.Create;
Picture.LoadFromFile(DBEDIT14.text);
Stream:=TMemoryStream.Create;
TJPEGImage(Picture.Graphic).SaveToStream(Stream);
ParamByName('iPic').SetBlobData(Stream.Memory,Stream.Size);
ExecSQL;
end
else if Uppercase(ExtractFileExt(DBEdit14.Text))='.BMP' then begin
JPG.Assign(Image1.Picture.Graphic);
JPG.CompressionQuality:=70;
JPG.SaveToStream(Stream);
ParamByName('iPic').SetBlobData(Stream.Memory,Stream.Size);
ExecSQL;
Showmessage('BITMAP are saved success : '+DBEdit14.Text);
end;
end;
end; // end of the If Course
Except
on E : Exception do begin
ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
ShowMessage('NOT SUCCESS TO UPLOAD IMAGE');
Exit;
end; // end of the Exception
end;
finally
JPG.Free;
Stream.Free;
end; // end of the finally try ( First Try )[/syntax]
==========
Part 2: query afterscroll method, to view image
[syntax="delphi"]with dmdbConnPool.q3 do begin
SQL.clear;
SQL.Add('select * from employee ');
SQL.Add('where Employeeid=:Employeeid');
ParamByName('Employeeid').AsString := EmpAdmin.dbEmployeeID.text;
ExecSQL;
open;
if not FieldByName('photo').IsNull then
try
MS:=TStringStream.Create('');
TBlobField(FieldByName('PHOTO')).SaveToStream(MS);
MS.Position:=0;
jpeg:=TJPEGImage.Create;
jpeg.LoadFromStream(MS);
EmpAdmin.Image1.Picture.Bitmap.Assign(jpeg);
finally
jpeg.Free;
ms.Free;
end // end of the try block
else begin
EmpAdmin.image1.Picture :=nil;
end // end of the begin
end[/syntax]
==========
If i use blobstream to save image to mysql, I don't know how to programming. If possible give me a hint, Why I save jpg while get
half of image, but bitmap now problem. I useing BLOB filed in my database. I think I part 1 have problem during save jpg, any change I can fix the jpg problem.
Thank you very much and.
[edit="Michael"]I put your code into syntax-tags - looks better now [/edit]