tags:

views:

186

answers:

1

I found a way to write avi from BMP files:
http://www.delphi3000.com/articles/article_2770.asp?SK=
I want to write avi from array or TList of TBitmaps?

+6  A: 

The key portion of the code you linked to is below, where IList is a TStrings with the names of all the files to include in the animation.

for i := 0 to IList.Count - 1 do begin
  AssignFile(BFile, IList[i]);
  Reset(BFile, 1);
  Seek(BFile, m_bfh.bfOffBits);
  BlockRead(BFile, m_MemBits[0], m_Bih.biSizeImage);
  Seek(BFile, SizeOf(m_Bfh));
  BlockRead(BFile, m_MemBitMapInfo[0], length(m_MemBitMapInfo));
  CloseFile(BFile);
  if AVIStreamWrite(psCompressed, i, 1, @m_MemBits[0],
      m_Bih.biSizeImage, AVIIF_KEYFRAME, 0, 0) <> 0 then begin
    ShowMessage('Error during Write AVI File');
    break;
  end;
end;

It reads portions of the file from disk and writes them to the AVI stream. The important part is that it reads from the files. The in-memory representation of a TBitmap doesn't necessarily match with the representation of a file. However, it's easy to adapt the given code to temporarily store the bitmaps in a memory stream; the stream will match what the layout of the file would be. Suppose IList is now an array of TBitmap, as you suggested. Then we could use this:

var
  ms: TMemoryStream;

ms := TMemoryStream.Create;
try
  for i := 0 to Length(IList) - 1 do begin
    IList[i].SaveToStream(ms);
    ms.Position := m_bfh.bfOffBits;
    ms.ReadBuffer(m_MemBits[0], m_Bih.biSizeImage);
    ms.Position := SizeOf(m_Bfh);
    ms.ReadBuffer(m_MemBitMapInfo[0], Length(m_MemBitMapInfo));
    ms.Clear;
    if AVIStreamWrite(psCompressed, i, 1, @m_MemBits[0],
        m_Bih.biSizeImage, AVIIF_KEYFRAME, 0, 0) <> 0 then begin
      ShowMessage('Error during Write AVI File');
      break;
    end;
  end;
finally
  ms.Free;
end;

There's code earlier in your cited example that reads the first file in the list to populate the various records and size the arrays used here, but you should be able to make the same changes there as I have done to the code shown here.

Rob Kennedy
It would be nice if you paste the whole example and change the necessary parts and definitely this will be my accepted answer.
isa
+1 great answer Rob, plenty to work with!
Argalatyr
No, Isa, I'd prefer not to. Which additional part of the code are you having trouble with? Do you recognize what changes I made in the above passages? Do you understand what they're for? Don't copy and paste code into your project if you don't understand what it does, because even though you didn't write it, since it's in your project you're going to have to support it eventually.
Rob Kennedy
I understand -in general- what is going on in the cited exampleAnd I thought about storing the bitmaps in memory stream before asking this question But I couldn’t write it correctly. still can’t write it correctly I get access violation error.And no, no body will ask me to support my code I’m coding this for fun.Anyway thank you for your help and advice :-)
isa