[英]Image rotation and quality loss after several rotations
通過這段代碼,我旋轉了一些圖像,我看到在第二次旋轉后,圖像立即丟失了信息。 我認為問題是由於圖像居中引起的,但事實並非如此。 可能是什么問題呢?
void RotatePicture1(double myangle, TObject *Sender)
{
XFORM xForm;
Graphics::TBitmap *SrcBitmap = new Graphics::TBitmap;
Graphics::TBitmap *DestBitmap = new Graphics::TBitmap;
SrcBitmap->PixelFormat=pf24bit;
DestBitmap->PixelFormat=pf24bit;
TImage *MyImage = (TImage*)Sender;
SrcBitmap->Width=MyImage->Width;
SrcBitmap->Height=MyImage->Height;
DestBitmap->Width=MyImage->Width;
DestBitmap->Height=MyImage->Height;
SrcBitmap->Assign(MyImage->Picture->Bitmap);
SetGraphicsMode(DestBitmap->Canvas->Handle, GM_ADVANCED);
double fangle = (double)(myangle / 180.0) * M_PI;
int x0=SrcBitmap->Width/2;
int y0=SrcBitmap->Height/2;
double cx=x0 - cos(fangle)*x0 + sin(fangle)*y0;
double cy=y0 - cos(fangle)*y0 - sin(fangle)*x0;
xForm.eM11 = (FLOAT) cos(fangle);
xForm.eM12 = (FLOAT) sin(fangle);
xForm.eM21 = (FLOAT) -sin(fangle);
xForm.eM22 = (FLOAT) cos(fangle);
xForm.eDx = (FLOAT) cx;
xForm.eDy = (FLOAT) cy;
SetWorldTransform(DestBitmap->Canvas->Handle, &xForm);
int offset = (MyImage->Width - MyImage->Height) / 2;
BitBlt(
DestBitmap->Canvas->Handle, //A handle to the destination device context.
0, //The x-coordinate, of the upper-left corner of the destination rectangle.
0, //The y-coordinate, of the upper-left corner of the destination rectangle.
SrcBitmap->Width, //The width, of the source and destination rectangles.
SrcBitmap->Height, //The height, of the source and the destination rectangles.
SrcBitmap->Canvas->Handle, //A handle to the source device context
0, //The x-coordinate, of the upper-left corner of the source rectangle.
0, //The y-coordinate, of the upper-left corner of the source rectangle.
SRCCOPY);
MyImage->Picture->Bitmap->Assign(DestBitmap);
delete DestBitmap;
delete SrcBitmap;
}
旋轉前:
幾輪之后:
XFORM.eDx
和XFORM.eDy
不用於旋轉操作。 WinAPI 輪換對我來說效果很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.