「Xamarin.Mac/System.Drawing.ImageをNSImageに変換する」の版間の差分
| (2人の利用者による、間の6版が非表示) | |||
| 1行目: | 1行目: | ||
Xamarin.MacにおいてSystem.Drawing.ImageをAppKit.NSImageに変換する方法を試行錯誤している。 | |||
== 方法1 == | |||
NSImageは[[ファイル]]からのインスタンス生成はできるようだが、byte[]などのメモリ上の[[データ]]からのインスタンス生成はできない。 | |||
そこでCoreGraphicsのCGImageを経由して変換を行う。大雑把には以下のような手順をとった。 | |||
# ImageをMemoryStreamに保存 | |||
# MemoryStreamからCGImageを生成 | |||
# CGImageをNSImageに変換 | |||
<source lang="csharp"> | <source lang="csharp"> | ||
using System; | using System; | ||
| 9行目: | 13行目: | ||
using System.Drawing.Imaging; | using System.Drawing.Imaging; | ||
using System.IO; | using System.IO; | ||
using | using Foundation; | ||
using | using AppKit; | ||
using | using CoreGraphics; | ||
public static class | public static class NSImageExtensions | ||
{ | { | ||
public static NSImage ToNSImage(this Image image) | public static NSImage ToNSImage(this Image image) | ||
| 34行目: | 38行目: | ||
using (var cg = CGImage.FromPNG(dp, null, false, CGColorRenderingIntent.Default)) | using (var cg = CGImage.FromPNG(dp, null, false, CGColorRenderingIntent.Default)) | ||
{ | { | ||
nsImage = NSImage(cg, new SizeF(cg.Width, cg.Height)); | nsImage = new NSImage(cg, new SizeF(cg.Width, cg.Height)); | ||
} | } | ||
} | } | ||
| 43行目: | 47行目: | ||
</source> | </source> | ||
System.Drawing.ImageからのNSImageへの変換はともかく、byte[]からNSImageインスタンスの生成は、[[プログラム]]中で[[ウェブ]]などから[[ファイル]] | System.Drawing.ImageからのNSImageへの変換はともかく、byte[]からNSImageインスタンスの生成は、[[プログラム]]中で[[ウェブ]]などから[[ファイル]]を落としてきて表示するようなアプリの場合にはほぼ登場する手法になると思われる。 | ||
==関連項目== | == 関連項目 == | ||
*[[ | * [[Xamarin.Mac]] | ||
*[[ | * [[Xamarin.Mac/NSImageをSystem.Drawing.Imageに変換する]] | ||
*[[ | * [[Xamarin.Mac/NSImageをファイルに保存する]] | ||
* [[Xamarin.Mac/CGImageをNSImageに変換する]] | |||
==参考文献== | ==参考文献== | ||
| 54行目: | 59行目: | ||
{{stub}} | {{stub}} | ||
[[category: Xamarin.Mac]] | |||
[[category: MonoMac]] | |||