Board index » delphi » save JPG, GIF, BMP?

save JPG, GIF, BMP?

Quote
>I have this function to store a bitmap into a blob field. Can anyone modify
>it to store other formats of pictures such as (jpg, gif, bmp...etc)?

The Tjpeg has a SaveToStream method and TGif components I have seen also have a
saveToStream method.  So if the paramater you are passing is a Tjpeg or Tgif the
code you have can remain the same except to change the type of parameter

procedure InsertImageIntoField(FieldName: TParam; Image: TJpeg);
var
    TempStream : TMemoryStream;
begin
    TempStream := TMemoryStream.Create;
    try
        Image.SaveToStream(TempStream);
        TempStream.Position := 0;
        FieldName.LoadFromStream(TempStream, ftBlob);
    finally
        FreeAndNil(TempStream);
    end;
end;
--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

 

Re:save JPG, GIF, BMP?


thanx @ lot,

I cannot seem to find Tjpeg and TGif. So, I replaced the procedure's
parameter with TJPEGImage. There is no error when saving to blob, but when I
try to view the blob field, no image is there.

procedure TForm1.Button1Click(Sender: TObject);
begin
    IBQuery1.Close;
    IBQuery1.SQL.Clear;
    IBQuery1.SQL.Add := 'insert into TABLE1(FIELD1)';
    IBQuery1.SQL.Add := 'values(:I_FIELD1)';
    InsertImageIntoField(IBQuery1.Params.ParamByName('I_FIELD2'),
TJPEGImage(Image1.Picture.Bitmap));
    IBQuery1.ExecSQL;
end;

procedure InsertImageIntoField(FieldName: TParam; Image: TJPEGImage);
var
    TempStream : TMemoryStream;
begin
    TempStream := TMemoryStream.Create;
    try
        Image.SaveToStream(TempStream);
        TempStream.Position := 0;
        FieldName.LoadFromStream(TempStream, ftBlob);
    finally
        FreeAndNil(TempStream);
    end;
end;

Re:save JPG, GIF, BMP?


Yet, another problem,
If I put the InsertImageIntoField procedure in a DLL, it doesn't seem to
work at all. It will work fine in normal Units but in a DLL, it doesn't seem
to be able to save the Bitmap.

IN DLL
---------
procedure InsertImageIntoField(FieldName: TParam; Image: TImage); stdcall;
var
  TempStream: TMemoryStream;
begin
  TempStream := TMemoryStream.Create;
  try
     Image.Picture.Bitmap.SaveToStream(TempStream);
     TempStream.Position := 0;
     FieldName.LoadFromStream(TempStream, ftBlob);
  finally
     FreeAndNil(TempStream);
  end;
end;

exports
  InsertImageIntoField;

PROGRAM
----------------
implementation

procedure InsertImageIntoField(FieldName: TParam; Image: TImage); stdcall;
external 'DllFile.dll';

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  IBQuery1.Close;
  IBQuery1.SQL.Clear;
  IBQuery1.SQL.Add('insert into TABLE1(FIELD1, FIELD2)');
  IBQuery1.SQL.Add('values(:I_FIELD1, :I_FIELD2)');
  IBQuery1.ParamByName('I_FIELD1').AsString := 'A';
  InsertImageIntoField(IBQuery1.Params.ParamByName('I_FIELD2'), Image1);
  IBQuery1.ExecSQL;
end;

thanx @ lot

Re:save JPG, GIF, BMP?


Quote
>I cannot seem to find Tjpeg and TGif. So, I replaced the procedure's
>parameter with TJPEGImage. There is no error when saving to blob, but when I
>try to view the blob field, no image is there.

Delphi includes a Jpeg unit that gives you a TjpegImage component.  This is not
a viewable component.  You can use a Timage to view a Tjeg
Delphi does not come with a Tgif component.  You will need to find a 3rd party
component to do that.

You can use code like this to load a jpeg from a field and then display it in a
TImage

procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
var
  MS: TMemoryStream;
  J1: TJPEGImage;
begin
  J1 := TJPEGImage.Create;
  MS := TMemoryStream.Create;
  try
    TBlobField(DataSet.Fieldbyname('myBlob')).SaveToStream(MS);
    MS.Seek(0,soFromBeginning);
    with J1 do begin
      PixelFormat := jf24Bit;
      Scale := jsFullSize;
      Grayscale := False;
      Performance := jpBestQuality;
      ProgressiveDisplay := True;
      ProgressiveEncoding := True;
      LoadFromStream(MS);
    end;
    if MS.Size >0 then
      Image1.Picture.Assign(J1)
    else
      Image1.Picture.Assign(nil);

  finally
    J1.Free;
    MS.Free;
  end;
end;

--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Re:save JPG, GIF, BMP?


thanx @ lot (Brian Bushay TeamB),

But, what i really need to know is how to modify my procedure to "store"
JPEG in a blob field. My problem is that when I try to save JPEG, it will
not raise any errors, but when viewed nothing is there.

Re:save JPG, GIF, BMP?


Quote
>But, what i really need to know is how to modify my procedure to "store"
>JPEG in a blob field. My problem is that when I try to save JPEG, it will
>not raise any errors, but when viewed nothing is there.

Your questions do not make much sense so I am not surprised if you do not get
the answers you are looking for.
If a the jpeg you refer to is a file then just use the tblobfield.loadfromFile
method.

--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Re:save JPG, GIF, BMP?


I had the same problem using Paradox 7 tables, and the graphic field type
due to the fact that the graphic field type wants only bitmaps. I dont know
if this will help, and have not tested it on other BLOB data types, but it
should work.

This allowed me to use the TDBImage component, the native JPEG support, and
a third-party gif unit for gif support.

You start by loading the graphic into a TImage, The TImage will handle all
the graphic types you make your unit aware of.
Pass the graphic from a TImage to a TBitmap for a fast clean conversion,
then assign it to the field object.

Here is a sample of the code...

Procedure .....
Var
  Image: TImage;
  Bitmap: TBitmap;
begin
  If OpenDialog.Execute then
    Begin
      Image := TImage.Create(Self);
      Bitmap := TBitmap.Create;
      Image.Picture.LoadFromFile(OpenDialog.FileName);
      Bitmap.Assign(Image.Picture.Graphic);
      Table_ImagesData.Assign(Bitmap);
    end;
  Image.Free;
  Bitmap.Free;
end;

Hope this helps.
Michael Gallaher
m...@asipay.com

Chua Wen Kiat <wkc...@chrysanth.com> wrote in message
news:39a481eb_2@dnews...

Quote
> I have this function to store a bitmap into a blob field. Can anyone
modify
> it to store other formats of pictures such as (jpg, gif, bmp...etc)?

> procedure TForm1.Button1Click(Sender: TObject);
> begin
>     IBQuery1.Close;
>     IBQuery1.SQL.Clear;
>     IBQuery1.SQL.Add := 'insert into TABLE1(FIELD1)';
>     IBQuery1.SQL.Add := 'values(:I_FIELD1)';
>     InsertImageIntoField(IBQuery1.Params.ParamByName('I_FIELD2'), Image1);
>     IBQuery1.ExecSQL;
> end;

> procedure InsertImageIntoField(FieldName: TParam; Image: TImage);
> var
>     TempStream : TMemoryStream;
> begin
>     TempStream := TMemoryStream.Create;
>     try
>         Image.Picture.Bitmap.SaveToStream(TempStream);
>         TempStream.Position := 0;
>         FieldName.LoadFromStream(TempStream, ftBlob);
>     finally
>         FreeAndNil(TempStream);
>     end;
> end;

Other Threads