请高手给小弟解燃眉之急,帮忙写一个将bmp图像旋转90度的算法

解决方案 »

  1.   

    function TPic.ImageRotate90(Bitmap: TBitmap):TBitmap;
    var
      nIdx, nOfs,
      x, y, i,nMultiplier: integer;
      nMemWidth, nMemHeight, nMemSize,nScanLineSize: LongInt;
      aScnLnBuffer: PChar;
      aScanLine: PByteArray;
    begin
      Result:=nil;
      Result:=TBitmap.create;
      nMultiplier := GetPixelSize(Bitmap);
      nMemWidth := Bitmap.Height;
      nMemHeight := Bitmap.Width;
      nMemSize := nMemWidth * nMemHeight * nMultiplier;
      GetMem(aScnLnBuffer, nMemSize);
      try
        nScanLineSize := Bitmap.Width * nMultiplier;
        GetMem(aScanLine, nScanLineSize);
        try
          for y := 0 to Bitmap.Height-1 do
          begin
            Move(Bitmap.ScanLine[y]^, aScanLine^, nScanLineSize);
            for x := 0 to Bitmap.Width-1 do
            begin
              nIdx := ((Bitmap.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;
          Bitmap.Height := nMemHeight;
          Bitmap.Width := nMemWidth;
          for y := 0 to nMemHeight-1 do
          begin
            nOfs := y * nMemWidth * nMultiplier;
            Move((@(aScnLnBuffer[nOfs]))^, Bitmap.ScanLine[y]^, nMemWidth * nMultiplier);
          end;
        finally
          FreeMem(aScanLine, nScanLineSize);
        end;
      finally
        FreeMem(aScnLnBuffer, nMemSize);
        Result.Assign(Bitmap);
      end;
    end;
      

  2.   

    //顺时针旋转90度
    procedure Rotate90(const Bitmap:TBitmap);
    var
      i, j: Integer;
      rowIn,rowOut: PRGBTriple;
      Bmp: TBitmap;
      Width,Height: Integer;
    begin
      Bmp := TBitmap.Create;
      Bmp.Width := Bitmap.Height;
      Bmp.Height := Bitmap.Width;
      Width := Bitmap.Width - 1;
      Height := Bitmap.Height - 1;
      for j := 0 to Height do
      begin
        rowIn := Bitmap.ScanLine[j];
        for i := 0 to Width do
        begin
          rowOut := Bmp.ScanLine[i];
          Inc(rowOut, Height - j);
          rowOut^ := rowIn^;
          Inc(rowIn);
        end;
      end;
      Bitmap.Assign(Bmp);
    end;
    //逆时针旋转90度
    procedure Rotate-90(const Bitmap:TBitmap);
    var
      i, j: Integer;
      rowIn, rowOut: PRGBTriple;
      Bmp: TBitmap;
      Width,Height: Integer;
    begin
      Bmp := TBitmap.Create;
      Bmp.Width := Bitmap.Height;
      Bmp.Height := Bitmap.Width;
      Width := Bitmap.Width - 1;
      Height:= Bitmap.Height - 1;
      for j := 0 to Height do
      begin
        rowIn := Bitmap.ScanLine[j];
        for i := 0 to Width do
        begin
          rowOut := Bmp.ScanLine[Width - i];
          Inc(rowOut,j);
          rowOut^ := rowIn^;
          Inc(rowIn);
        end;
      end;
      Bitmap.Assign(Bmp);
    end;
      

  3.   

    先创建一个临时的IMAGE,然后
        Left := 0;
        Top := 0;
        Height := image.Width;
        Width := image.Height;
        for aa := 0 to image.Height do
          for ss := 0 to image.Width do
            canvas.Pixels[(-aa + image.Height),
              ss] := image.canvas.Pixels[ss, aa];
    最后把临时的IMAGE重新写如原来的BMP中。
    上面的是右转90度。