Off-screen bitmaps)
Off-screen bitmap, also called memory bitmap, is widely used in Windows programming. It creates an image in the memory and displays it on the screen using the draw method. When users want to draw images on the screen faster, off-screen bitmap helps avoid blinking. Off-screen bitmap is also suitable for complex plotting programs. Users can pre-store images and display them as needed. The off-screen bitmap is used for animation. The most popular method for animation production is Microsoft's DirectX SDK.
The principle of off-screen bitmap is three simple steps:
- Create a memory bitmap)
- Draw a memory bitmap (draw on the memory bitmap)
- Copy the memory bitmap to the screen)
Creating a memory bitmap)
It is easy to create a memory bitmap. As a matter of fact, I have already created it several times in the previous explanation. Each time you create a tbitmap object, you create a memory bitmap. Some of them are to load files into the memory bitmap, and some are to create a memory bitmap, set its size, and then draw a memory bitmap. For example:
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;
Click the button each time and draw a string of rectangular boxes on the screen. This Code simply draws a memory bitmap and copies the bitmap to the form screen.
If you use the desktop 256 color settings, the final running result color will be uncertain.
Note
When a memory bitmap is created, the bitmap has the same color depth as the current display settings. In other words, if there are 256 color display settings, the memory bitmap is also a 256 color bitmap, if the display is set to 24 or 32, the memory bitmap contains 32 K, 64 K, or 16 m colors.
Saving a memory bitmap)
It is extremely easy to save the memory bitmap. What it needs to do is the code:
Bitmap.savetofile('test.bmp '); {Save the memory bitmap}
Yes. In fact, you can easily create your own screen capture program. All you need to do is copy the appropriate part of the desktop to the memory bitmap and store it in the file. The following code:
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); {number of returned color palette} 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, save the screenshot to the form.bmp file. The following figure is displayed:
Sample memory bitmap Program)
The program in the following list describes the application of memory bitmap. When one of the two buttons is clicked, the text will scroll horizontally along the screen.
The implementation of the first button does not use memory bitmap (directly written on the form screen ).
The second button uses a memory bitmap to horizontally scroll the text screen.
The third button stops scrolling.
Some code is as follows (for detailed code, download the sample code in this tutorial ):
{Draw directly to the canvas} 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; {via off-screen bitmap} 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; bitmap. free; end;
Two different drawing methods have different display effects. The scrolling Subtitles drawn through the off-screen bitmap are the smoothest. For example:
The above code passes the test in Delphi7. Download the sample code:Gdizhiping map .rar