3

由于 MonoMac 没有自己的类位图,我需要将图像从位图转换为 NSImage 的最佳方法。目前我的方式是:

byte[] bytes = SomeFuncReturnsBytesofBitmap();
NSData imageData = NSData.FromArray(bytes);
NSImage image = new NSImage(imageData);
imageView.Image = image;
4

2 回答 2

4

来自: 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 转换为 Image

 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());
        }
    }
于 2012-11-13T04:49:42.663 回答
0

使用NSImage.FromStreamHerman 的解决方案可以简化。我认为发布问题时它不可用。

public static NSImage ToNSImage(this Image img) {
    using (var s = new System.IO.MemoryStream()) {
        img.Save(s, System.Drawing.Imaging.ImageFormat.Png);
        s.Seek(0, System.IO.SeekOrigin.Begin);
        return NSImage.FromStream(s);
    }
}
于 2014-07-21T11:30:12.877 回答