[longomatch/statebar: 6/13] Add utility to open file images



commit 2dfba78f09d43ee34b3a4bc49ea543c8df055b43
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date:   Fri Sep 16 17:27:58 2011 +0200

    Add utility to open file images

 LongoMatch/Common/Images.cs |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)
---
diff --git a/LongoMatch/Common/Images.cs b/LongoMatch/Common/Images.cs
index 8b71a6e..68408de 100644
--- a/LongoMatch/Common/Images.cs
+++ b/LongoMatch/Common/Images.cs
@@ -16,7 +16,10 @@
 //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 // 
 using System;
+using System.IO;
+using Gtk;
 using Gdk;
+using Mono.Unix;
 
 namespace LongoMatch.Common
 {
@@ -49,6 +52,37 @@ namespace LongoMatch.Common
 		public static byte[] Serialize(Pixbuf pixbuf) {
 			return pixbuf.SaveToBuffer("png");
 		}
+		
+		public static FileFilter GetFileFilter() {
+			FileFilter filter = new FileFilter();
+			filter.Name = "Images";
+			filter.AddPattern("*.png");
+			filter.AddPattern("*.jpg");
+			filter.AddPattern("*.jpeg");
+			return filter;
+		}
+
+		public static Pixbuf OpenImage(Gtk.Window toplevel) {
+			Pixbuf pimage = null;
+			StreamReader file;
+			FileChooserDialog fChooser;
+			
+			fChooser = new FileChooserDialog(Catalog.GetString("Choose an image"),
+			                                 toplevel, FileChooserAction.Open,
+			                                 "gtk-cancel",ResponseType.Cancel,
+			                                 "gtk-open",ResponseType.Accept);
+			fChooser.AddFilter(GetFileFilter());
+			if(fChooser.Run() == (int)ResponseType.Accept)	{
+				// For Win32 compatibility we need to open the image file
+				// using a StreamReader. Gdk.Pixbuf(string filePath) uses GLib to open the
+				// input file and doesn't support Win32 files path encoding
+				file = new StreamReader(fChooser.Filename);
+				pimage= new Gdk.Pixbuf(file.BaseStream);
+				file.Close();
+			}
+			fChooser.Destroy();
+			return pimage;
+		}
 	}
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]