如题~~~~~~~~

解决方案 »

  1.   

    function GetClipBitmapFromDC(DC: HDC; x, y, Width,
      Height: Integer): HBITMAP;
    var
      memDC: HDC;
      oldBmp: HBITMAP;
    begin
      Result := CreateCompatibleBitmap(DC, Width, Height);
      memDC := CreateCompatibleDC(DC);
      oldBmp := SelectObject(memDC, Result);
      try
        BitBlt(memDC, 0, 0, Width, Height, DC, x, y, SRCCOPY);
      finally
        SelectObject(memDC, oldBmp);
        DeleteDC(memDC);
      end;
    end;// TPaintBox.Canvas.Handle只有在Paint事件中才是有效的,所以可设置某个外部条件,满足时保存图像
    procedure TForm1.PaintBox1Paint(Sender: TObject);
    var
      bmp: TBitmap;
    begin
      .... // 画图代码略
      if 条件 then
      begin
        bmp := TBitmap.Create;
        // x,y,Width,Height是给定矩形尺寸
        bmp.Handle := GetClipBitmapFromDC(PaintBox1.Canvas.Handle, 
          0, 0, PaintBox1.Width, PaintBox1.Height);
        bmp.SaveToFile('1.bmp');
        bmp.Free;
      end;
    end;
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      bmp: TBitmap;
    begin
      bmp := TBitmap.Create;
      bmp.Width := PaintBox1.Width;
      bmp.Height := PaintBox1.Height;
      bmp.PixelFormat := pf24Bit;
      bmp.Canvas.CopyRect(Rect(0, 0, bmp.Width, bmp.Height), PaintBox1.Canvas, Rect(0, 0, bmp.Width, bmp.Height));
      bmp.SaveToFile('c:\temp.bmp');
      bmp.Free;
    end;