Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

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!
0
VSpike
Asked:
VSpike
  • 2
1 Solution
 
Wim ten BrinkCommented:
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now