DIB to TImage control

I have a DIB in memory, which is allocated using GetMem.  In other words, it's not a GDI object, it's just on the heap.  It's a 24-bit bitmap, and it consists of a BITMAPINFOHEADER and an array of RGB data in the correct format.

What is the best way to display this in a TImage control?

Thanks!
LVL 1
VSpikeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wim ten BrinkSelf-employed developerCommented:
Write it to a TMemoryStream, then load it back as bitmap from this stream.
0
Slick812Commented:
hello VSpike, you say that there is a BITMAPINFOHEADER, , so I will guess that there is no  BITMAPFILEHEADER  with this, inorder to use the Delphi Graphics  TBitmap you will need to add a BITMAPFILEHEADER to your memory block, or you could do the API with  CreateDIBSection( )  to get a system DIB, and you will not need the BITMAPFILEHEADER, which way would be better for you?
0
Slick812Commented:
here is some code you might try for this, I have a function called  hBmpFromMem( )  which will return a DIP bitmap handle, if you pass a pointer to the  BITMAPINFOHEADER and pixel definitions to it in the  pBmpMem  parameter, below it is some code for a button click to use this function -



function hBmpFromMem(pBmpMem: Pointer): THandle;
var
pBits: Pointer;
bDC: THandle;
//bmi: TBitmapInfo;
palOff: Integer;
begin
if (pBmpMem = nil) or (TBitmapInfo(pBmpMem^).bmiHeader.biSize <>
      SizeOf(BITMAPINFOHEADER)) then
  begin
  Result := 0;
  Showmessage('ERROR - This is Not a BITMAPINFOHEADER memory block');
  Exit;
  end;

if TBitmapInfo(pBmpMem^).bmiHeader.biBitCount <15 then
  begin
  palOff := (1 shl TBitmapInfo(pBmpMem^).bmiHeader.biBitCount) *4;
  end else
  palOff := 0;

pBits := nil;
bDC := CreateCompatibleDC(0);
try
Result := CreateDIBSection(bDC, TBitmapInfo(pBmpMem^),
                DIB_RGB_COLORS, pBits, 0, 0);
CopyMemory(pBits, Pointer(Cardinal(pBmpMem)+TBitmapInfo(pBmpMem^).bmiHeader.biSize+palOff),
           TBitmapInfo(pBmpMem^).bmiHeader.biSizeImage);
finally
DeleteDC(bDC);
end;
end;

procedure TForm1.sbut_BmpFromMemoryClick(Sender: TObject);
var
aBmp, nBmp: TBitmap;
memStream: TMemoryStream;
memSize: Integer;
pMemBmp: Pointer;
hNewBmp: THandle;
begin
aBmp := TBitmap.Create;
try
aBmp.LoadFromFile('E:\8 bit.bmp');
  // get a bitmap into aBmp
memStream := TMemoryStream.Create;
aBmp.SaveToStream(memStream);
  //get the bitmap file into the memStream
finally
FreeAndNil(aBmp); // release the bitmap
end;

memSize := memStream.Size - SizeOf(BITMAPFILEHEADER);
  // subtract the bmp FILE header from the mem stream size to get the mem size
getMem(pMemBmp, memSize);
memStream.Position := SizeOf(BITMAPFILEHEADER); // move past the FILE header
memStream.ReadBuffer(pMemBmp^, memSize);
  // copy the bitmap file section into pMemBmp memory block
FreeAndNil(memStream);
   { ALL of the code above is just for this test, if you already have the
     pMemBmp, , you will not need the code above}

hNewBmp := hBmpFromMem(pMemBmp);
  // the hBmpFromMem function will create a new Bitmap from the mem block
Freemem(pMemBmp); // release memory
if hNewBmp <> 0 then
  begin
  // hBmpFromMem will return Zero if it does not work
  nBmp := TBitmap.Create;
  try
  nBmp.Handle := hNewBmp;
  PaintBox1.Canvas.Draw(4,4, nBmp);
  finally
  FreeAndNil(nBmp);
  end;
  end else
  ShowMessage('ERROR - Did not create New Bitmap');
end;

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

ask questions if you need more
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.