The de-screen bitmap of the Delphi GDI object (offscreen Bitmaps), also known as a memory bitmap

Source: Internet
Author: User
Tags textout

Http://www.cnblogs.com/pchmonster/archive/2012/07/09/2583613.html

Off-screen bitmap (offscreen Bitmaps)

Off-screen bitmaps, also called memory bitmaps, are commonly used in Windows programming. It makes images in memory and then uses the draw method to display them on the screen. When a user wants to draw an image faster on the screen, the off-screen bitmap helps avoid flickering. The off-screen bitmap is also suitable for complex charting programs. The user can pre-save the image and display it when needed. The most popular way to animate the off-screen bitmap is Microsoft's DirectX SDK.

The principle of a off-screen bitmap is three simple steps:

    1. Create a memory bitmap (create a bitmap)
    2. Draw the memory bitmap (draw on the bitmap)
    3. Copy the memory bitmap on the screen (copy the memories bitmap to the screens)
Create a Memory bitmap (Creating a Bitmap)

Creating a memory bitmap is easy. In fact, the previous explanation has been created several times. Each time you create a Tbitmap object, you create a memory bitmap, some of which load the file into a memory bitmap, some create a memory bitmap, set its size, and then draw a memory bitmap, for example:

123456789101112131415161718192021222324 procedure TForm1.btn1Click(Sender: TObject);var  Bitmap: TBitmap;  I, X, Y, W, H: Integer;  Red, Green, Blue: Integer;begin  Bitmap := TBitmap.Create;  Bitmap.Width := 500;  Bitmap.Height := 500;  for I := 0 to 19 do  begin    X := Random(400);    Y := Random(400);    W := Random(100) + 50;    H := Random(100) + 50;    Red := Random(255);    Green := Random(255);    Blue := Random(255);    Bitmap.Canvas.Brush.Color := RGB(Red, Green, Blue);    Bitmap.Canvas.Rectangle(X, Y, W, H);  end;  Canvas.Draw(0, 0, Bitmap);  Bitmap.Free;end;

Each time you click the button, you will be drawn on the screen with a rectangular frame. This code simply draws the memory bitmap and copies the bitmap to the form screen.

If you use the desktop 256 color setting, the color of the final run result will be indeterminate.

Note

When a memory bitmap is created, the bitmap will have the same color depth as the current display setting. In other words, if you have 256 color display settings, the memory bitmap is also a 256-color bitmap, and if the display is set to 24-bit or 32-bit, the memory bitmap will contain 32K, 64K, or 16M colors.

Save memory Bitmap (saving a Bitmap)

It is extremely easy to save a memory bitmap. All it needs is a bit of code:

1 Bitmap.SaveToFile(‘test.bmp‘); { 保存内存位图}

Yes, that's OK. In fact, it's easy to create your own screen capture program. All you have to do is copy the appropriate part of the desktop into a memory bitmap and store it in a file. The following code:

123456789101112131415161718192021222324252627282930313233343536373839404142 procedure TForm1.btn2Click(Sender: TObject);var  DtCanvas: TCanvas;  Bitmap: TBitmap;  NumColor: Integer;  LogPal: PLogPalette;  Src, Dst: TRect;begin  { Create a TCanvas object for the desktop DC.}  DtCanvas := TCanvas.Create;  DtCanvas.Handle := GetDC(0);  { Create a new TBitmap object and set its}  { size to the size of the form.}  Bitmap := TBitmap.Create;  Bitmap.Width := Width;  Bitmap.Height := Height;  { Create a palette from the form‘s Canvas}  { and assign that palette to the Bitmap‘s}  { Palette property.}  NumColor := GetDeviceCaps(Canvas.Handle, SIZEPALETTE);  { 返回调色板的颜色数}  GetMem(LogPal, SizeOf(TLogPalette) + (NUMCOLORS - 1) * SizeOf(TPaletteEntry));  LogPal.palVersion := $300;  LogPal.palNumEntries := NumColor;  GetSystemPaletteEntries(Canvas.Handle, 0, NumColor, LogPal.palPalEntry);  Bitmap.Palette := CreatePalette(LogPal^);  FreeMem(LogPal);  { Copy a section of the screen from the    desktop canvas to the Bitmap }  Src := BoundsRect;  Dst := Rect(0, 0, Width, Height);  Bitmap.Canvas.CopyRect(Dst, DtCanvas, Src);  { Save it to disk}  Bitmap.SaveToFile(‘form.bmp‘);  { Clean up and go home}  Bitmap.Free;  DtCanvas.Free;end;

After running, the program interface will be intercepted and saved to the Form.bmp file, showing the picture as follows:

Memory Bitmap Program instance (Sample memory Bitmap programs)

The following list shows the application of the memory bitmap in the program. When you click one of the two buttons, the text scrolls horizontally along the screen.

The implementation of the first button does not use a memory bitmap (written directly on the form screen).

The second button uses a memory bitmap to enable horizontal scrolling of the text screen.

A third button stops scrolling.

Some of the code is as follows (please download this example code for detailed code):

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970 { 直接画到画布上}procedure TForm1.btn3Click(Sender: TObject);var  I: Integer;begin  Canvas.Font.Name := ‘Arial Bold‘;  Canvas.Font.Size := 20;  Canvas.Brush.Color := clSilver;  Done := False;  while not Done do  begin    for I := -canvas.TextWidth(DisplayText) to pred(Width) do    begin      Sleep(1);      Application.ProcessMessages;      if Done then        Break;      Canvas.Font.Color := clGray;      Canvas.Brush.Style := bsClear;      Canvas.TextOut(i + 2, 12, DisplayText);       Canvas.Font.Color := clBlack;      Canvas.Brush.Style := bsClear;      Canvas.TextOut(i, 10, DisplayText);      Canvas.Font.Color := clSilver;      Canvas.TextOut(i + 2, 12, DisplayText);      Canvas.TextOut(i, 10, DisplayText);    end;  end;end;{ 通过脱屏位图}procedure TForm1.btn5Click(Sender: TObject);begin  Done := True;end;procedure TForm1.btn4Click(Sender: TObject);var  Bitmap: TBitmap;  I: Integer;begin  Bitmap := TBitmap.Create;  Bitmap.Width := Width;  Bitmap.Height := 40;  Bitmap.Canvas.Font.Name := ‘Arial Bold‘;  Bitmap.Canvas.Font.Size := 20;  Bitmap.Canvas.Brush.Color := clSilver;  Bitmap.Canvas.FillRect(Rect(0, 0, Width, 40));  Done := False;  while not Done do  begin    for I := -Bitmap.Canvas.TextWidth(DisplayText) to Pred(Width) do    begin      Application.ProcessMessages;      if (Done) then        Break;      Sleep(1);      Bitmap.Canvas.Font.Color := clGray;      Bitmap.Canvas.Brush.Style := bsClear;      Bitmap.Canvas.TextOut(2, 12, DisplayText);      Bitmap.Canvas.Font.Color := clBlack;      Bitmap.Canvas.Brush.Style := bsClear;      Bitmap.Canvas.TextOut(0, 10, DisplayText);      Canvas.Draw(i, 0, Bitmap);    end;  end;  Bitmap.Free;end;

Two different drawing styles, the display of the effect is also different, which is drawn by the off-screen bitmap scrolling subtitle is the smoothest. Such as:

The above code is tested in Delphi7 and the sample code is downloaded: GDI's off-screen bitmap. rar

The de-screen bitmap of the Delphi GDI object (offscreen Bitmaps), also known as a memory bitmap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.