Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a bitmap to a dll written by Delphi in C#?

I have a Delphi function in a dll written like this:

       function LensFlare(Bitmap: TBitmap; X, Y: Int32; Brightness: Real): TBitmap; StdCall;
       Begin
         // ...
         Result := Bitmap;
       End;

I want to use it in C#, I have tried this but I was not successful:

    [DllImport("ImageProcessor")]
    static extern Bitmap LensFlare(Bitmap bitmap, int x, int y, double Brightness);

    private void button1_Click(object sender, EventArgs e)
    {
        Bitmap b = new Bitmap(@"d:\a.bmp");
        pictureBox1.Image = LensFlare(b, 100, 100, 50); // Error!
    }

Error: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

How can I do this?

like image 530
Mohammad Ahmadzadeh Avatar asked Jan 29 '23 15:01

Mohammad Ahmadzadeh


1 Answers

You can't use this function. It's not even safe to use between two Delphi modules unless you use packages. You cannot pass native Delphi classes across a module boundary like that.

You'll need to switch to an interop friendly type. The obvious option is to use an HBITMAP. You will need to modify the Delphi library. If you don't have the source you will need to contact the original developer.

like image 97
David Heffernan Avatar answered Feb 02 '23 09:02

David Heffernan