2011-09-19 62 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