Solved

BLOB fields and bitmaps

Posted on 1998-07-16
6
289 Views
Last Modified: 2013-12-03
Hi,

I am trying to copy a bitmap from a Blob field into a BitMap variable and then display it in an image control. Why does the following not work?

var
  BitImage : TBitmap;
  PicStream : TMemoryStream;


begin
  PicStream := TMemoryStream.Create;
  BitImage := Tbitmap.Create;
  try
    TBlobField(Table1.FieldByName('Graphic')).SaveToStream(PicStream);
    BitImage.LoadFromStream(PicStream);
    //Process bitmap image here........

    //Display processed image
    Image1.Picture.Bitmap := BitImage;
  finally
    PicStream.Free;
    BitImage.Free;
  end;
end;

I does not crash, just does not do anything!

I think I am missing something fundamental here.

Thanks

Chris
0
Comment
Question by:ChrisBerry
  • 3
  • 3
6 Comments
 
LVL 3

Accepted Solution

by:
vladika earned 50 total points
ID: 1357961
I think you forget update PicStream.Position
before BitImage.LoadFromStream(PicStream);

so try such
............
TBlobField(Table1.FieldByName('Graphic')).SaveToStream(PicStream);
PicStream.Position := 0;
BitImage.LoadFromStream(PicStream);
...........

0
 
LVL 3

Expert Comment

by:vladika
ID: 1357962
Or you can use Assign method

Image1.Picture.Bitmap.Assign(TBlobField(Table1.FieldByName('Graphic')));

It call TBlobField.AssignTo method and
AssignTo call SaveToBitmap private method
(see in source)

0
 

Author Comment

by:ChrisBerry
ID: 1357963
You are quite right, however this leads me on to a supplemetary question.

I am using the DBDEMOS database BIOLIFE table for the graphic and when I made the change to the program as suggested it then told me that it was an invalid bitmap.

What type of graphic is in this database and how is it handled in the above code?

Regards

Chris
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ChrisBerry
ID: 1357964
Thanks

Regards

Chris

0
 
LVL 3

Expert Comment

by:vladika
ID: 1357965
You are right.
There is small error/bug.

Sometimes TGraphicHeader can appears before image
So modify procedure as

{ Paradox graphic BLOB header }

type
  TGraphicHeader = record
    Count: Word;                { Fixed at 1 }
    HType: Word;                { Fixed at $0100 }
    Size: Longint;              { Size not including header }
  end;

procedure TForm1.Button1Click(Sender: TObject);
var
  BitImage : TBitmap;
  PicStream : TMemoryStream;
  Header: TGraphicHeader;
  Size: Integer;
begin
  PicStream := TMemoryStream.Create;
  BitImage := Tbitmap.Create;
  try
    TBlobField(Table1.FieldByName('Graphic')).SaveToStream(PicStream);

    PicStream.Position := 0;
    Size := PicStream.Size;
    if Size >= SizeOf(TGraphicHeader) then
    begin
      PicStream.Read(Header, SizeOf(Header));
      if (Header.Count <> 1) or (Header.HType <> $0100) or
        (Header.Size <> Size - SizeOf(Header)) then
        PicStream.Position := 0;
    end;

    BitImage.LoadFromStream(PicStream);
    Image1.Picture.Bitmap := BitImage;
  finally
    PicStream.Free;
    BitImage.Free;
  end;
end;

I found its algorithm in the TBlobField.SaveToBitmap private method (db.pas)

0
 

Author Comment

by:ChrisBerry
ID: 1357966
Thanks again,

Regards

Chris

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

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

12 Experts available now in Live!

Get 1:1 Help Now