Delphi dll图像拉伸错误

发布于 2024-12-05 12:39:59 字数 1624 浏览 1 评论 0原文

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

I am trying to resize (scale) a bitmap image using a dll function which is below mentioned

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

It works fine when I use this application call (to feed all the images in my listview):

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

But when I try this application call (to get individual image into my TImage component):

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

It gives me an error on:

 thumbnail.Canvas.StretchDraw(thumbRect, thumbnail) ;

saying:

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

And when I close my executable I get this:

 runtime error 216 at 0101C4BA 

If I define and use the same function (ResizeImg) inside my exe pas file it works completely fine without any errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽梦紫曦~ 2024-12-12 12:39:59

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

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

您至少有以下选择:

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

You can't pass Delphi objects between modules unless you take steps to ensure that those modules share the same runtime and memory allocator. Ir appears you have not taken such steps.

The basic problem is that a Delphi object is both data and code. If you naively call a method on an object that was created in a different module then you execute code from this module on data from that module. That typically ends in runtime errors.

You have at least the following options:

  1. Use runtime packages. This will enforce a shared runtime.
  2. Use COM interop. COM was designed for sharing components across module boundaries.
  3. Link all code into a single executable.
  4. Pass HBITMAPs between the modules since they can be shared in such a manner.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文