Solved

JPEG into BLOB field NO FILE WRITES

Posted on 2002-07-17
3
246 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
ID: 7161246
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:Mohammed Nasman
ID: 7161489
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
ID: 7162072
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

920 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

16 Experts available now in Live!

Get 1:1 Help Now