Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Storing BLOB Fields and calling BLOB Fields

Posted on 2000-03-23
3
Medium Priority
?
214 Views
Last Modified: 2010-04-04
Dear Sir,
May I know how to store pictures into a table and recall it from the table. Please show me by example. And what format of picture does Delphi accepts?
0
Comment
Question by:antioch_75
[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

Expert Comment

by:kretzschmar
ID: 2650371
hi antioch,

tell us for what
database you need this.

meikl
0
 
LVL 17

Accepted Solution

by:
inthe earned 200 total points
ID: 2650561
for wmf i find this at borland.com:
FAQ878D.txt Save and load metafiles in a BLOB field without using DBImage
Category :VCL
Platform :All
Product :All 32 bit

Question:
How can I save and load metafiles in a BLOB field without using the
DBImage component?


Answer:
The following example demonstrates saving metafile images to a
database as they exist on the disk, preserving any Placeable metafile
headers that may be present in the Metafile. The image is displayed
from the database in a TImage component.

Example:

unit Unit1;

interface

{$IFDEF WIN32}
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, DBCtrls, Grids, DBGrids, Db,
DBTables;
{$ELSE}
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, DBTables, DB, Grids, DBGrids, ExtCtrls, StdCtrls;
{$ENDIF}

type
TForm1 = class(TForm)
Table1: TTable;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Image1: TImage;
Button1: TButton;
Table1Name: TStringField;
Table1WMF: TBlobField;
OpenDialog1: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure DataSource1DataChange(Sender: TObject; Field: TField);
private
{ Private declarations }
FileName : string; {Used to hold a temp file name}
procedure LoadWMFFromDatabase; {loads a WMF from the database}
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
{Used for loading metafiles}
OpenDialog1.Filter := 'Metafiles (*.wmf)|*.wmf';
OpenDialog1.Options := [ofHideReadOnly, ofNoChangeDir];
Image1.Stretch := true;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
{Erase the temp file if it exists}
if FileName <> '' then
DeleteFile(FileName);
end;

{This function gets a temporary file name form the system}
function GetTemporaryFileName : string;
{$IFNDEF WIN32}
const MAX_PATH = 144;
{$ENDIF}
var
{$IFDEF WIN32}
lpPathBuffer : PChar;
{$ENDIF}
lpbuffer : PChar;
begin
{Get the file name buffer}
GetMem(lpBuffer, MAX_PATH);
{$IFDEF WIN32}
{Get the temp path buffer}
GetMem(lpPathBuffer, MAX_PATH);
{Get the temp path}
GetTempPath(MAX_PATH, lpPathBuffer);
{Get the temp file name}
GetTempFileName(lpPathBuffer,
'tmp',
0,
lpBuffer);
{Free the temp path buffer}
FreeMem(lpPathBuffer, MAX_PATH);
{$ELSE}
{Get the temp file name}
GetTempFileName(GetTempDrive('C'),
'tmp',
0,
lpBuffer);
{$ENDIF}
{Create a pascal string containg}
{the temp file name and return it}
result := StrPas(lpBuffer);
{Free the file name buffer}
FreeMem(lpBuffer, MAX_PATH);
end;

procedure TForm1.LoadWMFFromDatabase;
var
FileStream: TFileStream; {a temp file}
BlobStream: TBlobStream; {the WMF Blob}
begin
Image1.Picture.Metafile.Assign(nil);
{Create a blob stream for the WMF blob}
BlobStream := TBlobStream.Create(Table1WMF, bmRead);
if BlobStream.Size = 0 then begin
BlobStream.Free;
Exit;
end;
{if we have a temp file then erase it}
if FileName <> '' then
DeleteFile(FileName);
{Get a temp file name}
FileName := GetTemporaryFileName;
{Create a temp file stream}
FileStream := TFileStream.Create(FileName,
fmCreate or fmOpenWrite);
{Copy the blob to the temp file}
FileStream.CopyFrom(BlobStream, BlobStream.Size);
{Free the streams}
FileStream.Free;
BlobStream.Free;
{Dispaly the image}
Image1.Picture.Metafile.LoadFromFile(FileName);
end;

{Save a wmf file to the database}
procedure TForm1.Button1Click(Sender: TObject);
var
FileStream: TFileStream; {to load the wmf file}
BlobStream: TBlobStream; {to save to the blob}
begin
{Allow the button to repaint}
Application.ProcessMessages;
if OpenDialog1.Execute then begin
{Turn off the button}
Button1.Enabled := false;
{Assign the avi file name to read}
FileStream := TFileStream.Create(OpenDialog1.FileName,
fmOpenRead);
Table1.Edit;
{Create a BlobStream for the field Table1WMF}
BlobStream := TBlobStream.Create(Table1WMF, bmReadWrite);
{Seek to the Begginning of the stream}
BlobStream.Seek(0, soFromBeginning);
{Delete any data that may be there}
BlobStream.Truncate;
{Copy from the FileStream to the BlobStream}
BlobStream.CopyFrom(FileStream, FileStream.Size);
{Free the streams}
FileStream.Free;
BlobStream.Free;
{Post the record}
Table1.Post;
{Load the metafile in to a TImage}
LoadWMFFromDatabase;
{Enable the button}
Button1.Enabled := true;
end;
end;

procedure TForm1.DataSource1DataChange(Sender: TObject; Field: TField);
begin
if (Sender as TDataSource).State = dsBrowse then
LoadWMFFromDatabase;
end;

end.



for bmp files
Table1YourBlob.LoadFromFile('c:\windows\scary.bmp');
 
or Another way is to create a TFileStream object to access the file. Use the TBlobField.LoadFromStream method to copy from stream to BLOB column.

example for jpegs:

procedure frmMain.msImportJpeg;
var
lFile:  string;
lFileStream: TFileStream;
lPicStream:  TBlobStream;
ljpeg:  TJPEGImage;
begin
lFile := 'c:\somejpeg.jpg';
lFileStream := TFileStream.Create(lFile,fmOpenRead);
lFileStream.position :=0;
ljpeg := TJPEGImage.create;
ljpeg.LoadFromStream(lFileStream);
imgImport.picture.Graphic := ljpeg;
tblPicture.append;
lPicStream :=
TBlobStream.Create(TBLOBField(tblPicture.FieldByName('PICTURE')),bmReadWrite);
lPicStream.Position := 0;
lPicStream.Truncate;
lFileStream.Position :=0;
lPicStream.CopyFrom(lFileStream, lFileStream.Size);
lPicStream.position := 0;
ljpeg.free;
lFileStream.free;
lPicStream.free;
tblPicture.post;
end;



epsylon if you read this ,i was so tempted .. hehehhehe ;-)) :o)

0
 

Expert Comment

by:sateesh09
ID: 2660301
I have a simple answer for this. But it is not implementable b'coz it is like hardcode.

1. Take dbRichedit component  and connect to the image field in the table.

2. To insert any image first get that image into clipboard.

3. You have property 'CopyFromClipboardToBlob' for dbRichedit component. so u can insert image into database.

4. But for retriving it will show like junk characters in TdbRichedit component.

All the best,
Sateesh
sateesh09@usa.net
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

715 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