如何将一个窗体保存为一幅位图

解决方案 »

  1.   

    看看delphi带的form.pas文件关于form.print的部分吧
      

  2.   

    var 
        winHWND, hCur:integer; 
         winDC:integer;
         rect:TRect;
         pt:TPoint;
         fBitmap:TBitmap;
    begin
         winHWND :=Form1.Handle;
         winDC := GetDC(winHWND); 
         GetWindowRect(winHWND, rect); 
         fBitmap := TBitmap.create; 
         fBitmap.width := rect.right-rect.left; 
         fBitmap.height := rect.bottom-rect.top; 
         BitBlt(fBitmap.canvas.handle, 0, 0, fBitmap.width, fBitmap.height, winDC, 0, 0, SRCCOPY); 
         DrawIcon(fBitmap.canvas.handle, pt.x, pt.y, hCur); 
         ReleaseDC(winHWND, winDC); 
         Image1.Picture.Bitmap.Assign(fBitmap);
         fBitmap.Free;
    end;
      

  3.   

    就是这段:
    forms.pas中procedure TCustomForm.Print;
    var
      FormImage: TBitmap;
      Info: PBitmapInfo;
      InfoSize: DWORD;
      Image: Pointer;
      ImageSize: DWORD;
      Bits: HBITMAP;
      DIBWidth, DIBHeight: Longint;
      PrintWidth, PrintHeight: Longint;
    begin
      Printer.BeginDoc;
      try
        FormImage := GetFormImage;
        Canvas.Lock;
        try
          { Paint bitmap to the printer }
          with Printer, Canvas do
          begin
            Bits := FormImage.Handle;
            GetDIBSizes(Bits, InfoSize, ImageSize);
            Info := AllocMem(InfoSize);
            try
              Image := AllocMem(ImageSize);
              try
                GetDIB(Bits, 0, Info^, Image^);
                with Info^.bmiHeader do
                begin
                  DIBWidth := biWidth;
                  DIBHeight := biHeight;
                end;
                case PrintScale of
                  poProportional:
                    begin
                      PrintWidth := MulDiv(DIBWidth, GetDeviceCaps(Handle,
                        LOGPIXELSX), PixelsPerInch);
                      PrintHeight := MulDiv(DIBHeight, GetDeviceCaps(Handle,
                        LOGPIXELSY), PixelsPerInch);
                    end;
                  poPrintToFit:
                    begin
                      PrintWidth := MulDiv(DIBWidth, PageHeight, DIBHeight);
                      if PrintWidth < PageWidth then
                        PrintHeight := PageHeight
                      else
                      begin
                        PrintWidth := PageWidth;
                        PrintHeight := MulDiv(DIBHeight, PageWidth, DIBWidth);
                      end;
                    end;
                else
                  PrintWidth := DIBWidth;
                  PrintHeight := DIBHeight;
                end;
                StretchDIBits(Canvas.Handle, 0, 0, PrintWidth, PrintHeight, 0, 0,
                  DIBWidth, DIBHeight, Image, Info^, DIB_RGB_COLORS, SRCCOPY);
              finally
                FreeMem(Image, ImageSize);
              end;
            finally
              FreeMem(Info, InfoSize);
            end;
          end;
        finally
          Canvas.Unlock;
          FormImage.Free;
        end;
      finally
        Printer.EndDoc;
      end;
    end;
      

  4.   

    procedure TMainForm.Button1Click(Sender: TObject);
    var
      Bitmap: TBitmap;
    begin
      Bitmap := GetFormImage;
      try
        Bitmap.SaveToFile('c:\Form1.bmp'); 
      finally
        Bitmap.Free;
      end;
    end;
      

  5.   

    var
    bmp:tbitmap;
    begin
      bmp:=tbitmap.create;
      bmp.width:=width;
      bmp.height:=height;
      bmp.canvas.copyrect(bmp.canvas.cliprect,canvas,canvas.cliprect);
      bmp.savetofile('1.bmp');
      bmp.free;
    end;