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 thumbna开发者_JAVA百科il.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.
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:
- Use runtime packages. This will enforce a shared runtime.
- Use COM interop. COM was designed for sharing components across module boundaries.
- Link all code into a single executable.
- Pass HBITMAPs between the modules since they can be shared in such a manner.
精彩评论