[Mono-list] About the thumbnail creator

Benoit Caccinolo benoit.caccinolo@free.fr
Wed, 16 Jun 2004 18:59:06 +0200


On Wed, 2004-06-16 at 18:20, Todd Berman wrote:
> I think the issue is mozilla not getting enough time to render. I would 
> try putting a Thread.Sleep (3000) or something as the first line of code 
> in the NetStop handler.

The problem is surely a problem of time. But with a Thread.Sleep in the
MakeShot method the problem is not solved cause the sleep stops the
rendering too :)

My solution is too put a button to launch the MakeShot method. It is
less elegant but it works.

Thx

Here is the code.

using System;
using Gtk;
using Gecko;

class Thumbnail { 
	static WebControl wc;
	static string output = "shot.png";
	static string url;
	static int width = -1;
	static int height = -1;
	
	static void Main (string [] args)
	{
		for (int i = 0; i < args.Length; i++){
			switch (args [i]){
			case "-width":
				try {
					i++;
					width = Int32.Parse (args [i]);
				} catch {
					Console.WriteLine ("-width requires an numeric argument");
				}
				break;
			case "-height":
				try {
					i++;
					height = Int32.Parse (args [i]);
				} catch {
					Console.WriteLine ("-height requires an numeric argument");
				}
				break;
			case "-help":
			case "-h":
				Help ();
				break;
				
			default:
				if (url == null)
					url = args [i];
				else if (output == null)
					output = args [i];
				else
					Help ();
				break;
			}
		}

		Application.Init();
		Window w = new Window ("test");
		VBox vbox = new VBox ();
		Button bt = new Button ("Take a Shot");
		bt.Clicked += new EventHandler (btn_click);
		vbox.PackStart (bt);	

		wc = new WebControl ();
		wc.LoadUrl (url);
		//wc.NetStop += MakeShot;
		//wc.Show ();
		wc.SetSizeRequest (800, 600);
		vbox.PackStart (wc);
		w.Add (vbox);
		w.ShowAll ();
		Application.Run();
		
		System.Threading.Thread.Sleep (1500);
	}

	static void Help ()
	{
		Console.WriteLine ("Usage is: shot [-width N] [-height N] url
[shot]");
		Environment.Exit (0);
	}
	
	 static void btn_click (object obj, EventArgs args)
     {
          Console.WriteLine ("Button Clicked");
          MakeShot();
     }
     
	static void MakeShot()
	{		
		System.Console.Write("Taking a new shot");	
		
		Gdk.Window win = wc.GdkWindow;
		int iwidth = wc.Allocation.Width;
		int iheight = wc.Allocation.Height;
		Gdk.Pixbuf p = new Gdk.Pixbuf (Gdk.Colorspace.Rgb, false, 8, iwidth,
iheight);
		Gdk.Pixbuf scaled;
		
		p = p.GetFromDrawable (win, win.Colormap, 0, 0, 0, 0, iwidth,
iheight);
		if (width == -1){
			if (height == -1)
				scaled = p;
			else
				scaled = p.ScaleSimple (height * iwidth / iheight, height,
Gdk.InterpType.Hyper);
		} else {
			if (height == -1)
				scaled = p.ScaleSimple (width, width * iheight / iwidth,
Gdk.InterpType.Hyper);
			else
				scaled = p.ScaleSimple (width, height, Gdk.InterpType.Hyper);
		}
		
		scaled.Savev (output, "png", null, null);  
	}
	
}