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

x
?
Solved

Loading a JPEG from DB

Posted on 2003-03-09
5
Medium Priority
?
310 Views
Last Modified: 2010-04-04
Hi.

I have been trying to load a JPEG image (blob) from an Access database into a TImage. Sometimes it works, sometimes not.

Now, how is the proper way of loading a blob Jpeg into a TImage?


I discovered that sometimes TImage.Picture.Assign(FieldByName('Image')); works, and sometimes TImage.Picture.Graphic.Assign(FieldByName('Image'));


Help.
0
Comment
Question by:sageryd
[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
5 Comments
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 8099649
I haven't tried it, but could you create a TBlobField, then stream-copy the contents of the field to a TJPEGImage?

I don't have access here, but I'm sure there must be an easy way of doing it.  I'll try it later when I'm infront of a PC with Access on it.

Stu
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 600 total points
ID: 8101211
var
  m : tmemoryStream;
  jpg : TJpegImage;
begin
  m   := TMemoryStream.Create;
  jpg := TJpegImage.Create;
  try
    TBlobField(Dataset.FieldByName('FieldName')).SaveToStream(m);
    m.position := 0;
    jpg.LoadFormStream(m);
    image1.picture.bitmap.assign(jpg);
  finally
    m.free;
    jpg.free;
  end;
end;

meikl ;-)  
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8102198
from my PAQ (http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20532935.html)


uses jpeg;

procedure TForm1.btnSaveClick(Sender: TObject);
begin
 Query1.SQL.Text := 'INSERT INTO htest (img) values (:img)';
 Query1.ParamByName('img').LoadFromFile('D:\My.jpg', ftGraphic);
 Query1.ExecSQL;
end;

procedure TForm1.btnLoadClick(Sender: TObject);
var
s: TStream;
buf: word;
gf: TGraphic;
begin
Query1.SQL.Text := 'SELECT img FROM htest';
Query1.Open;
Query1.First;
s := Query1.CreateBlobStream(Query1.FieldByName('img'), bmRead);
try
  if s.read(buf, sizeof(buf)) <> sizeOf(buf) then exit;
  s.Position := 0;
  if buf = $D8FF then
    gf := TJPEGImage.Create
  else if buf = $4D42 then
    gf := TBitmap.Create
  else if buf = $CDD7 then
    gf := TMetafile.Create
  else if buf = 0 then  
    gf := TIcon.Create;
  try
    gf.LoadFromStream(s);
    Image1.Picture.Graphic := gf;
  finally
    gf.Free;
  end;
finally
  s.Free;  // must free stream
end;
end;
0
 
LVL 1

Author Comment

by:sageryd
ID: 8103751
Cool because I just solved it myself with pretty much the exact same code. Thanks for the confirmation :)

Mocarts, looks like a good solution, but since I'm only using jpegs your code is overkill. Thanks anyway.


johan
0
 
LVL 9

Expert Comment

by:mocarts
ID: 8103780
of course, if you use only jpeg then you can ommit those if's structures.
only what I wanted show you is how to skip intermediate stream usage by using TDataSet's CreateBlobStream.

wbr, mo.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 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