有关图片旋转的问题!!
真是不好意思,我又来问问题了@@~因为我又遇到瓶颈了!!!
依照妖哥的写法,我修改了水平翻转,变成了垂直翻转!但是~九十度旋转的部分我就推导不出来了!!
真是丢人!自己都想不怎么出来!!
我知道 bmpDst->Width = nHeight;
bmpDst->Height = nWidth; 这两句要修改成这样!因为长宽改变!
书上的公式是~
I[x] [y]代表翻转/旋转前;
O[x][y]代表翻转/旋转后;
X & Y的范围是:
0<=x<=Width-1;
0<=y<=Height-1;
水平翻转:O[x][Height-1-y] = I[x][y];
=>TRect rctDst(nWidth - 1, 0, -1, nHeight);
垂直翻转:O[Width-1-x][y] = I[x][y];
=>TRect rctDst(0, nHeight - 1, nWidth, -1);
//不知道是否这样!测试是可以垂直翻转,但是不知道是不是真的这样写!
顺时针旋转九十度:O[Height-1-y][x]= I[x][y];
逆时针旋转九十度:O[y][Width-1-x] = I[x][y];
我真的很逊,怎样就是弄不出来,可否请教一下各位前辈,该怎样修改呢?
我试着去修改,变成~TRect rctDst(0, nWidth - 1, nHeight,-1 );(顺时针九十度)
但是,大错特错啊!
像这样旋转的问题,他是固定一个角吗?还是说要怎样去想呢?
烦请各位大大跟小妹开释一下!感谢~
[解决办法]
可以参考这个,c#图片旋转代码.可以指定角度来旋转
http://download.csdn.net/source/933744
[解决办法]
找了一段代码给你,你参考一下,但是这种办法虽说通用可行,但是速度比较慢。推荐你用一个控件Envision Image Library。这个组件旋转速度快,还支持扫描仪。估计你的项目里面不会不需要这部分功能的。
void __fastcall TForm4::SpeedButton2Click(TObject *Sender)
{
//旋转180度
long lXTemp,lYTemp;
TColor tTempColor;
long lPicWidth,lPicHeight;
lPicWidth = Image1->Width ;
lPicHeight=Image1->Height;
Graphics::TBitmap *bmp = new Graphics::TBitmap();
bmp->Assign(Image1->Picture->Bitmap);
for(lYTemp=0;lYTemp <lPicHeight;lYTemp ++)
{
for(lXTemp=0;lXTemp <=lPicWidth;lXTemp ++)
{
tTempColor=bmp->Canvas->Pixels[lXTemp][lYTemp];
Image1->Canvas->Pixels[lPicWidth - lXTemp][lPicHeight - lYTemp]=tTempColor;
}
}
Image1->Update() ;
delete bmp;
}
//---------------------------------------
void __fastcall TForm4::SpeedButton1Click(TObject *Sender)
{ //水平翻转
long lXTemp,lYTemp;
TColor tTempColor;
long lPicWidth,lPicHeight;
lPicWidth = Image1->Width ;
lPicHeight=Image1->Height;
Graphics::TBitmap *bmp = new Graphics::TBitmap();
bmp->Assign(Image1->Picture->Bitmap);
for(lYTemp=0;lYTemp <lPicHeight;lYTemp ++)
{
for(lXTemp=0;lXTemp <=lPicWidth;lXTemp ++)
{
tTempColor=bmp->Canvas->Pixels[lXTemp][lYTemp];
Image1->Canvas->Pixels[lPicWidth - lXTemp][lYTemp]=tTempColor;
}
}
Image1->Update() ;
delete bmp;
}
//---------------------------------------
void __fastcall TForm4::SpeedButton3Click(TObject *Sender)
{
//垂直翻转
long lXTemp,lYTemp;
TColor tTempColor;
long lPicWidth,lPicHeight;
lPicWidth = Image1->Width ;
lPicHeight=Image1->Height;
Graphics::TBitmap *bmp = new Graphics::TBitmap();
bmp->Assign(Image1->Picture->Bitmap);
for(lYTemp=0;lYTemp <lPicHeight;lYTemp ++)
{
for(lXTemp=0;lXTemp <=lPicWidth;lXTemp ++)
{
tTempColor=bmp->Canvas->Pixels[lXTemp][lYTemp];
Image1->Canvas->Pixels[lXTemp][lPicHeight - lYTemp]=tTempColor;
}
}
Image1->Update() ;
delete bmp;
}
//---------------------------------------
void __fastcall TForm4::SpeedButton4Click(TObject *Sender)
{
//左旋转90度
Graphics::TBitmap *bmp = new Graphics::TBitmap();
bmp->Width=Image1->Height;
bmp->Height=Image1->Width;
static TRect sourcepix,destpix,fullbufferimage,fulldestimage;
fullbufferimage.Left= 0;
fullbufferimage.Top= bmp->Height;
fullbufferimage.Right= bmp->Width;
fullbufferimage.Bottom= 0;
for (int y=0; y <Image1->Height; y++)
{
for (int x=0; x <Image1->Width; x++)
{
sourcepix.Left= x;
sourcepix.Top= y+1;
sourcepix.Right= x+1;
sourcepix.Bottom= y;
destpix.Left=y;
destpix.Top=bmp->Height-x;
destpix.Right=y+1;
destpix.Bottom=bmp->Height-x-1;
bmp->Canvas->CopyRect(destpix,Image1->Canvas,sourcepix);
}
}
Image1->Width=bmp->Width;
Image1->Height=bmp->Height;
Image1->Picture->Bitmap->Width=bmp->Width;
Image1->Picture->Bitmap->Height=bmp->Height;
Image1->Canvas->CopyRect(fullbufferimage,bmp->Canvas,fullbufferimage);
delete bmp;
}
//---------------------------------------
void __fastcall TForm4::SpeedButton6Click(TObject *Sender)
{
//转成黑白
long lXTemp,lYTemp;
TColor tTempColor;
long lPicWidth,lPicHeight;
int iR,iG,iB;
lPicWidth = Image1->Width ;
lPicHeight=Image1->Height;
Graphics::TBitmap *bmp = new Graphics::TBitmap();
bmp->Assign(Image1->Picture->Bitmap);
for(lYTemp=0;lYTemp <lPicHeight;lYTemp ++)
{
for(lXTemp=0;lXTemp <=lPicWidth;lXTemp ++)
{
tTempColor=bmp->Canvas->Pixels[lXTemp][lYTemp];
iR=GetRValue(tTempColor);
iG=GetGValue(tTempColor);
iB=GetBValue(tTempColor);
iR=iG=iB=(iR+iG+iB)/3;
tTempColor=(TColor)RGB(iR,iG,iB);
Image1->Canvas->Pixels[lXTemp][lYTemp]=tTempColor;
}
}
Image1->Update() ;
delete bmp;
}
//---------------------------------------
void __fastcall TForm4::SpeedButton5Click(TObject *Sender)
{
//裁剪
if ((y2 - y1) <= 0 ||(x2 - x1) <= 0 )
{
MessageBox(this->Handle,"请用鼠标在图像上框取区域","提示",NULL);
return ;
}
Graphics::TBitmap *bmp = new Graphics::TBitmap();
bmp->Height = y2 - y1;
bmp->Width = x2 - x1;
bmp->Canvas->CopyRect(Rect(0,0,x2 - x1,y2 - y1),Image1->Picture->Bitmap->Canvas,Rect(x1,y1,x2,y2));
Image1->Picture->Bitmap->Assign(bmp);
delete bmp;
x1=0;
y1=0;
x2=0;
y2=0;
Shape1->Visible = false;
}
//---------------------------------------
void __fastcall TForm4::Image1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if(!Shift.Contains(ssLeft))
return;
x1=X;
y1=Y;
PP.x = X;
PP.y = Y;
Shape1->Shape=stRectangle;
Shape1->Top = Y-ScrollBox1->VertScrollBar->Position;
Shape1->Left = X - ScrollBox1->HorzScrollBar->Position;
Shape1->Width = 1;
Shape1->Height = 1;
Shape1->Visible = true;
}
//---------------------------------------
void __fastcall TForm4::Image1MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if(!Shift.Contains(ssLeft))
return;
x2=X;
y2=Y;
Shape1->Width = X-PP.x;
Shape1->Height = Y-PP.y;
}
//---------------------------------------
void __fastcall TForm4::FormCreate(TObject *Sender)
{
x1=0;
y1=0;
x2=0;
y2=0;
ScrollBox1->DoubleBuffered = true;
Shape1->Visible = false;
}
//---------------------------------------
void __fastcall TForm4::SpeedButton7Click(TObject *Sender)
{
//右旋转90度
Graphics::TBitmap *bmp = new Graphics::TBitmap();
bmp->Width=Image1->Height;
bmp->Height=Image1->Width;
static TRect sourcepix,destpix,fullbufferimage,fulldestimage;
fullbufferimage.Left= 0;
fullbufferimage.Top= bmp->Height;
fullbufferimage.Right= bmp->Width;
fullbufferimage.Bottom= 0;
for (int y=0; y <Image1->Height; y++)
{
for (int x=0; x <Image1->Width; x++)
{
sourcepix.Left= x;
sourcepix.Top= y+1;
sourcepix.Right= x+1;
sourcepix.Bottom= y;
destpix.Left=Image1->Height-y;
destpix.Top=x;
destpix.Right=Image1->Height-y-1;
destpix.Bottom=x+1;
bmp->Canvas->CopyRect(destpix,Image1->Canvas,sourcepix);
}
}
Image1->Width=bmp->Width;
Image1->Height=bmp->Height;
Image1->Picture->Bitmap->Width=bmp->Width;
Image1->Picture->Bitmap->Height=bmp->Height;
Image1->Canvas->CopyRect(fullbufferimage,bmp->Canvas,fullbufferimage);
delete bmp;
}
//---------------------------------------