0

For some reason I cannot find useful resources to pinpoint out how to do this.

   Picture : TPicture;
   Bitmap: TBitmap;
   ---------------------------
   Picture := TPicture.Create;
   try
          Picture.LoadFromFile('path\to\file\file.jpg');
          Bitmap := TBitmap.Create;

          try
                 Bitmap.Width := Picture.Width;
                 Bitmap.Height := Picture.Height;
                 Bitmap.Canvas.Draw(0, 0, Picture.Graphic);

                 with dtm.cds do //Data module and client data set
                 begin
                     //TODO       
                 end;
          finally
                 Bitmap.Free;
          end;
   finally
          Picture.Free;
   end;

Now I have a bitmap, next I have created a Stream:TStream object and a blobfield :TBlobField Object as well but not sure how to connect the chain

Pardon my ignorance when it comes to this, I am new to delphi and not really sure how approach this issue.

Thanks in advance.

EDIT**** Now this is what I have, but still not getting an image

Picture := TPicture.Create;
   try
          Picture.LoadFromFile('path\to\file\file.jpg');
          Bitmap := TBitmap.Create;

          try
                 Bitmap.Width := Picture.Width;
                 Bitmap.Height := Picture.Height;
                 Bitmap.Canvas.Draw(0, 0, Picture.Graphic);

                 with dtm.cds do //Data module and client data set
                 begin
                     edit  ;
                     myStream := dtm.cdsr.CreateBlobStream(dtm.cds.FieldByName('PICTURE'),bmWrite);
                     Bitmap.SaveToStream(myStream);
                     post;
                     ApplyUpdates(-1);
                 end;
          finally
                 Bitmap.Free;
          end;
   finally
          Picture.Free;
   end;

This is how I am displaying iamges, which is working fine for the ones currently in the database

jpg.Free;
       stream.Free;
       img.Free;


       Stream := dtm.cds.CreateBlobStream(dtm.cds.FieldByName('PICTURE'), bmRead);
       JPG := TJPEGImage.Create;

       jpg.LoadFromStream(Stream);

       //The position of the image on the form.
       Img := TImage.Create(Self);
       Img.Parent := Self;
       Img.Left := 160;
       Img.Top := 4;
       Img.AutoSize := True;
       Img.Stretch := True;
       img.Picture.Assign(jpg);
LionsFan
  • 119
  • 1
  • 10
  • Call `MyStream := dtm.cds.CreateBlobStream(BlobField, bmWrite);`. That will create BLOB stream (`MyStream` is declared as `TStream` variable). Then `Bitmap.SaveToStream(MyStream)` for writing your bitmap to the BLOB stream. Finally release the stream. Oh, and here is [an example](https://stackoverflow.com/a/26103631/8041231). – Victoria Apr 06 '18 at 20:03
  • Thank you, I've tried that example you quoted but I will try that and will get back to you. Again thanks. – LionsFan Apr 06 '18 at 20:17
  • Some useful resources: https://stackoverflow.com/questions/13863169/load-and-save-image-from-blob-field-in-delphi-using-firebird https://stackoverflow.com/questions/11773986/how-to-insert-image-into-database-using-tadoquery-component-only https://stackoverflow.com/questions/26103437/delphi-load-image-save-as-blob-in-a-sql-database http://docwiki.embarcadero.com/CodeExamples/en/FishFacts_(Delphi) – Sertac Akyuz Apr 06 '18 at 20:27
  • Thank you guys, I have updated the post above, please take a look. @SertacAkyuz Thank you for the links, I will go through them and see if I can find something. – LionsFan Apr 06 '18 at 20:39
  • I think that for displaying image you must use Stream := dtm.cds.CreateBlobStream(dtm.cds.FieldByName('PICTURE'), bmRead); Stream.Position:= 0; jpg.LoadFromStream(Stream); – MSB Apr 07 '18 at 10:16
  • @BMS I am using that, I didnt have stream.position := 0, i added it and still not working. – LionsFan Apr 09 '18 at 12:25

1 Answers1

1

i use this functions: to Load image into blob field :

function LoadToJPG(FName: String; var JPG: TJPEGImage): Boolean;
var Bmp: Graphics.TBitmap;
begin
  Bmp := Graphics.TBitmap.Create;
  try
    try
      Bmp := LoadGraphicsFile(FName);
    except
      Bmp.FreeImage;
    end;
    if not Bmp.Empty then JPG.Assign(Bmp);
  finally
    Bmp.Free;
  end;

  Result:= not JPG.Empty;
end;

function ChangeImage(DS: TDataSet; FName: String= ''; FieldName: string =
    'PHOTO'): boolean;
var JPG: TJPEGImage;
begin
  Result := False;
  if FName='' then FName:= 'path\to\file\file.jpg';

  if (FName<>'') then
  begin
    JPG:=TJPEGImage.Create;
    try
      LoadToJPG(FName, JPG);
      begin
        if not DS.Active then DS.Active:= true;
        if DS.RecordCount = 0 then DS.Append;

        TryEdit(DS);
        (DS.FieldByName(FieldName) as TBlobField).Assign(JPG);
        DS.Post;
        Result := True;
      end;
    finally
      JPG.Free;
    end;
  end;
end;

and to load image from dataset i use this (http://stackoverflow.com/questions/14726756/retrieve-image-saved-on-database)

function BlobDataToImage(const AField: TBlobField): TGraphic;
var Ms: TMemoryStream;
begin
  Result:= nil;
  Ms:= TMemoryStream.Create;
  AField.SaveToStream(Ms);
  Try
    Result:= StreamToBmp(Ms);
  Finally
    ms.Free;
  End;
end;

function BlobDataToBmp(const AField: TBlobField): TBitmap;
var AGraphics: TGraphic;
    Bitmap: TBitmap;
begin
  Result:= nil;
  AGraphics:= BlobDataToImage(AField);
  if AGraphics<>nil then
  begin
    Bitmap:= TBitmap.Create;
    try
      Bitmap.Width:= AGraphics.Width;
      Bitmap.Height:= AGraphics.Height;
      Bitmap.Canvas.Draw(0, 0, AGraphics);
      Result:= Bitmap;
    except
      Bitmap.Assign(nil); Bitmap.free;
    end;
  end;
end;
MSB
  • 181
  • 1
  • 2
  • 9