2011-09-19 63 views
1

我想用這將在下文中提到德爾福DLL映像stretchdraw錯誤

{ to resize the image } 
function ResizeImg(maxWidth,maxHeight: integer;thumbnail : TBitmap): TBitmap; 
var 
thumbRect : TRect; 
begin 
thumbRect.Left := 0; 
thumbRect.Top := 0; 

if thumbnail.Width > maxWidth then 
    begin 
    thumbRect.Right := maxWidth; 
    end 
else 
    begin 
    thumbRect.Right := thumbnail.Width;; 
    end; 

if thumbnail.Height > maxHeight then 
    begin 
    thumbRect.Bottom := maxHeight; 
    end 
else 
    begin 
    thumbRect.Bottom := thumbnail.Height; 
    end; 
thumbnail.Canvas.StretchDraw(thumbRect, thumbnail) ; 

    //resize image 
thumbnail.Width := thumbRect.Right; 
thumbnail.Height := thumbRect.Bottom; 

//display in a TImage control 
Result:= thumbnail; 
end; 

當我使用這個應用程序調用它正常工作,一個DLL函數(養活所有的圖像調整大小(規模)的位圖圖像我的列表視圖):

//bs:TStream; btmap:TBitmap; 
    bs := CreateBlobstream(fieldbyname('Picture'),bmRead); 
    bs.postion := 0; 
    btmap.Loadfromstream(bs); 
    ListView1.Items[i].ImageIndex := ImageList1.Add(ResizeImg(60,55,btmap), nil); 

但是當我嘗試此應用程序調用(以獲得單個圖像到我的TImage組件):

bs := CreateBlobstream(fieldbyname('Picture'),bmRead); 
bs.postion := 0; 
btmap.Loadfromstream(bs); 
Image1.Picture.Bitmap := ResizeImg(250,190,btmap); 

它給了我一個錯誤的:

thumbnail.Canvas.StretchDraw(thumbRect, thumbnail) ; 

說:

AV at address 00350422 in module 'mydll.dll' Read of Address 20000027 

當我結束我的可執行文件,我得到這樣的:

runtime error 216 at 0101C4BA 

如果我定義和使用相同的函數(ResizeImg)在我的EXE pas文件裏面,它工作得很好,沒有任何錯誤。

+0

該函數意味着給出完全不同的TBitmap,但它只是返回輸入TBitmap。我建議將這個例程重構成一個過程。 – NGLN

+0

我不能使用它的過程,因爲我將函數的結果分配給其他組件 – Shirish11

回答

3

您不能在模塊之間傳遞Delphi對象,除非您採取措施確保這些模塊共享相同的運行時和內存分配器。愛爾蘭似乎你沒有采取這樣的步驟。

基本問題是一個Delphi對象是數據和代碼。如果您在一個不同的模塊中創建的對象上天真地調用某個方法,那麼您可以從該模塊的數據中執行該模塊中的數據。這通常以運行時錯誤結束。

你至少有以下選項:

  1. 使用運行時包。這將強制共享運行時。
  2. 使用COM互操作。 COM旨在跨模塊邊界共享組件。
  3. 將所有代碼鏈接到單個可執行文件中。
  4. 在模塊之間傳遞HBITMAP,因爲它們可以以這種方式共享。
+0

感謝第三個選項適用於我 – Shirish11