我用程序画了一个矩形在Image控件上,现在我想90度,180度旋转它。怎么办呢,有哪位高手能帮看看哦。~ⷼ/td>

void __fastcall TForm1::BitBtn1Click(TObject *Sender)
{
Graphics::TBitmap *bmp=new Graphics::TBitmap();
bmp->Width =Image1->Width ;
bmp->Height=Image1->Height ;
TRect r,r1;
r.top =0;r.left =0;
r.right =Image1->Width ;
r.Bottom  =Image1->Height ;
//r1设置跟r倒过来,这样是180度,还可以只倒一个方向
r1.top=r.Bottom ;
r1.left=r.right ;
r1.right =0;r1.Bottom =0;
bmp->Canvas->CopyRect(r,Image1->Canvas ,r1);

PaintBox1->Canvas->CopyRect(r,bmp->Canvas ,r);
delete bmp;
}
//image内必须是bmp图像,如是jpeg,要转换,自己画的就没问题了

引用 1 楼 yumikoo 的回复:
C/C++ code
#include "math.h"
void RotatePicture(Graphics::TBitmap *SourcePicture,Graphics::TBitmap *DestPicture,int angle)
{//以任意角度旋转图像
  if(angle>180)
    angle=360-angle;
  if(angle <-180)
    angle=360+angle;
  float radians=(2*3.1416*angle)/360;
  float cosine=(float)cos(radians);
  float sine=(float)sin(radians);
  float Point1x=(-SourcePicture->Height*sine);
  float Poi…

这个程序是有问题的。~

我测试过是没有问题的。这段代码我以前用过几次。
你再检查下。

会造成内存泄漏。~点几下的时候没事,点多了就有问题~ⷼ/td>

并且图像会随点击的次数而变得越来越模糊~

void __fastcall TOPDialog::Turn(Graphics::TBitmap *TBMP, Graphics::TBitmap *BMP, int Angle)
{
BMP->Assign(TBMP);
TBMP->Canvas->Lock();
BMP->Canvas->Lock();
switch (Angle)
{
case 0:
break;
case 1: //90度
{
TBMP->Height = BMP->Width;
TBMP->Width = BMP->Height;
for(int i=0; i < TBMP->Height; i++)
{
for(int j=0; j < TBMP->Width; j++)
{
TBMP->Canvas->Pixels[j][i] =
BMP->Canvas->Pixels[i][TBMP->Width - j - 1];
}
}
break;
}
case 2: //180度
{
TBMP->Height = BMP->Height;
TBMP->Width = BMP->Width;
for(int i=0; i < TBMP->Height; i++)
{
for(int j=0; j < TBMP->Width; j++)
{
TBMP->Canvas->Pixels[j][i] =
BMP->Canvas->Pixels[TBMP->Width - j - 1][TBMP->Height - i - 1];
}
}
break;
}
case 3: //270度,也就是-90度
{
TBMP->Height = BMP->Width;
TBMP->Width = BMP->Height;
for(int i=0; i < TBMP->Height; i++)
{
for(int j=0; j < TBMP->Width; j++)
{
TBMP->Canvas->Pixels[j][i] =
BMP->Canvas->Pixels[TBMP->Height - i - 1][j];
}
}
break;
}
}
TBMP->Canvas->Unlock();
BMP->Canvas->Unlock();
}

void __fastcall TForm1::btnR180Click(TObject *Sender)
{
  Image1->Canvas->FillRect(Image1->Canvas->ClipRect);
  pBitmap2->Height = pBitmap1->Height;
  pBitmap2->Width = pBitmap1->Width;

  for (int y = 0; y < pBitmap2->Height; y++){
      ptr1 = (Byte*)(pBitmap1->ScanLine[y]);
      ptr2 = (Byte*)(pBitmap2->ScanLine[((pBitmap1->Height - 1) - y)]);
      for (int x = 0; x < pBitmap1->Width; x++)
        ptr2[x] = ptr1[x];
  };

  Image1->Canvas->Draw(0,0,pBitmap2);
}
//—————————————————————————

void __fastcall TForm1::btnR90Click(TObject *Sender)
{
  Image1->Canvas->FillRect(Image1->Canvas->ClipRect);
  pBitmap2->Height = pBitmap1->Width;
  pBitmap2->Width = pBitmap1->Height;

  for (int y = 0; y < pBitmap1->Width; y++){
      ptr2 = (Byte*)(pBitmap2->ScanLine[y]);
      for (int x = 0; x < pBitmap1->Height; x++){
        ptr1 = (Byte*)(pBitmap1->ScanLine[x]);
        ptr2[x] = ptr1[y];
      };
  };

  Image1->Canvas->Draw(0,0,pBitmap2);

}

以前写的,参考下

实现了对图像的旋转–小只

Unit1.h
//—————————————————————————

#ifndef Unit1H
#define Unit1H
//—————————————————————————
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Graphics.hpp>
//—————————————————————————
class TForm1 : public TForm
{
__published: // IDE-managed Components
    TImage *Image1;
    TButton *Button1;
    TButton *Button2;
    TButton *Button3;
    TButton *Button4;
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
    void __fastcall Button3Click(TObject *Sender);
    void __fastcall Button4Click(TObject *Sender);
private: // User declarations
public: // User declarations
    __fastcall TForm1(TComponent* Owner);
};
//—————————————————————————
extern PACKAGE TForm1 *Form1;
//—————————————————————————
#endif

Unit1.cpp

//—————————————————————————

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//—————————————————————————
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//—————————————————————————
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//—————————————————————————

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    int width,height;
    Graphics::TBitmap *newbmp;
    newbmp=new Graphics::TBitmap;
    newbmp->Width=Image1->Width;
    newbmp->Height=Image1->Height;
    width=Image1->Width;
    height=Image1->Height;
    for(int j=0;j <=height;j++)
    {

        newbmp->Canvas->CopyRect(Rect(0,height-j,width,height-j-1),Image1->Canvas,Rect(0,j,width,j+1));    //Turn 180  + symmetry
    }

    Form1->Canvas->Draw(width*3,0,newbmp);

    delete newbmp;
}
//—————————————————————————
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    int width,height;
    Graphics::TBitmap *newbmp;
    newbmp=new Graphics::TBitmap;
    newbmp->Width=Image1->Height;
    newbmp->Height=Image1->Width;
    width=Image1->Width;
    height=Image1->Height;

    for(int j=0;j <=height;j++)
    {
        for(int i=0;i <width;i++)
        {
          //newbmp->Canvas->CopyRect(Rect(height-j,i,height-(j+1),i+1),Image1->Canvas,Rect(i,j,i+1,j+1));  // turn right 90
          newbmp->Canvas->CopyRect(Rect(j,width-i,j+1,width-i+1),Image1->Canvas,Rect(i,j,i+1,j+1));  // turn left 90
        }
    }

    Form1->Canvas->Draw(width*4,0,newbmp);

    delete newbmp;
}

void __fastcall TForm1::Button3Click(TObject *Sender)
{
    int width,height;
    Graphics::TBitmap *newbmp;
    newbmp=new Graphics::TBitmap;
    newbmp->Width=Image1->Width;
    newbmp->Height=Image1->Height;
    width=Image1->Width;
    height=Image1->Height;

    for(int j=0;j <=height;j++)
    {
        for(int i=0;i <width;i++)
        {

          newbmp->Canvas->CopyRect(Rect(i,j,i+1,j+1),Image1->Canvas,Rect(width-i-1,height-j-1,width-i,height-j));
          //newbmp->Canvas->CopyRect(Rect(width-i-1,height-j-1,width-i,height-j),Image1->Canvas,Rect(i,j,i+1,j+1));
        }
    }

    Form1->Canvas->Draw(width*2,0,newbmp);

    delete newbmp;

}
//—————————————————————————
void __fastcall TForm1::Button4Click(TObject *Sender)
{
    int width,height;
    Graphics::TBitmap *newbmp;
    newbmp=new Graphics::TBitmap;
    newbmp->Width=Image1->Width;
    newbmp->Height=Image1->Height;
    width=Image1->Width;
    height=Image1->Height;
    for(int j=0;j <=height;j++)
    {
        for(int i=0;i <width;i++)
        {
            newbmp->Canvas->CopyRect(Rect(width-i-1,j,width-i,j+1),Image1->Canvas,Rect(i,j,i+1,j+1));  //symmetry
        }

    }

    Form1->Canvas->Draw(width,0,newbmp);

    delete newbmp;
}
//—————————————————————————

包括了左右对称,旋转90,180,360

引用 10 楼 wwp3321 的回复:
包括了左右对称,旋转90,180,360

我是用一个矩阵的数组来实现画一些不规则的方块,上述变形后我怎么再次确定他们的坐标呢?我不知道怎么定位哦。