Solved

JPEG into BLOB field NO FILE WRITES

Posted on 2002-07-17
3
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

729 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