JPG and PNG inside a database (VCL)

In VCL applications we can easily add images to a database using the TDBImage component. But TDBImage only supports Bitmaps (BMP), and these uncompressed images take a lot of space and network bandwidth to store them. Fortunately it is quite easy to store compressed JPG and PNG images inside a database using TWICImage.

The TWICImage component has been available since Delphi 2010, and it uses Windows DirectX to determine and handle multiple image formats. It is actually an encapsulation of the Windows Imaging Component (WIC). We can use TWICImage to determine the type of an image and have it create a correct image format header. We can then save this header + image into a blob field.


procedure SavePictureFileToField(PictureFile: TFileName; Field: TBlobField);
var
  lWICImage: TWICImage;

begin
  lWICImage := TWICImage.Create;
  lWICImage.LoadFromFile(PictureFile);
  Field.Assign(lWICImage);
  lWICImage.Free;
end;

When we later read this image from the blob field, we use TWICImage to interpret the header and then assign it into a TImage.Picture for display.


procedure LoadPictureFromField(Field: TBlobField; Picture: TPicture);
var
  lWICImage: TWICImage;

begin
  if (Field.BlobSize > 0) then
  begin {Assume image}
    lWICImage := TWICImage.Create;
    lWICImage.Assign(Field);
    Picture.Assign(lWICImage);
    lWICImage.Free;
  end
  else {Empty}
  begin
    Picture.Assign(nil);
  end;
end;

And that’s all.

This code will work for any Delphi version starting from Delphi 2010.

Please note that if you are creating a multi-device / FireMonkey application you should not use TWICImage, the FireMonkey TImage and TImageControl both already handle all types of images just fine themselves and going through TWICImage would not be of benefit, in fact it would lock you into Windows.

Completed source code as a simple VCL SQLLite XE7 database application can be downloaded here: DBImage_JPG_PNG

Why I like FireMonkey

Yesterday I had a presentation at the SDE+ event, talking about Actions in general, and the addition of these in FireMonkey in particular. The implementation of Actions in FireMonkey differs somewhat from the VCL implementation, but I’ll plug that info a separate article.

At the presentation someone asked me; “Danny, why are you so fond of the FireMonkey framework?”. This made me think. Although there are more than enough tangible benefits to using FireMonkey instead of the VCL for your next Delphi project, there are actually only two that convinced me it’s the framework of choice of all the frameworks out there.

FireMonkey in Delphi allows you to develop cross-platform applications for Win32, Win64, OSX and soon iOS, Android and WinRT, but i find it unique because it’s:
1. One framework
2. One language

Let’s think about that; there is no other framework out there that succesfully abstracts (hides) a lot of the platform specific details, and does this with only one programming language.

If you look at competitors like Hydra or Oxygene or even C# .NET. Hydra has the same framework for a lot of platforms, but requires you to learn Objective-C for iOS and Java for Android. Oxygene has only one language, Object Pascal, and although it’s a nice product, it does not hide the underlying platform, it just exposes it. So you need to learn the underlying Cocoa framework if you want to develop for Mac OSX. If you’re using C# .NET, you do have one framework, one language, but you’re limited with Mono or WinRT. I asked a C# developer about his opinion, and he said that just now it’s easier to develop two applications if you want to deploy to Windows Phone and Windows-8 Desktop.

Food for thought.

Delphi XE3 EULA

Er is wat ophef geweest over een aanpassing van EULA van Delphi. Waar het kort gezegd op neer kwam was dat je met Delphi XE3 Professional niet meer naar remote databases zou mogen, ook niet als je onafhankelijke third-party componenten gebruikt. De EULA is inmiddels gelukkig herzien, alleen dbexpress is voor de professional versie, zoals ook in XE2, beperkt tot local access. De definitieve EULA vindt je hier http://edn.embarcadero.com/article/42481. Je kunt overigens een los Client/Server pack kopen voor remote database access met dbExpress.

Delphi XE3

Wat is er nieuw in XE3? Windows-8 style applicaties met interfacing naar een WinRT live tile, live bindings heeft nu een visual editor en de werkwijze is aangepast, de nieuwe naam is Visual LiveBindings, het FireMonkey framework is uitgebreid en heet nu FM2. Verder nog veel kleine en minder kleine wijzigingen. Wist je bijvoorbeeld dat je nu FormatSettings als prefix moet gebruiken bij het instellen van localized settings?

Delphi XE3 en Mobile development

JT @ Embarcadero heeft bekend gemaakt dat de Mobile ondersteuning in Delphi XE3 is uitgesteld. http://blogs.embarcadero.com/jtembarcadero/2012/08/20/xe3-and-beyond/

De bestaande mobile iOS ondersteuning van XE2 zal niet worden meegeleverd met XE3, in plaats daarvan wordt op een later tijdstip een native ARMv7 compiler voor iOS en Android geleverd, waarmee Delphi het enige ontwikkelplatform wordt dat vanuit één codebase met FireMonkey 2 applicaties en apps naar desktop en mobile kan brengen.

Nieuwsgierig naar alle overige nieuwe XE3 features? Delphi XE3 Tech – 25 september