下面的代码放在主程序中没问题的。 Bitmap2 := TBitmap.Create;
 Bitmap2.PixelFormat := Bitmap1.PixelFormat;
 Bitmap2.Assign(Bitmap1);但如果放在 dll中,调试时,提示"can not assign a TBitmap to a TBitmap",这是为什么呢?

解决方案 »

  1.   

    dll中要引用graphics单元
    uses  graphics
      

  2.   

    我只知道Bitmap2.PixelFormat := Bitmap1.PixelFormat;是一句多余的代码
      

  3.   

    http://topic.csdn.net/t/20031028/17/2403781.html
    找到个例子
      

  4.   

    你还是把你的dll代码发一下过来吧,我试了一下以下代码,完全没问题。
    这是我写的dll中的代码,完全OK啊。你可以拿去试试。library Project1;
    uses
      SysUtils,graphics,
      Classes;  procedure x();stdcall;
      var
      Bitmap1:Tbitmap;
      Bitmap2:Tbitmap;
      begin
       BitMap1 := TBitMap.Create;
       BitMap1.LoadFromFile('c:\1.bmp');
       Bitmap2 := TBitmap.Create;
       Bitmap2.PixelFormat := Bitmap1.PixelFormat;
       Bitmap2.Assign(Bitmap1);
      end;
      exports x;
    {$R *.RES}begin
    end.
      

  5.   

    同一份代码,在EXE和DLL中,是两个不同的拷贝。EXE和DLL中的graphics单元实际是不同的,因此它们各自的TBitmap虽然名称一样,但实际是两个不同的类(地址不同),因此不能assign。解决方法是共享包含graphics单元的bpl(rtl.bpl)。详细请查询“DLL类型穿透问题”
      

  6.   

    顺便,你的两个BITMAP一个在主程序,一个在DLL中才会有问题。
      

  7.   

    恩,楼上高见。
    楼主你应该发代码的,看来的确应该是楼上说的,你的BITmap1和Bitmap2应该是分别在主程序和DLL中。
      

  8.   

    uses  SysUtils,
      Classes,
      Dialogs,windows,
      math,Graphics,Variants;  function Sharp(var MyBmp:TBitmap;Threshold:integer):integer;stdcall;
         var
           x,y:integer;
           xx,yy,i:integer;
           itemp:integer;       newBmp: TBitmap;
           p: array[-4..4] of PByteArray;
           new_p: PByteArray;
         begin
          newBmp := TBitmap.Create;
          newBmp.PixelFormat := MyBmp.PixelFormat;
          newBmp.Assign(MyBmp);以上是我dll的代码,执行到最后一句出错的。
      

  9.   

    类似的问题我在C++Builder中也遇到过。症状主要表现在App中创建的对象和Dll中创建的对象(如TBitmap和TStringList等)互相Assign时产生问题。貌似要用到ShareMem单元才能解决。
      

  10.   

    非常感谢6楼的热心,你说的太对了,我用了个傻办法解决, 
    function Sharp(var MyBmp,newBmp:TBitmap;Threshold:integer):integer;stdcall;
    先在主程序里把newBmp.Assign(MyBmp);就可以了。
    你说的“DLL类型穿透问题”,还不理解。
      

  11.   

    DLL类型穿透问题大概就是之前跟你描述的那个了,由于两者的类地址实际不同,导致在不共享BPL的前提下,跨DLL(EXE)传递的类无法用is as操作(应该说得不到正确的结果)