【问题标题】:MonoMac: Best way to convert Bitmap to NSImageMonoMac:将位图转换为 NSImage 的最佳方法
【发布时间】:2014-07-21 19:30:12
【问题描述】:

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

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

【问题讨论】:

    标签: bitmap nsimage monomac


    【解决方案1】:

    发件人: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());
            }
        }
    

    【讨论】:

      【解决方案2】:

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

      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);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-21
        • 2018-03-07
        • 1970-01-01
        相关资源
        最近更新 更多