如题..

解决方案 »

  1.   


      var DC : HDC;
          Bmp : TBitmap;
          rc : TRect;
          png : TPngObject;
    begin
      DC := GetDC(form1.Handle);
      GetClipBox(DC,rc);
      Bmp := Tbitmap.Create;
      Bmp.Width := rc.right - rc.left;
      Bmp.Height := rc.Bottom - rc.Top;
      Bitblt(Bmp.Canvas.Handle,0,0,Bmp.Width,Bmp.Height,DC,0,0,SRCCOPY);
      ReleaseDC(form1.Handle,DC);
      Png := TPngObject.Create;
      Png.Assign(Bmp);
      Png.SaveToFile('a.png');
      Bmp.Free;
      Png.Free;
    end;
      

  2.   

    说明,如果用GDI+的方式 ,如果原图是32位的,那保存时,不会保存透明通道,采用GDI的方式,可以把透明通道也保存,代码如下  png.Assign(Bitmap);
      png.CreateAlpha;
      for i := 0 to png.Header.Height - 1 do
      begin
        pData := Bitmap.ScanLine[i];
        for j := 0 to png.Header.Width - 1 do
          png.AlphaScanline[i]^[j] := pData[j * 4 + 3];
      end;GDI+保存PNG格式代码:  procedure SaveTofile(Bitmap : TBitmap;aExt : string);
      var Gpbmp : TGpBitmap;
          CLSID : TGUID;
          Parameters: TEncoderParameters;
          Quality: Integer;
      begin
        if GetEncoderClsid('Image/' + aExt,CLSID) then
        begin
          Gpbmp := TGpBitmap.Create(Bitmap.Handle,Bitmap.Palette);
          Parameters.Count := 1;
          Parameters.Parameter[0].NumberOfValues := 1;
          Quality := 100;
          Parameters.Parameter[0].Value := @Quality;
          Gpbmp.Save(aFile,CLSID,@Parameters);
          FreeandNil(Gpbmp);
        end;
      end;