簡體   English   中英

Delphi DLL圖像StretchDraw錯誤

[英]Delphi dll image stretchdraw errors

我正在嘗試使用下面提到的dll函數調整位圖圖像的大小(縮放)

{ 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;

當我使用此應用程序調用(以提供列表視圖中的所有圖像)時,它工作正常:

  //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 

如果我在exe pas文件中定義並使用相同的函數( ResizeImg ),它將完全正常運行而不會出現任何錯誤。

除非采取步驟確保這些模塊共享相同的運行時和內存分配器,否則您不能在模塊之間傳遞Delphi對象。 看來您還沒有采取這樣的步驟。

基本問題是Delphi對象既是數據又是代碼。 如果您天真地調用在另一個模塊中創建的對象的方法,那么您將對該模塊中的數據執行該模塊中的代碼。 通常以運行時錯誤結束。

您至少具有以下選擇:

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

暫無
暫無

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

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