Exemplo n.º 1
0
		public static Gdk.Pixbuf GetPixbufFromNSImageRep (NSImageRep rep, int width, int height)
		{
			var rect = new RectangleF (0, 0, width, height);
			var bitmap = rep as NSBitmapImageRep;
			
			if (bitmap == null) {
				using (var cgi = rep.AsCGImage (ref rect, null, null))
					bitmap = new NSBitmapImageRep (cgi);
			}
			
			try {
				byte[] data;
				using (var tiff = bitmap.TiffRepresentation) {
					data = new byte[tiff.Length];
					System.Runtime.InteropServices.Marshal.Copy (tiff.Bytes, data, 0, data.Length);
				}
				
				int pw = bitmap.PixelsWide, ph = bitmap.PixelsHigh;
				var pixbuf = new Gdk.Pixbuf (data, pw, ph);
				
				// if one dimension matches, and the other is same or smaller, use as-is
				if ((pw == width && ph <= height) || (ph == height && pw <= width))
					return pixbuf;
				
				// otherwise scale proportionally such that the largest dimension matches the desired size
				if (pw == ph) {
					pw = width;
					ph = height;
				} else if (pw > ph) {
					ph = (int) (width * ((float) ph / pw));
					pw = width;
				} else {
					pw = (int) (height * ((float) pw / ph));
					ph = height;
				}
				
				var scaled = pixbuf.ScaleSimple (pw, ph, Gdk.InterpType.Bilinear);
				pixbuf.Dispose ();
				
				return scaled;
			} finally {
				if (bitmap != rep)
					bitmap.Dispose ();
			}
		}
Exemplo n.º 2
0
		public static Gdk.Pixbuf GetPixbufFromNSImageRep (NSImageRep rep, int width, int height)
		{
			var rect = new RectangleF (0, 0, width, height);

			var bitmap = rep as NSBitmapImageRep;
			try {
				if (bitmap == null) {
					using (var cgi = rep.AsCGImage (ref rect, null, null)) {
						if (cgi == null)
							return null;
						bitmap = new NSBitmapImageRep (cgi);
					}
				}
				return GetPixbufFromNSBitmapImageRep (bitmap, width, height);
			} finally {
				if (bitmap != null)
					bitmap.Dispose ();
			}
		}