Category Archives: Objective Pascal

Articles about programming with Objective Pascal in Delphi or Appmethod

LiveBindings in VCL – Part 6 – Master Object, Detail TDataSet

Useful background to this post is available in post 4 (Linking to an Object for master detail) and post 5 (advanced master detail).

Filtering a TDataSet as detail of an object

Having explored how to link to a Master Detail relationship and created a funky anonymous method to return the data, I thought there is no reason why I should have to be returning linked data. The data could be from anywhere.

One example people have asked about when I’ve been at developer events is how to link an object to be the master for filtering a TDataSet… well, this actually provides a simple example for doing exactly this (as you can see in this video)

Continue reading LiveBindings in VCL – Part 6 – Master Object, Detail TDataSet

LiveBindings in VCL – Part 5 – Advanced Master Detail Objects

Please read LiveBindings in VCL – Part 4 – Master Detail before reading this blog / watching the video

In the last blog we looked at how to use master detail and how the BeforeScroll and AfterScoll events worked. I also created using Generics a Sync Object that allowed a single detail object to be linked to.

As promised, this blog post takes it a step further, using the previous foundation I have updated the code (so make sure you download the latest version from code central from the link below) to allow multiple objects of different types to be linked. Additionally, detail lists are also now available.

Link to code sample below…
Continue reading LiveBindings in VCL – Part 5 – Advanced Master Detail Objects

LiveBindings in VCL – Part 4 – Master Detail and Objects as Properties

This is the 4th video and blog in a series on using Visual LiveBindings mixed into existing VCL applications. 

In my last post, we looked at how to bind directly in code, using some common Visual LiveBinding Classes and in the next post we will look at more classes that can be used and the difference in the bindings they create. However before we get there a number of you asked about binding to master detail and sub objects. So lets explore this matter.

More in the blog post.

Continue reading LiveBindings in VCL – Part 4 – Master Detail and Objects as Properties

LiveBindings in VCL Part 3: Bindings via code

This is the third post of a series looking at using VCL and LiveBindings. This posts will expand on post 2 and dynamically create via code the LiveBindings at run time.

In my second blog post on Using LiveBindings in a VCL application, I looked at using the Prototype bind source for the first time and how it can be used to link objects to screens. But what if you need to work this out dynamically?

Dynamically linking to objects is possible if you create the LiveBindings at run time and in this video I show how to easily work out the components and properties you need to setup. This post builds on what was learned in the previous posts VCL LiveBindings and DataSets and VCL LiveBindings to Objects

Continue reading LiveBindings in VCL Part 3: Bindings via code

Bitmap to PNG/JPEG/JPG in Delphi / C++Builder

In FireMonkey, Bitmap is the common currency for working with images. If you want to send an image via Stream using LiveBindings or want to save an Image to disk or into a database on a device with limited storage however, they are quite large.

Thankfully FireMonkey has a really easy to use CodecManager that allows you to SaveToFile or SaveToStream and convert to PNG or JPG across all platforms (which are vastly smaller in size).

SaveToFile

Creating a PNG, JPG/JPEG is as simple as calling the Bitmap.SaveToFile with the correct file extension. This works really well with even more extensions supported.

Personally I would use PNG as the JPG formats are around the same size on some test I’ve run, but produce a better quality of image, especially when grabbing screenshots.

SaveToStream and TBitmapCodecManager

Bitmap.SaveToStream doesn’t expose the CodexManager in the same way as it is available automatically with the SaveToFile option so takes a little more work to get a compressed image into the memory stream.

This is done using the TBitmapCodecManager class. A simple screen test has gone from over 1mb as a bitmap to around 6kb when saved to PNG using this approach.

The following code is an example using two image viewers (and a few save to files to help demonstrate the difference in file size). The key part is the TBitmapSurface and TBitmapCodecManager.

procedure TForm1.Button1Click(Sender: TObject);
var
  Stream: TMemoryStream;
  Surf: TBitmapSurface;
  aFile : string;
begin
  ImageViewer1.Bitmap :=
    TabControl1.MakeScreenshot;

  // Comparison save file as BMP 
  aFile := 'c:\test\myImage';
  ImageViewer1.Bitmap.SaveToFile(aFile+'.bmp');
  Stream := TMemoryStream.Create;
  try
    Stream.Position := 0;
    Surf := TBitmapSurface.Create;
    try
      Surf.Assign(ImageViewer1.Bitmap);
      // use the codec to save Surface to stream
      if not TBitmapCodecManager.SaveToStream( 
                       Stream, 
                       Surf, 
                       '.png') then
        raise EBitmapSavingFailed.Create(
              'Error saving Bitmap to png');
    finally
      Surf.Free;
    end;
    // do something with the png stream image
    Stream.Position := 0;
    ImageViewer2.Bitmap.LoadFromStream(Stream);

    // comparison output as PNG from the stream
    Stream.Position := 0;
    Stream.SaveToFile(aFile+'.png');
  finally
    Stream.Free;
  end;
end;

Now, you can obviously save as a ‘PNG’ from the bitmap, but the above code shows how to avoid saving a file  to disk unless you really need to.

Happy coding!

See Whats Coming in Delphi and C++ Builder

WhatsNewInDelphi20150902

September 2nd is due to be a very important day in Delphi & C++ Builder history, if the recent news from Microsoft and what I have collated from the “whats coming” posts recently.

Register for the Delphi and C++ Builder “First look”

Spoiler alert!

If you only want to find out whats new when you’re on the “See whats coming in Delphi, RAD Studio and C++ Builder” webinar on the 2nd September, then don’t read further. You may learn

Continue reading See Whats Coming in Delphi and C++ Builder