mj2008
mj2008

Reputation: 6757

How can I create an independent copy of a Delphi TBitmap?

I have an application that prints text and images to pages on a printer. At the footer, we output an image, which is cached by loading it once, and stored in a TBitmap. In the print routine, it creates a new TBitmap, then calls a function which assigns the cached bitmap. It then ends up calling Canvas.StretchDraw on that bitmap.

Function GetFooterGraphic(Var xBitmap : TBitmap) : boolean;
begin
  // load cache here
  if assigned(g_xFooterBitmap) then
  begin
    xBitmap.Assign(g_xFooterBitmap);
    result := true;
  end;
end

// Get bitmap, then:
xCanvas.StretchDraw(xDrawRect, xBitmap);

The problem is that the bitmap is failing to work after a certain number of pages. I can only imagine that this is a driver problem, but it happens on most printers at different times. I can fix it by reloading the bitmap each time, but I'd rather keep the cache.

Having looked at the VCL, the xBitmap.Assign actually just adds a reference to the stored item. What I want to do is take a complete copy, the most efficient way. Which comes to the question:
How can I make the TBitmap content completely independent of any other reference?

I'd like to keep the cached TBitmap content completely independent, and return a complete (deep) copy, so that the printing does not affect the cached version, and thus hopefully fix this issue.

Delphi 2007 if relevant.

Upvotes: 4

Views: 3345

Answers (2)

Uwe Raabe
Uwe Raabe

Reputation: 47809

I cannot test it here because I'm not able to reproduce the problem, but perhaps a call to FreeImage right after the Assign may help.

Upvotes: 5

Roddy
Roddy

Reputation: 68064

I'd use SaveToStream and LoadToStream, probably with a TMemoryStream.

Upvotes: 0

Related Questions