來源:http://lists.ximian.com/pipermail/mono-osx/2011-July/004436.html
public static NSImage ToNSImage(this Image img) {
System.IO.MemoryStream s = new System.IO.MemoryStream();
img.Save(s, System.Drawing.Imaging.ImageFormat.Png);
byte[] b = s.ToArray();
CGDataProvider dp = new CGDataProvider(b,0,(int)s.Length);
s.Flush();
s.Close();
CGImage img2 = CGImage.FromPNG(dp,null,false,CGColorRenderingIntent.Default);
return new NSImage(img2, new SizeF(img2.Width,img2.Height));
}
編輯:
,並從一個NSImage中轉換爲圖像
public static Image ToImage(this NSImage img) {
using (var imageData = img.AsTiff()) {
var imgRep = NSBitmapImageRep.ImageRepFromData(imageData) as NSBitmapImageRep;
var imageProps = new NSDictionary();
var data = imgRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Png, imageProps);
return Image.FromStream(data.AsStream());
}
}