Sign in to follow this  
thedustbustr

c# unsafe code: pinning asynchronously

Recommended Posts

asynchronous might be the wrong word. I'm putting a C# GUI around a C++/CLI class. The GUI registers a C# callback with C++, this callback returns a writable pointer to a bitmap. Is this even possible to do with the unsafe/fixed keywords? I need to hold my C# image pointer fixed starting in the BeginRender callback, and ending in the EndRender callback. The image pointer cannot move between those calls.
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            BeginRender_callback = new ManagedSs.BeginRender_callback(BeginRender);
            EndRender_callback = new ManagedSs.EndRender_callback(EndRender);
            Resize_callback = new ManagedSs.Resize_callback(Resize_);

            ss = new ManagedSs();

            ss.RegisterBeginRender(BeginRender_callback);
            ss.RegisterEndRender(EndRender_callback);
            ss.RegisterResize(Resize_callback);

            ss.Connect("localhost");
        }

        public unsafe sbyte* BeginRender()
        {
            //pin image buf
            return nullptr; // fixed_image_write_pointer;
        }
        public unsafe void EndRender()
        {
            //refresh image
            //unpin
        }
        public void Resize_(int width, int height)
        {
            //resize image
        }

        private ManagedSs.BeginRender_callback BeginRender_callback;
        private ManagedSs.EndRender_callback EndRender_callback;
        private ManagedSs.Resize_callback Resize_callback;

        private ManagedSs;
    }




[Edited by - thedustbustr on July 2, 2008 9:53:14 AM]

Share this post


Link to post
Share on other sites
New problem. I can write to the bitmap, but the containing picturebox does not update. If I call picturebox.invalidate(), nothing happens, probably because this method is being executed as a callback from a separate thread. if I use picturebox.update(), it crashes telling me that I can't call that from a separate thread. if I use picturebox.invoke to call invalidate through a delegate, still nothing happens.

For googlers, here is the solution to the original problem:

        public unsafe sbyte* BeginRender()
{
Rectangle locked_area =
new Rectangle(0,0,target_image.Width, target_image.Height);
target_data = target_image.LockBits(
locked_area,
System.Drawing.Imaging.ImageLockMode.WriteOnly,
target_image.PixelFormat);

IntPtr pixels = target_data.Scan0;
return (sbyte*)pixels;
}
public unsafe void EndRender()
{
target_image.UnlockBits(target_data);
pictureBox1.Invalidate();
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this