public static Gdk.Pixbuf GetPixbufFromNSImage(NSImage icon, int width, int height) { var rect = new CGRect(0, 0, width, height); var rep = icon.BestRepresentation(rect, null, null); var bitmap = rep as NSBitmapImageRep; try { if (bitmap == null) { if (rep != null) { rep.Dispose(); } using (var cgi = icon.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(); } } }
private static NSImage SCCopyWithResolution(NSImage image, float size) { var imageRep = image.BestRepresentation(new CGRect(0, 0, size, size), null, null); if (imageRep != null) { return(new NSImage(imageRep.CGImage, imageRep.Size)); } return(image); }
protected override Gdk.Pixbuf OnGetPixbufForFile(string filename, Gtk.IconSize size) { //this only works on MacOS 10.6.0 and greater if (systemVersion < 0x1060) { return(base.OnGetPixbufForFile(filename, size)); } NSImage icon = null; if (Path.IsPathRooted(filename) && File.Exists(filename)) { icon = NSWorkspace.SharedWorkspace.IconForFile(filename); } else { string extension = Path.GetExtension(filename); if (!string.IsNullOrEmpty(extension)) { icon = NSWorkspace.SharedWorkspace.IconForFileType(extension); } } if (icon == null) { return(base.OnGetPixbufForFile(filename, size)); } int w, h; if (!Gtk.Icon.SizeLookup(Gtk.IconSize.Menu, out w, out h)) { w = h = 22; } var rect = new System.Drawing.RectangleF(0, 0, w, h); using (var rep = icon.BestRepresentation(rect, null, null)) { if (rep == null) { return(base.OnGetPixbufForFile(filename, size)); } return(GetPixbufFromNSImageRep(rep, w, h)); } }
public static Gdk.Pixbuf GetPixbufFromNSImage (NSImage icon, int width, int height) { var rect = new CGRect (0, 0, width, height); var rep = icon.BestRepresentation (rect, null, null); var bitmap = rep as NSBitmapImageRep; try { if (bitmap == null) { if (rep != null) rep.Dispose (); using (var cgi = icon.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 (); } }
private static NSImage SCCopyWithResolution (NSImage image, float size) { var imageRep = image.BestRepresentation (new CGRect (0, 0, size, size), null, null); if (imageRep != null) { return new NSImage (imageRep.CGImage, imageRep.Size); } return image; }
protected override Gdk.Pixbuf OnGetPixbufForFile(string filename, Gtk.IconSize size) { NSImage icon = null; //FIXME: better handling of names of files that haven't been saved yet if (Path.IsPathRooted(filename)) { icon = NSWorkspace.SharedWorkspace.IconForFile(filename); } else { icon = NSWorkspace.SharedWorkspace.IconForFile("/tmp/" + filename); } if (icon != null) { int w, h; if (!Gtk.Icon.SizeLookup(Gtk.IconSize.Menu, out w, out h)) { w = h = 22; } var rect = new System.Drawing.RectangleF(0, 0, w, h); var rep = icon.BestRepresentation(rect, null, null) as NSBitmapImageRep; if (rep != null) { var tiff = rep.TiffRepresentation; byte[] arr = new byte[tiff.Length]; System.Runtime.InteropServices.Marshal.Copy(tiff.Bytes, arr, 0, arr.Length); int pw = rep.PixelsWide, ph = rep.PixelsHigh; var px = new Gdk.Pixbuf(arr, pw, ph); //if one dimension matches, and the other is same or smaller, use as-is if ((pw == w && ph <= h) || (ph == h && pw <= w)) { return(px); } //else scale proportionally such that the largest dimension matches the desired size if (pw == ph) { pw = w; ph = h; } else if (pw > ph) { ph = (int)(w * ((float)ph / pw)); pw = w; } else { pw = (int)(h * ((float)pw / ph)); ph = h; } var scaled = px.ScaleSimple(pw, ph, Gdk.InterpType.Bilinear); px.Dispose(); return(scaled); } } return(base.OnGetPixbufForFile(filename, size)); }
protected override Gdk.Pixbuf OnGetPixbufForFile(string filename, Gtk.IconSize size) { //this only works on MacOS 10.6.0 and greater if (systemVersion < 0x1060) { return(base.OnGetPixbufForFile(filename, size)); } NSImage icon = null; if (Path.IsPathRooted(filename) && File.Exists(filename)) { icon = NSWorkspace.SharedWorkspace.IconForFile(filename); } else { string extension = Path.GetExtension(filename); if (!string.IsNullOrEmpty(extension)) { icon = NSWorkspace.SharedWorkspace.IconForFileType(extension); } } if (icon == null) { return(base.OnGetPixbufForFile(filename, size)); } int w, h; if (!Gtk.Icon.SizeLookup(Gtk.IconSize.Menu, out w, out h)) { w = h = 22; } var rect = new System.Drawing.RectangleF(0, 0, w, h); var arep = icon.BestRepresentation(rect, null, null); if (arep == null) { return(base.OnGetPixbufForFile(filename, size)); } var rep = arep as NSBitmapImageRep; if (rep == null) { using (var cgi = arep.AsCGImage(rect, null, null)) rep = new NSBitmapImageRep(cgi); arep.Dispose(); } try { byte[] arr; using (var tiff = rep.TiffRepresentation) { arr = new byte[tiff.Length]; System.Runtime.InteropServices.Marshal.Copy(tiff.Bytes, arr, 0, arr.Length); } int pw = rep.PixelsWide, ph = rep.PixelsHigh; var px = new Gdk.Pixbuf(arr, pw, ph); //if one dimension matches, and the other is same or smaller, use as-is if ((pw == w && ph <= h) || (ph == h && pw <= w)) { return(px); } //else scale proportionally such that the largest dimension matches the desired size if (pw == ph) { pw = w; ph = h; } else if (pw > ph) { ph = (int)(w * ((float)ph / pw)); pw = w; } else { pw = (int)(h * ((float)pw / ph)); ph = h; } var scaled = px.ScaleSimple(pw, ph, Gdk.InterpType.Bilinear); px.Dispose(); return(scaled); } finally { if (rep != null) { rep.Dispose(); } } }