[Gtk-sharp-list] nplot + gtk#?

Ben Maurer bmaurer@ximian.com
Sun, 06 Feb 2005 22:24:42 -0500


On Mon, 2005-02-07 at 15:53 +0800, Mark Gallop wrote:
> Hello all,
> 
> I have been trying to get nplot 0.9.8.3 (http://netcontrols.org/nplot/) 
> to work with gtk/glade# but I can't work out how to correctly get the 
> returned system.drawing.graphics into a gtk.image.
> 
> Currently I am using with following:
> ...
>        NPlot.PlotSurface2D plotSurface;
>        [Widget] Gtk.Image image1; 
> ...
>        Bitmap b = new Bitmap (Xmax, Ymax);
>        Graphics g = Graphics.FromImage (b);
>        g.FillRectangle  (Brushes.White, 0, 0, Xmax, Ymax);
>        Rectangle bounds = new Rectangle (0, 0, Xmax, Ymax);
>        plotSurface.Draw(g,bounds);
>               b.Save ("file.png", ImageFormat.Png);
>        image1.FromFile = "file.png";
> 
> This works but doesn't seem very efficient. Should I not be using a 
> gtk.image? Should I be using a pixmap somewhere? Could someone please 
> give me some hints?
> 
> I am developing in linux with mono 1.0.5 and gtk# 1.0.4 but I would also 
> like it to run in win32 with .net/gtk#.  Thanks in advance for any help.

Try using the Gtk.DotNet stuff that is in HEAD now (you can c&p the
code, if you don't want head).

You should do something like

class Blah : DrawingArea {
	Gdk.Pixmap bitmap_cache;
	//System.Drawing.Bitmap bitmap_cache;
	Gdk.Rectangle allocation;	// The current allocation. 
	bool allocated = false;
			       
	protected override bool OnExposeEvent (Gdk.EventExpose args)
	{
		
		if (bitmap_cache == null) {
			bitmap_cache = new Gdk.Pixmap (GdkWindow, allocation.Width, allocation.Height, -1);
			bitmap_cache.DrawRectangle (Style.WhiteGC, true, 0, 0,
				allocation.Width, allocation.Height);
			
			using (Graphics g = Gtk.DotNet.Graphics.FromDrawable (bitmap_cache)) {
				// draw to the Graphics here.
			}
		}
		
		Gdk.Rectangle area = args.Area;
		GdkWindow.DrawDrawable (Style.BlackGC,
						bitmap_cache,
						area.X, area.Y,
						area.X, area.Y,
						area.Width, area.Height);
		
		return true;
	}
	
	protected override void OnSizeAllocated (Gdk.Rectangle allocation)
	{
		allocated = true;
		this.allocation = allocation;
		UpdateCache ();
		base.OnSizeAllocated (allocation);
	}

	public void UpdateCache ()
	{
		if (bitmap_cache != null)
			bitmap_cache.Dispose ();
			
		bitmap_cache = null;
	}
}

-- Ben