GDI+转换问题
var TempBMP:TBitmap; gpBmp:TGPBitmap; g:TGPGraphics;procedure TForm1.FormCreate(Sender: TObject);begin Timer1.Interval:=33; ... //gpBmp:=TGPBitmap.Create(TempBMP.Handle,TempBMP.Palette); //放在这里貌似无效,在Timer1里面,gpBmp不会随着TempBMP而更新end;procedure TForm1.Timer1Timer(Sender: TObject);begin ... //这里在TempBMP上面画一些图形 gpBmp:=TGPBitmap.Create(TempBMP.Handle,TempBMP.Palette); g.DrawImage(gpBmp,0,0); gpBmp.Free; //放在这里的话,由于不断初始化和销毁,会占用很多的CPU ...end;
function BmpToGPBmp(bmp: TBitmap; alpha: Byte = 256): TGPBitmap;var x, y: Integer; p0, p1: pbytearray; isXPIcon: boolean; ScanLines: array of Byte; W, H: integer; Data: TBitmapData; CurrentX: integer;begin bmp.PixelFormat := pf32bit; try SetLength(ScanLines, Bmp.Height * Bmp.Width * 4); for y := 0 to bmp.Height - 1 do begin p0 := bmp.scanline[y]; CurrentX := bmp.Width * y * 4; for x := 0 to bmp.Width - 1 do begin ScanLines[CurrentX + x * 4] := p0[x * 4]; ScanLines[CurrentX + x * 4 + 1] := p0[x * 4 + 1]; ScanLines[CurrentX + x * 4 + 2] := p0[x * 4 + 2]; //设置Alpha ScanLines[CurrentX + x * 4 + 3] := alpha; // p0[x * 4 + 3]; end; end; Result := TGPBitmap.Create(bmp.Width, bmp.Height); //(bmp.Handle, bmp.Palette); // ;// W := Result.Width; H := Result.Height; Data := Result.LockBits(GpRect(0, 0, W, H), [imRead, imWrite], pf32bppARGB); Move(ScanLines[0], Data.Scan0^, Data.Height * Data.Stride); Result.UnlockBits(Data); finally SetLength(ScanLines, 0); end;end;