[Mono-osx] System.Drawing into Cocoa views?

Alain Bocherens alain at slide-effect.com
Mon Jul 18 04:02:28 EDT 2011


Hi Eric,

I faced the exact same problem. I wrote a small class to handle this 
case. It is derived from NSView and you only have to inherit from it and 
wrote your own OnPaint method.

Here it is:
------------------------------------------------------------------------
using System;
using System.Drawing;
using MonoMac.AppKit;
using MonoMac.Foundation;

namespace SEForms.Forms
{
     public class SECustomView
     {
         public class CustomView : NSView
         {
             private NSImage _buffer=null;
             public override bool IsFlipped
             {
             get { return true; }
             }

             public NSImage Buffer
             {
                 set
                 {
                     _buffer = value;
                 }
             }

             public override void DrawRect (RectangleF dirtyRect)
             {
                 if(_buffer!=null)
                 {
                     _buffer.Draw(this.Bounds,new 
RectangleF(0,0,_buffer.Size.Width,_buffer.Size.Height),NSCompositingOperation.Copy,1.0f);
                 }
             }

         }

         private Bitmap _buffer = null;
         private CustomView _innerControl = null;

         public SECustomView ()
         {
             _innerControl = new CustomView();

         }

         public void Invalidate()
         {
             Graphics g = Graphics.FromImage(_buffer);
             OnPaint(g);
             g.Dispose();
             _innerControl.Buffer = MacTools.ConvertToNSImage(_buffer);
             _innerControl.NeedsDisplay = true;
         }

         public Bitmap InternalBuffer
         {
             get
             {
                 return _buffer;
             }
         }

         virtual public void OnPaint(Graphics g)
         {
             // just a test, but otherwrite this method to specialise 
the drawing
             g.DrawRectangle(Pens.HotPink,10,10,100,100);
         }



         public Size Size
         {
             get { return _innerControl.Frame.Size.ToSize(); }
             set
             {
                 _innerControl.Frame = new RectangleF 
(_innerControl.Frame.Location, value);
                 if((_buffer==null) || (_buffer.Size != value))
                 {
                     if(_buffer!=null)
                     {
                         _buffer.Dispose();
                     }
                     //resize buffer
                     _buffer = new 
Bitmap((int)value.Width,(int)value.Height);
                 }
                 Invalidate();
             }
         }

         public static NSImage ConvertToNSImage(Image img)
         {
             System.IO.MemoryStream s = new System.IO.MemoryStream();
             img.Save(s, System.Drawing.Imaging.ImageFormat.Png);
             byte[] b = s.ToArray();
             CGDataProvider dp = new CGDataProvider(b,0,(int)s.Length);
             s.Flush();
             s.Close();
             CGImage img2 = 
CGImage.FromPNG(dp,null,false,CGColorRenderingIntent.Default);
             return new NSImage(img2, new SizeF(img2.Width,img2.Height));
         }
     }
}

------------------------------------------------------------------------

-- 
Alain Bocherens

Slide Effect :: Create Amazing Presentations
http://www.slide-effect.com
Banner Effect :: Flash Banners Made Simple.
http://www.banner-effect.com


Le 17.07.2011 23:59, Eric J. M. Smith a écrit :
> Greetings,
>
> I'm new to MonoMac (and to Mono in general).  So I apologise in advance for my ignorance.
>
> I'm exploring the feasibility of porting a .NET/Windows app to run on the Mac.  I've resigned myself to the fact that we'll be rewriting any code which relies on Windows.Forms widgets.  Our existing rendering code draws into a System.Drawing.Image, and it would be nice to not have to rewrite everything.  Is there any way of drawing into a System.Drawing.Graphics and making the results available to Cocoa?  Either by drawing directly into an NSView or by somehow getting the contents of a System.Drawing.Image into an NSImage.
>
> I did find some mention on this mailing-list of a "drawing bridge" to do libgdiplus drawing in a Cocoa custom view, but I can't find any documentation or samples for this (plus, it sounds like this approach might require X11).
>
> Thanks for your help,
>
> Eric Smith
> Tarkvara Design Inc.
> _______________________________________________
> Mono-osx mailing list
> Mono-osx at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-osx


More information about the Mono-osx mailing list