Solved

BLOB fields and bitmaps

Posted on 1998-07-16
6
297 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
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…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

628 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