Solved

displaying bitmaps from memory

Posted on 1997-04-23
15
281 Views
Last Modified: 2013-12-03
I have a bitmap in memory in the correct format stored in an array and I wish to display it as quickly as possible on the screen. At present I copy the array to disk and display it after doing a bitmap.loadfromfile this is however to slow for my purpose. Please advise
0
Comment
Question by:thales042397
  • 6
  • 3
  • 3
  • +2
15 Comments
 
LVL 4

Expert Comment

by:StevenB
ID: 1335648
Presumably your bitmap is stored in A TBitmap in memory. To disply this on the screen simply use the Draw method of a canvas:

1) Create the component on which you wish to display the bitmap
2) With the component call Canvas.Draw(X,Y,Bitmap) Where X and Y are the coordinates where you want the image.

If your bitmap in memory is not stored in a TBitmap then the best approach is to get it into one as soon as possible:

1) Create a TBitmap
2) Loop through the array which holds the image and use the Pixels property of the Bitmap's Canvas to copy the bitmap into the TBitmap pixel at a time.
3) Display the bitmap as above.

  Hope this helps, if you want more info then add a comment,

  Steven


0
 

Author Comment

by:thales042397
ID: 1335649
Thanks steve,

Is it however not possible to somehow Point Tbitmap to the image in memory because time is of the essense, rather than copying  the info from one point in memory to the next and then displaying



0
 
LVL 4

Expert Comment

by:StevenB
ID: 1335650
 OK, firstly, I am wondering where your bitmap is coming from. If at all possible I would be tempted to suggest that you ensure that it gets into a TBitmap, we are using Delphi afterall. Perhaps some more info on what you are trying to do would help.

  If you cannot get the TBitmap component involved then you will probably be looking to use the API calls CreateBitmap or CreateCompatibleBitmap and SetDIBits calls. These allow you to turn a block of data in memory into a bitmap and display them. I won't go into this in too much depth here, there is online help.

  If you tell me more about what you are trying to do, how the bitmap is being generated and what format it is being stored in then I may be able to help you better. What sort of handle do you have on the bitmap?

  Steven.
0
 

Author Comment

by:thales042397
ID: 1335651
Ok, let me start at the begining. I have a custom made frame grabber in the pc. I access the frame grabber as a block of memory between d000 and dfff which gives me 65k worth of data. The format of the  received data from the frame grabber is YUV. I copy the frame grabber memory location to an array in memory, approximatelly 8 times (65k*8) to get the entire image, after which I convert the image to RGB and add bitmap info for display purposes. What I really want is to display the image as close to realtime as possible thus the need for a fast method of display.

Thanks,
Kevin
0
 
LVL 4

Expert Comment

by:StevenB
ID: 1335652
 If I understand you correctly then you have a handle on the bitmap. Have you looked at the API call GetDIBits, I think it may be what you're looking for. There are lots of functions for Blitting bitmaps to devices, depending on what sort of handles you have.

0
 
LVL 3

Expert Comment

by:sperling
ID: 1335653
If I've understood this, you have a memory image of a valid bitmap file, and you need to have windows recognize your raw data as a bitmap and then display it, without going through too many conversions/data moves.

If so, let me know and I'll have a solution for you in a day or so.

Regards,

Erik.
0
 

Author Comment

by:thales042397
ID: 1335654
If Eric Sperling could please attempt the question as he offered in the last response I would really appreciate it

Thanks,
Kevin.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 3

Expert Comment

by:sperling
ID: 1335655
Just to be sure. Your memory image is in the exact format of a bitmap file. Right?

Erik.

0
 
LVL 2

Expert Comment

by:gysbert1
ID: 1335656
Completely off the subject for a moment:
Kevin, are you the same Kevin that I know from the company
Thales Advanced Engineering, or is it just a coincedence?
0
 
LVL 3

Expert Comment

by:mheacock
ID: 1335657
Couldn't you copy the data straight from the frame grabber
into a pre-made TBitmap, so your YUV to RGB conversions within
the TBitmap, then display it.  YUV is the same width as RGB, is
it not...therefore each TBitmap pixel will hold YUV data (though
in that format the Bitmap would look odd) and you could just do the conversions in the TBitmap (to be redundant).
0
 
LVL 3

Accepted Solution

by:
sperling earned 70 total points
ID: 1335658
Here's a unit... Think it does what you wan't.

Regards,

Erik.

--
unit MainForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    OD: TOpenDialog;
    Button: TButton;
    PB: TPaintBox;
    procedure ButtonClick(Sender: TObject);
    procedure PBPaint(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FBitmapData : POINTER;
  public
    procedure LoadBitmapFile (AFilename : STRING);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


//////////////////////////////////////////////////////////////////////////////
// Start of useful source
//////////////////////////////////////////////////////////////////////////////

const
// Constants used to determine what headers are used in the bitmap
  BMP_COREINFO = SizeOf(TBitmapCoreHeader);
  BMP_INFO     = SizeOf(TBitmapInfoHeader);

type
  // A record encapsulating the first few bytes of any bitmap file.
  // Check InfoType against the BMP_XXX constants to determine which
  // info record to use, bmi or bmci
  TBMPHeader =
    packed record
      Header      : TBitmapFileHeader;
      case integer of
        0 : (bmci     : TBitmapCoreInfo);
        1 : (bmi      : TBitmapInfo);
        2 : (InfoType : DWORD);
    end;
  PBMPHeader = ^TBMPHeader;


function BMPToCanvas (lpBitmapImage : POINTER; Canvas : TCanvas) : BOOLEAN;
// Function accepts a pointer to an in-memory image of a bitmap file, and
// displays this bitmap on the passed TCanvas
var
  lpBmpHeader     : PBMPHeader;
  lpBmpData       : POINTER;
begin
  Result := FALSE;

  // Access data as a file header.
  lpBmpHeader := lpBitmapImage;

  // Calculate offset to pixels in memory
  lpBmpData := POINTER ( INTEGER (lpBitmapImage) + lpBmpHeader^.Header.bfOffBits );

  // We only support one type of bitmap.
  if lpBmpHeader^.InfoType <> BMP_INFO then exit;

  // And then let windows do the work.
  Result := StretchDIBits (
    Canvas.Handle,
    0, 0,                                  
    lpBmpHeader^.bmi.bmiHeader.biWidth,
    lpBmpHeader^.bmi.bmiHeader.biHeight,
    0, 0,
    lpBmpHeader^.bmi.bmiHeader.biWidth,
    lpBmpHeader^.bmi.bmiHeader.biHeight,
    lpBmpData,
    lpBmpHeader^.bmi,
    DIB_RGB_COLORS,
    SRCCOPY) <> GDI_ERROR;
end;

//////////////////////////////////////////////////////////////////////////////
// End of useful source
//////////////////////////////////////////////////////////////////////////////

procedure TForm1.LoadBitmapFile (AFilename : STRING);
var
  hFile,
  fSize,
  fRead    : DWORD;
  pData    : POINTER;
begin
  // Load the specified filename raw into the field FBitmapData
  hFile := CreateFile(PChar(AFilename), GENERIC_READ, FILE_SHARE_WRITE or FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
  if hFile=INVALID_HANDLE_VALUE then exit;

  if FBitmapData <> nil then FreeMem(FBitmapData);

  fSize := GetFileSize(hFile, nil);
  GetMem (FBitmapData, fSize);
  ReadFile (hFile, FBitmapData^, fSize, fRead, nil);
  CloseHandle (hFile);
end;

procedure TForm1.ButtonClick(Sender: TObject);
begin
  if OD.Execute then begin
    // Load the file
    LoadBitmapFile (OD.Filename);
    // And make sure the paintbox is redrawn
    PB.Invalidate;
  end;
end;

procedure TForm1.PBPaint(Sender: TObject);
var
  ndx,
  Ticks   : INTEGER;
begin
  if FBitmapData <> nil then begin

    // For testing, we repeat the draw operation 10 times and then
    // displays the time used
    Ticks := GetTickCount;
    for ndx := 1 to 10 do begin
      BmpToCanvas (FBitmapData, PB.Canvas);
    end;
    Ticks := GetTickCount-Ticks;
    Button.Caption := IntToStr(Ticks);

  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if FBitmapData <> nil then FreeMem(FBitmapData);
end;

end.
0
 

Author Comment

by:thales042397
ID: 1335659
Thanks Eric I will be trying your proposed unit today and Will post a response.

To Gysbert1

Thales is the same company but I am a different kevin. Drop me a note and I will provide more info.
0
 

Author Comment

by:thales042397
ID: 1335660
Thanks again the results were perfect
0
 
LVL 2

Expert Comment

by:gysbert1
ID: 1335661
Hi Kevin,
what's your email address?
I couldn't seem to find it in your customer info...
0
 

Author Comment

by:thales042397
ID: 1335662
To gysbert1

My email address is support-desk@thales.co.za

Hope to hear from you soon

Kevin.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now