delphi 圖像旋轉

網絡蒐集的 以爲有用的拿去吧  速度不錯呢網絡

//旋轉90°
procedure Rotate(Bitmap: TBitmap);
type
  THelpRGB = packed record
    rgb: TRGBTriple;
    dummy: byte;
  end;

  pRGBArray = ^TRGBArray;
  TRGBArray = array[0..32767] of TRGBTriple;
var
  aStream: TMemorystream;
  //內存流
  header: TBITMAPINFO;
  dc: hDC;
  P: ^THelpRGB;
  x, y, b, h: Integer;
  RowOut: pRGBArray;
begin
  //建立內存流
  aStream := TMemoryStream.Create;
  //設置大小,必須是4的倍數
  aStream.SetSize(Bitmap.Height * Bitmap.Width * 4);
  with header.bmiHeader do //操做位圖文件
  begin
    biSize := SizeOf(TBITMAPINFOHEADER); //大小
    biWidth := Bitmap.Width; //位圖寬
    biHeight := Bitmap.Height; //位圖高
    biPlanes := 1;
    biBitCount := 32;
    //無壓縮
    biCompression := 0;
    biSizeimage := aStream.Size;
    biXPelsPerMeter := 1; //水平分辨率
    biYPelsPerMeter := 1; //豎直分辨率
    biClrUsed := 0;
    biClrImportant := 0;
  end;
  dc := GetDC(0);
  P := aStream.Memory;
  GetDIBits(dc, Bitmap.Handle, 0, Bitmap.Height, P, header, dib_RGB_Colors);
  ReleaseDC(0, dc);
  b := bitmap.Height; //源圖高
  h := bitmap.Width; //源圖寬
  //指定要建立的位圖的大小尺寸
  bitmap.Width := b;
  bitmap.height := h;
  for y := 0 to (h - 1) do
  begin
    rowOut := Bitmap.ScanLine[y]; //獲取新的位圖信息
    P := aStream.Memory; //設置文件指針
    inc(p, y); //指針移位
    for x := 0 to (b - 1) do
    begin
      rowout[x] := p^.rgb; //進行數據轉移
      inc(p, h);
    end;
  end;
  aStream.Free; //釋放資源
end;

 

後面這個很差找 指針

//反向旋轉90°
procedure Rotate2(aBitmap: TBitmap);
var
  nIdx, nOfs,
  x, y, i, nMultiplier: integer;
  nMemWidth, nMemHeight, nMemSize, nScanLineSize: LongInt;
  aScnLnBuffer: PChar;
  aScanLine: PByteArray;
begin
  nMultiplier := 3;
  nMemWidth := aBitmap.Height;
  nMemHeight := aBitmap.Width;
  //實際須要內存大小
  nMemSize := nMemWidth * nMemHeight * nMultiplier;
  //開闢內存
  GetMem(aScnLnBuffer, nMemSize);
  try
    //Scanline的長度
    nScanLineSize := aBitmap.Width * nMultiplier;
    //爲ScanLine分配內存
    GetMem(aScanLine, nScanLineSize);
    try
      for y := 0 to aBitmap.Height - 1 do
      begin
        //進行數據塊的移動
        Move(aBitmap.ScanLine[y]^, aScanLine^, nScanLineSize);
        for x := 0 to aBitmap.Width - 1 do
        begin
          nIdx := ((aBitmap.Width - 1) - x) * nMultiplier;
          nOfs := (x * nMemWidth * nMultiplier) + (y * nMultiplier);
          for i := 0 to nMultiplier - 1 do
            Byte(aScnLnBuffer[nOfs + i]) := aScanLine[nIdx + i];
        end;
      end;
      //寬和高交換開始,逆時針旋轉
      aBitmap.Height := nMemHeight;
      aBitmap.Width := nMemWidth;
      for y := 0 to nMemHeight - 1 do
      begin
        //數據移動
        nOfs := y * nMemWidth * nMultiplier;
        Move((@(aScnLnBuffer[nOfs]))^, aBitmap.ScanLine[y]^, nMemWidth *
          nMultiplier);
      end;
    finally
      //釋放內存aScanLine
      FreeMem(aScanLine, nScanLineSize);
    end;
  finally
    //釋放內存aScnLnBuffer
    FreeMem(aScnLnBuffer, nMemSize);
  end;
end;