views:

494

answers:

6

Hello,

I am working on a project where I'm going to create a user interface in a C# windows application. It needs to display a picture. And I want to transfer the picture data to a C++ module that will edit it and then return the result.

I've already established how to transfer data between C# and C++ (I use a C++ DLL). My question is: which class in C# can do what I want? I need it to export the image data into some kind of a pixel matrix (preferably with separated RGB values, each of size 1 byte = 3 bytes per pixel) that I can work with in the C++ module. Also, it needs to be able to import the result back into the object, and display in the C# windows application window.

Any ideas which class might work best for me?

Thanks, Malki.

+3  A: 

If you want to work with pixel data as you describe, then Bitmap.LockBits is the method for you.

danbystrom
A: 

What does your dll's method expect as an input parameter(s)?

EDIT: As far as i know, in c# you need to go into "unsafe mode" to work with pixels directly (and fast:)). Something like this.

A: 

In addition to danbystrom's answer about Bitmap.LockBits you will want to look at the Marshal class for copying binary data. Check the Marshal.Copy method for an example.

Rune Grimstad
A: 

I reply to marko.sabotin: I haven't written the C++ methods yet, but I need access to pixels, so I'm guessing it will be a byte matrix, as described in my question.

Thanks guys, I'll look into these classes you mentioned :)

A: 

In answer to you all! :-)

No, you don't need any marshaling and you do not need any unsafe code if you just want to pass the pixels to native C++.

You can check out this post were I do exactly this, calling a C++ function containing hand crafted assembly language to do some image processing:

http://danbystrom.se/2008/12/22/optimizing-away-ii/

danbystrom
A: 

Malki, can i get your email..I am doing a very similar project and would love to discuss things. thanks. send me an [email protected]