loucks
asked on
JPEG into BLOB field NO FILE WRITES
Here is my code:
msPic := TMemoryStream.Create;
try
s := y.Result.pic;
msPic.WriteBuffer(pointer( s)^, length(s));
if msPic.Size > 0 then
begin
jpg := TJpegImage.Create;
msPic.Position := 0;
jpg.LoadFromStream(msPic);
dbiPhoto.Picture.Assign(jp g);
dbiPhoto.Width := Round(dbiPhoto.Height * jpg.Width / jpg.Height);
end;
finally
msPic.Free;
end;
This works fine. dbiPhoto though, is a TDBImage, and I want it to save it to the database.
How do I do it?
Obviously, it has to be in edit mode and such. I have tried many things but nothing worked.
So basically, the question is. How do I go from my JPEG into the database for display in the TDBImage component at a later (and current) time? I can easily make it do:
jpeg.savetofile and tdbimage.loadfromfile, but I want to do it from a stream. I am beginning to think it is impossible.
You need more points, just let me know.
msPic := TMemoryStream.Create;
try
s := y.Result.pic;
msPic.WriteBuffer(pointer(
if msPic.Size > 0 then
begin
jpg := TJpegImage.Create;
msPic.Position := 0;
jpg.LoadFromStream(msPic);
dbiPhoto.Picture.Assign(jp
dbiPhoto.Width := Round(dbiPhoto.Height * jpg.Width / jpg.Height);
end;
finally
msPic.Free;
end;
This works fine. dbiPhoto though, is a TDBImage, and I want it to save it to the database.
How do I do it?
Obviously, it has to be in edit mode and such. I have tried many things but nothing worked.
So basically, the question is. How do I go from my JPEG into the database for display in the TDBImage component at a later (and current) time? I can easily make it do:
jpeg.savetofile and tdbimage.loadfromfile, but I want to do it from a stream. I am beginning to think it is impossible.
You need more points, just let me know.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well, I guess that is it then... It seems like borland would support JPEG's in TDBImage, but what do I know... :)
as Meikl said, you can't use dbimage to store jpeg image in the database, but you can find some free components to replace the dbimage with one support the jpg, look at torry site
http://www.torry.net/db_images.htm
if u still would like to use file stream, here's my code, but insure that u added all the fields first in field editor
==
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
DBCtrls, StdCtrls, Mask, Db, DBTables, ExtCtrls, Grids, DBGrids,Jpeg;
type
TForm1 = class(TForm)
Button1: TButton;
Image1: TImage;
Table1: TTable;
DataSource1: TDataSource;
Table1PIC: TBlobField;
DBNavigator1: TDBNavigator;
OpenDialog1: TOpenDialog;
DBGrid1: TDBGrid;
Table1ID: TStringField;
Label1: TLabel;
DBEdit1: TDBEdit;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure DataSource1DataChange(Send
private
{ Private declarations }
FileName : String;
procedure ReadPicFromDB;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.ReadPicFromDB;
var
FileStream : TFileStream;
BlobStream : TBlobStream;
begin
Image1.Picture.Assign(nil)
BlobStream := TBlobStream.Create(Table1P
if BlobStream.Size =0 then
begin
BlobStream.Free;
Exit;
end;
if FileName <> '' then
DeleteFile(FileName);
FileName := 'Img.jpg';
FileStream := TFileStream.Create(FileNam
FileStream.CopyFrom(BlobSt
FileStream.Free;
BlobStream.Free;
Image1.Picture.LoadFromFil
end;// end procedure radpicfromdb
procedure TForm1.Button1Click(Sender
var
FileStream : TFileStream;
BlobStream : TBlobStream;
begin
Application.ProcessMessage
if OpenDialog1.Execute then
begin
Button1.Enabled := false;
FileStream := TFileStream.Create(OpenDia
Table1.Edit;
BlobStream := TBlobStream.Create(Table1P
BlobStream.Seek(0,soFromBe
BlobStream.Truncate;
BlobStream.CopyFrom(FileSt
FileStream.Free;
BlobStream.Free;
Table1.Post;
ReadPicFromDB;
Button1.Enabled := True;
end;
end;
procedure TForm1.FormDestroy(Sender:
begin
if FileName <> '' then
DeleteFile(FileName);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
OpenDialog1.Filter := 'Jpeg Files |*.jpg';
OpenDialog1.Options := [ofHideReadOnly, ofNoChangeDir];
Image1.Stretch := true;
end;
procedure TForm1.DataSource1DataChan
begin
if DataSource1.State = dsBrowse then
ReadPicFromDb;
end;
Best regards
Mohammed Nasman