簡體   English   中英

使用 Borland C++ Builder 和 Windows API 函數旋轉圖像

[英]Rotating an image using Borland C++ Builder and Windows API functions

我構建了這個示例以快速將圖像旋轉 90 度,但我總是在側面切下圖像。 經過多次測試,不幸的是我仍然不明白問題的原因。

void rotate()
{
    Graphics::TBitmap *SrcBitmap = new Graphics::TBitmap;
    Graphics::TBitmap *DestBitmap = new Graphics::TBitmap;
    
    SrcBitmap->LoadFromFile("Crayon.bmp");
    
    DestBitmap->Width=SrcBitmap->Width;
    DestBitmap->Height=SrcBitmap->Height;

    SetGraphicsMode(DestBitmap->Canvas->Handle, GM_ADVANCED);
    
    double myangle = (double)(90.0 / 180.0) * 3.1415926;
    int x0=SrcBitmap->Width/2;
    int y0=SrcBitmap->Height/2; 
    double cx=x0 - cos(myangle)*x0 + sin(myangle)*y0;
    double cy=y0 - cos(myangle)*y0 - sin(myangle)*x0;
    
    xForm.eM11 = (FLOAT) cos(myangle);
    xForm.eM12 = (FLOAT) sin(myangle);
    xForm.eM21 = (FLOAT) -sin(myangle);
    xForm.eM22 = (FLOAT) cos(myangle);
    xForm.eDx  = (FLOAT) cx;
    xForm.eDy  = (FLOAT) cy;

    SetWorldTransform(DestBitmap->Canvas->Handle, &xForm);  
    
    BitBlt(DestBitmap->Canvas->Handle,
    0,
    0,
    SrcBitmap->Width,
    SrcBitmap->Height,
    SrcBitmap->Canvas->Handle,
    0,
    0,
    SRCCOPY);
    
    DestBitmap->SaveToFile("Crayon2.bmp");
    
    delete DestBitmap;
    delete SrcBitmap;
}   

如果旋轉整個圖像,則應翻轉目標圖像的寬度和高度:

DestBitmap->Width = SrcBitmap->Height;
DestBitmap->Height = SrcBitmap->Width;

變換例程根據原始寬度/高度將圖像居中。 我們要調整 x/y 位置以將BitBlt的起點推到左/上

int offset = (SrcBitmap->Width - SrcBitmap->Height) / 2;
BitBlt(DestBitmap->Canvas->Handle, offset, offset, SrcBitmap->Width, SrcBitmap->Height,
    SrcBitmap->Canvas->Handle, 0, 0, SRCCOPY);

有一次我遇到了類似的問題。 我想圍繞一個共同的旋轉點旋轉兩個圖像。 但是我不能用標准函數來做,因為它不允許旋轉點分散到中心。 盡管如此,我當時已經對標准功能做了筆記。 也許他們會幫助你。 我記得目標圖像的大小正確很重要! 如果縱向圖像變成橫向圖像,圖像變得更寬,因此 BitBlt 函數還必須指定目標圖像的大小。

這是我對標准功能的說明。 填充 xForm 參數對我來說與您的代碼片段中的不太一樣。

在此處輸入圖片說明


這就是我用來圍繞任何中心旋轉的功能。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM