Solved

GraphicEx, loading from BlobField

Posted on 2009-05-04
4
486 Views
Last Modified: 2013-11-22
I'm having trouble loading a TImage from a BlobField using GraphicEx. The GraphicFromContent is not returning a valid Graphicclass for Bitmaps or JPEGs.

*** I DO NOT STORE THE GRAPHIC'S EXTENSION IN THE DB***

That is the purpose of using GraphicFromContent using a Stream.

What am I doing wrong?  I need use this in the AfterScroll of the dataset to load a TImage based on what is stored in the blob and I
can not make changes to my table to save the stored graphic extension.

procedure LoadImageFromDB(ABlobField: TField; AImage: TImage);

  var

    NewGraphic: TGraphicClass;

    GraphicClass: TGraphicExGraphicClass;

    Stream : TStream;

  begin

    Stream := ABlobField.DataSet.CreateBlobStream(ABlobField, bmRead);

    // for some reason, Size is ALWAYS 0 however, it STILL

    // goes into the block ???..???

    if (Stream.Size > 0) then

    begin

      Stream.Position := 0;

      GraphicClass := FileFormatList.GraphicFromContent(Stream);
 

      if GraphicClass = nil then

        raise EInvalidGraphic.Create('UnknownExtension');
 

      NewGraphic := GraphicClass.Create;

      try

        AImage.Picture.Graphic := NewGraphic;

      finally

        NewGraphic.Free;

      end;

      AImage.Picture.Graphic.LoadFromStream(Stream);
 

      if AImage.Picture.Graphic <> nil then

        AImage.Picture.Graphic := nil;

      AImage.Picture.Graphic := NewGraphic;

    end;

  end;

Open in new window

0
Comment
Question by:EddieShipman
  • 2
  • 2
4 Comments
 
LVL 21

Expert Comment

by:developmentguru
ID: 24297497
 The method that you are using will only return class types that descend from TGraphicExGraphic.  The types that are registered for those extensions currently are the base types that Delphi defines.  It looks like, in order for this to work, you will need to create two new descendants of TGraphicExGraphic (one for bitmap and one for JPG).  Register them with the FileFormatList making sure to set the replace parameter to true.  If you implement the necessary elements of the TGraphicExGraphic base class you should be able to get it to start recognizing the file types.

  Another approach came to mind.  You could store the extension within the blob field prior to storing the blob itself.  This will work beautifully as long as no other software needs to use the blob field.

Let me know if you need more.
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 24297927
How do I register my new class with FileFormatList? How do I create the new descendants, just a class or do they need something more? I haven't worked with GraphicEx in a very long time.

[quote]Another approach came to mind.  You could store the extension within the blob field prior to storing the blob itself.  This will work beautifully as long as no other software needs to use the blob field.[/quote]
I said that I can not make changes to my table to save the stored graphic extension.
0
 
LVL 21

Accepted Solution

by:
developmentguru earned 500 total points
ID: 24298667
---------------------------------------------
---How do I register my new c lass---
---------------------------------------------

use the method of the FileFormatList RegisterFileFormat:

    procedure RegisterFileFormat(const Extension, Common, Individual: String; FormatTypes: TFormatTypes; Replace, RegisterDefault: Boolean; GraphicClass: TGraphicClass);

---------------------------------------------
---How do I create the new descendants, just a class or do they need something more?---
---------------------------------------------
The class that you register can be any descendant of TGraphic.  This is part of the issue that you ran into though.  In order to be returned by GraphicFromContent it has to be a descendant of TGraphicExGraphic.  TGraphicExGRaphic descends from TBitmap which descends from TGraphic.  You will need to look at the class definition and see which of the methods you will need to overload.  An important one would seem to be the class function CanLoad.  This would allow you to code the test to see if the class you are implementing can be loaded from the stream or resource (overridden methods for each).  

---------------------------------------------
---I said that I can not make changes to my table to save the stored graphic extension---
---------------------------------------------
I assumed you meant that you could not change the number (or type) of the database fields.  My suggestion would not cause either.

I hope you understand that I have never worked with these classes.  I gleaned this information from a few minutes perusing the source.

One last comment.  I would be sure to restore the stream position after you do any testing to what it was when the routine was entered.  As a precaution I would also set the position to zero before attempting to read the stream into the new graphics class.
0
 
LVL 26

Author Closing Comment

by:EddieShipman
ID: 31577635
We actually went a different route with this one but thanks for the answer. I was unaware that we had ImageLib and thus the DBMultiImage control in their library afforded us to store any type of image in the DB. All I had to do was make a change to how the image was stored in the DB.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ddeman not working in activex 3 93
Delphi XE2 application frozen on Windows 10 10 270
Help on project with Soap 10 48
oracle global variables 4 63
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…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

21 Experts available now in Live!

Get 1:1 Help Now