1

我正在尝试使用下面提到的 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),它可以完全正常工作,没有任何错误。

4

1 回答 1

3

除非您采取措施确保这些模块共享相同的运行时和内存分配器,否则您不能在模块之间传递 Delphi 对象。看来你还没有采取这样的步骤。

基本问题是 Delphi 对象既是数据又是代码。如果您天真地调用在不同模块中创建的对象的方法,那么您将在该模块中的数据上执行该模块中的代码。这通常以运行时错误结束。

您至少有以下选择:

  1. 使用运行时包。这将强制执行共享运行时。
  2. 使用 COM 互操作。COM 旨在跨模块边界共享组件。
  3. 将所有代码链接到一个可执行文件中。
  4. 在模块之间传递 HBITMAP,因为它们可以以这种方式共享。
于 2011-09-19T08:44:51.037 回答