2012-08-31 23 views
3

下午好。在調用Canvas.DrawBitmap(Firemonkey)之前將轉換應用於位圖

我正在研究一個繪圖程序,允許用戶在畫布上拖放使用位圖加載的TImages。 (在RAD Studio XE2中的Firemonkey HD應用程序中)然後用戶可以在保存圖像之前更改x和y的比例和旋轉角度。 所有TImages都保存在一個列表,然後這個列表是使用這個簡單的程序寫入底層帆布:

for i := 0 to DroppedList.Count - 1 do 
    begin 
    AImage := DroppedList[i]; 
    SourceRect.Left := 0; 
    SourceRect.Right := AImage.Bitmap.Width; 
    SourceRect.Top := 0; 
    Sourcerect.Bottom := AImage.Bitmap.Height; 

    TargetRect.Left := AImage.Position.X; 
    TargetRect.Right := AImage.Position.X + AImage.Bitmap.Width; 
    TargetRect.Top := AImage.Position.Y; 
    TargetRect.Bottom := AImage.Position.Y + AImage.Bitmap.Height; 

    with FImage.Bitmap do 
    begin 
     Canvas.BeginScene; 
     Canvas.DrawBitmap(AImage.Bitmap, SourceRect, TargetRect, 1, True); 
     Canvas.EndScene; 
     BitmapChanged 
    end; 
    end; 

    FImage.Bitmap.SaveToFile('test.bmp'); 

的問題,這是轉變到規模和,在可見的圖像的旋轉窗口不被DrawBitmap考慮在內,保存時會丟失。 我正在尋找一種將繪製到背景之前應用轉換到位圖的方法。 我無法找到關於此的任何信息,所以我希望有人在這裏可以幫助。

謝謝 丹尼爾

+1

你上面的代碼是不執行和轉換或旋轉。它只是在不同的X,Y位置複製每個位圖。你如何在你的應用程序的其他地方進行轉換?你可以重新使用該代碼嗎?你需要一個圖像處理庫來達到你想要達到的目的嗎? –

回答

2

的問題似乎是,縮放和旋轉都applyed到源的TImage。在這個「源TImage」中,轉換不是在位圖上完成的,而是在TImage層次上完成的(因爲它是TControl,所有TControl都可以縮放和旋轉)。稍後將其他位置的源位圖複製,但實際上此位圖從未更改過

所以必須旋轉和縮放循環中的位圖,根據源TImage中的設置:

with FImage.Bitmap do 
begin 
    Canvas.BeginScene;  
    LBmp := TBitmap.Create; 
    try 
    // create a copy on which transformations will be applyed 
    LBmp.Assign(AImage.Bitmap); 
    // rotate the local bmp copy according to the source TImage. 
    if AImage.RotationAngle <> 0 then 
     LBmp.Rotate(AImage.RotationAngle); 
    // scale the local bmp copy... 
    If AImage.Scale.X <> 1 
     then ; 
    Canvas.DrawBitmap(LBmp, SourceRect, TargetRect, 1, True); 
    finally 
    LBmp.Free; 
    Canvas.EndScene; 
    BitmapChanged 
    end; 
end; 

這個簡單的代碼示例很好地解釋了這個問題。例如,RotatationAngle屬於AImage而不是AImage.Bitmap

避免執行轉換的解決方法是使用TControl.MakeScreenshot()。(待驗證,這可能會失敗)

with FImage.Bitmap do 
begin 
    Canvas.BeginScene; 
    LBmpInclTranformations := AImage.MakeScreenShot; 
    Canvas.DrawBitmap(LBmpInclTranformations, SourceRect, TargetRect, 1, True); 
    Canvas.EndScene; 
    BitmapChanged 
end; 
+0

嗨az01。 謝謝你的幫助。現在一切正常。 還有其他人應該碰到這個問題: 您應該按照az01的說法將旋轉應用於BMP,並通過縮放TargetRect來縮放位圖。 – Daniel

+0

@丹尼爾,然後只是['接受這個職位'](http://meta.stackexchange.com/a/5235/179541)作爲答案。您也可以通過點擊投票計數上方的向上箭頭來上傳帖子;-) – TLama

相關問題