Solved

JPEG into BLOB field NO FILE WRITES

Posted on 2002-07-17
3
243 Views
Last Modified: 2010-04-04
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(jpg);
       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.
0
Comment
Question by:loucks
3 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 300 total points
Comment Utility
from my paq

following a sample how to store jpg-images into paradox,
but is applyable to other database.

by the way, you can't use tdbimage with jpg,
use a timage instead

//Sample unit for Storing JPG-Images in the JPeg-Format
//in a BlobField in Paradox
//It is recommended, that the BlobField in Paradox is
//Typ Binary, not Graphic, because the file will be stored
//Appendix: Its a sample, therefore no try, except, finally blocks included

unit db_pict_u;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables, ExtCtrls, DBCtrls, StdCtrls, Mask, Menus, jpeg; // JPeg unit is used

type
TForm1 = class(TForm)
  DBEdit1: TDBEdit;   // A other DBField
  DBNavigator1: TDBNavigator;
  Table1: TTable;
  DataSource1: TDataSource;
  PopupMenu1: TPopupMenu;     // a Popup linked to Image1
  LoadPicture1: TMenuItem;    // the MenuItem of the Popup
  OpenDialog1: TOpenDialog;   // OpenDialog
  Image1: TImage;             // Visual Output for the JPeg
  procedure FormCreate(Sender: TObject);
  procedure FormDestroy(Sender: TObject);
  procedure LoadJPEG1Click(Sender: TObject);      // File Load and Store in DB
  procedure Table1AfterScroll(DataSet: TDataSet); // Get JPeg and Display in Image1
private
  { Private-Deklarationen }
public
  { Public-Deklarationen }
end;

var
Form1: TForm1;
JPegImage : TJpegImage;  //A temporary JPeg

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
JpegImage := TJPegImage.Create; // Create JPeg-Object
Table1.Open;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
Table1.Close;
JPegImage.Free;  // Free JPeg-Object
end;


//Store
procedure TForm1.LoadJPEG1Click(Sender: TObject);
Var
MyBlobStream : TBlobStream;      // Streams
MyJPegStream : TMemoryStream;
begin
if (Table1.State = dsEdit) or (Table1.State = dsInsert) then    // Do only if in edit mode
begin
  If opendialog1.Execute then    // If file selected (only JPeg-Files)
  begin
    if Table1.FieldByName('Bild2').IsBlob then   // this line can deleted
    begin
      MyJPegStream := TMemoryStream.Create;      // Handle JPegFile
      JPegImage.LoadFromFile(opendialog1.filename);
      JPegImage.SaveToStream(MyJPegStream);
      // Prepare Blob
      MyBlobStream := TBlobStream.Create(TBlobField(Table1.FieldByName('Bild2')),bmReadWrite);
      MyBlobStream.CopyFrom(MyJPegStream,0);  // Copy JPeg into BlobField
      Image1.Picture.Assign(JPegImage);  // Display
      MyJPegStream.Free;  //Free all
      MyBlobStream.Free;
    end;
  end;
end else ShowMessage('Table is not in Edit Mode!');
end;

//Get
procedure TForm1.Table1AfterScroll(DataSet: TDataSet);
Var
MyBlobStream : TBlobStream;
begin
// Do Only if a JPeg available
if (Table1.FieldByName('Bild2').IsBlob) and (not(Table1.FieldByName('Bild2').IsNull)) then
begin
  // Prepare Streams
  MyBlobStream := TBlobStream.Create(TBlobField(Table1.FieldByName('Bild2')),bmRead);
  JPegImage.LoadFromStream(MyBlobStream);
  Image1.Picture.Assign(JPegImage);
  MyBlobStream.Free;  // Free Stream
end
else image1.Picture := Nil;  // No JPeg saved disable display
end;

end.

meikl ;-)
0
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
Hello

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(Sender: TObject; Field: TField);
  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(Table1Pic, bmRead);
  if BlobStream.Size =0 then
  begin
    BlobStream.Free;
    Exit;
  end;
  if FileName <> '' then
    DeleteFile(FileName);
  FileName := 'Img.jpg';
  FileStream := TFileStream.Create(FileName, fmCreate or fmOpenWrite);
  FileStream.CopyFrom(BlobStream,BlobStream.Size);
  FileStream.Free;
  BlobStream.Free;
  Image1.Picture.LoadFromFile(FileName);
end;// end procedure radpicfromdb

procedure TForm1.Button1Click(Sender: TObject);
var
  FileStream : TFileStream;
  BlobStream : TBlobStream;
begin
  Application.ProcessMessages;
  if OpenDialog1.Execute then
  begin
    Button1.Enabled := false;
    FileStream := TFileStream.Create(OpenDialog1.FileName, fmOpenRead);
    Table1.Edit;
    BlobStream := TBlobStream.Create(Table1Pic,bmReadWrite);
    BlobStream.Seek(0,soFromBeginning);
    BlobStream.Truncate;
    BlobStream.CopyFrom(FileStream,FileStream.Size);
    FileStream.Free;
    BlobStream.Free;
    Table1.Post;
    ReadPicFromDB;
    Button1.Enabled := True;
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
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.DataSource1DataChange(Sender: TObject; Field: TField);
begin
  if DataSource1.State = dsBrowse then
    ReadPicFromDb;
end;

Best regards
Mohammed Nasman
0
 

Author Comment

by:loucks
Comment Utility
Well, I guess that is it then...  It seems like borland would support JPEG's in TDBImage, but what do I know... :)

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now