{"id":482,"date":"2016-07-17T11:40:23","date_gmt":"2016-07-17T02:40:23","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2016\/07\/17\/wpf_bitmapbitmapsource\/"},"modified":"2016-07-17T11:40:23","modified_gmt":"2016-07-17T02:40:23","slug":"wpf_bitmapbitmapsource","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2016\/07\/17\/wpf_bitmapbitmapsource\/","title":{"rendered":"WPF \u30a4\u30e1\u30fc\u30b8\u30ea\u30bd\u30fc\u30b9\u3092\u753b\u9762\u8868\u793a\u3059\u308b\u305f\u3081\u306b\u3001Bitmap\u3092BitmapSource\u306b\u5909\u63db\u3059\u308b"},"content":{"rendered":"<p>[<a href=\"http:\/\/typea.info\/tips\/wiki.cgi?page=WPF+Tips\"><u>WPF Tips<\/u><\/a>]<\/p>\n<p><a href=\"http:\/\/typea.info\/products\/shikalog.html\">WPF \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u4f5c\u6210\u3067\u5f97\u305fTips\u3092\u30e1\u30e2<\/a>\u3002<\/p>\n<p> <a href=\"http:\/\/www.amazon.co.jp\/gp\/product\/4798114200\/ref=as_li_ss_il?ie=UTF8&amp;camp=247&amp;creative=7399&amp;creativeASIN=4798114200&amp;linkCode=as2&amp;tag=typea09-22\"><img decoding=\"async\" border=\"0\" src=\"http:\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4798114200&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\" \/><\/a><img loading=\"lazy\" decoding=\"async\" style=\"border-top-style: none !important; border-left-style: none !important; border-bottom-style: none !important; border-right-style: none !important; margin: 0px\" border=\"0\" alt=\"\" src=\"http:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=as2&amp;o=9&amp;a=4798114200\" width=\"1\" height=\"1\" \/>   <\/p>\n<h2>1.\u72b6\u6cc1<\/h2>\n<p>\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30d7\u30ed\u30d1\u30c6\u30a3\u306e\u30ea\u30bd\u30fc\u30b9(Resource.resx)\u306b\u753b\u50cf\u3092\u767b\u9332\u3057\u3066\u304a\u3044\u3066\u3001<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/WPF-BitmapBitmapSource_12C40\/bitmap_converter02.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"bitmap_converter02\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"bitmap_converter02\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/WPF-BitmapBitmapSource_12C40\/bitmap_converter02_thumb.jpg\" width=\"244\" height=\"108\" \/><\/a><\/p>\n<p>\u767b\u9332\u3057\u305f\u753b\u50cf\u306e\u30d3\u30eb\u30c9\u30a2\u30af\u30b7\u30e7\u30f3\u30d7\u30ed\u30d1\u30c6\u30a3\u3092Resource\u306b\u3057\u3066\u304a\u304f<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/WPF-BitmapBitmapSource_12C40\/bitmap_converter03.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"bitmap_converter03\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"bitmap_converter03\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/WPF-BitmapBitmapSource_12C40\/bitmap_converter03_thumb.jpg\" width=\"148\" height=\"244\" \/><\/a><\/p>\n<p>\u3068\u3001XAML\u3067\u753b\u50cf\u3092\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u6307\u5b9a\u3067\u304d\u308b\u3002<\/p>\n<pre class=\"prettyprint linenums\">&lt;Button Command=&quot;{Binding OnShowSearchDialog}&quot; CommandParameter=&quot;{Binding SelectedSearchCondtion}&quot; Width=&quot;26&quot; ToolTip=&quot;\u691c\u7d22\u6761\u4ef6\u3092\u6307\u5b9a\u3057\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u307e\u3059&quot;&gt;\r\n    &lt;Image Source=&quot;Resources\/ic_cloud_download_black_36dp.png&quot; \/&gt;\r\n&lt;\/Button&gt;<\/pre>\n<p>&#160;<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/WPF-BitmapBitmapSource_12C40\/bitmap_converter04.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"bitmap_converter04\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"bitmap_converter04\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/WPF-BitmapBitmapSource_12C40\/bitmap_converter04_thumb.jpg\" width=\"244\" height=\"119\" \/><\/a><\/p>\n<p>&#160;<\/p>\n<p>\u305f\u3060\u3001\u3053\u306e\u30a2\u30a4\u30b3\u30f3\u3092\u52d5\u7684\u306b\u5909\u66f4\u4f7f\u7528\u3068\u3059\u308b\u3068\u4e00\u624b\u9593\u5fc5\u8981\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/WPF-BitmapBitmapSource_12C40\/bitmap_converter01.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"bitmap_converter01\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"bitmap_converter01\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/WPF-BitmapBitmapSource_12C40\/bitmap_converter01_thumb.jpg\" width=\"244\" height=\"97\" \/><\/a><\/p>\n<p>\u4e0a\u8a18\u30ea\u30bd\u30fc\u30b9\u306b\u767b\u9332\u3059\u308b\u3068\u3001\u30b3\u30fc\u30c9\u4e0a\u304b\u3089\u3001\u30ea\u30c6\u30e9\u30eb\u3067\u306f\u306a\u304f\u3001Properties.Resource.{\u30ea\u30bd\u30fc\u30b9\u540d} \u3067\u53c2\u7167\u3067\u304d\u88dc\u5b8c\u3082\u52b9\u304f\u3057\u3001\u5909\u66f4\u3059\u308b\u3068\u30b3\u30f3\u30d1\u30a4\u30eb\u30a8\u30e9\u30fc\u3068\u306a\u308b\u306e\u3067\u975e\u5e38\u306b\u4fbf\u5229\u306a\u306e\u3067\u3001ViewModel\u306b\u3066\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u516c\u958b\u3057\u3001\u30d0\u30a4\u30f3\u30c9\u3057\u305f\u3044\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3001System.Drawing.Bitmap \u578b\u3068\u306a\u308b\u3002<\/p>\n<p>\u3053\u306eViewModel\u306e\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u30d0\u30a4\u30f3\u30c9\u3057\u3066\u3082\u3046\u307e\u304f\u3044\u304b\u306a\u3044\u3002<\/p>\n<pre class=\"prettyprint linenums\">using System.Drawing;\r\nprivate Bitmap _IconImageSource;\r\npublic Bitmap IconImageSource\r\n{\r\n    get\r\n    {\r\n        return _IconImageSource;\r\n    }\r\n    set\r\n    {\r\n        _IconImageSource = value;\r\n        RaisePropertyChanged();\r\n    }\r\n}\r\n\r\n:\r\n\r\nIconImageSource = Properties.Resources.ic_help_outline_black_18dp;<\/pre>\n<h2>2.\u5bfe\u5fdc<\/h2>\n<p>Bitmap\u3092BitmapSource\u306b\u5909\u63db\u3059\u308b\u30b3\u30f3\u30d0\u30fc\u30bf\u30fc\u3092\u4f5c\u6210\u3057\u3001\u305d\u308c\u3092\u5229\u7528\u3059\u308b\u3002<\/p>\n<h2>3.\u53c2\u8003<\/h2>\n<ul>\n<li><a href=\"http:\/\/posaune.hatenablog.com\/entry\/2013\/02\/09\/011905\">System.Drawing.Bitmap\u3092System.Windows.Media.BitmapSource\u306bWin32\u7121\u3057\u3067\u5909\u63db\u3059\u308b<\/a> <\/li>\n<\/ul>\n<h2>4.\u30b5\u30f3\u30d7\u30eb<\/h2>\n<h3>4.1 \u30b3\u30f3\u30d0\u30fc\u30bf\u30fc<\/h3>\n<pre class=\"prettyprint linenums\">public class WinFormBitmapConverter : IValueConverter\r\n{\r\n    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)\r\n    {\r\n        var bitmap = value as Bitmap;\r\n\r\n        if (bitmap == null)\r\n        {\r\n            return null;\r\n        }\r\n\r\n        try\r\n        {\r\n            using (var memoryStream = new MemoryStream())\r\n            {\r\n                \/\/ You need to specify the image format to fill the stream. \r\n                \/\/ I'm assuming it is PNG\r\n                bitmap.Save(memoryStream, ImageFormat.Png);\r\n                memoryStream.Seek(0, SeekOrigin.Begin);\r\n\r\n\r\n                return CreateBitmapSourceFromBitmap(memoryStream);\r\n            }\r\n        }\r\n        catch (Exception)\r\n        {\r\n            return null;\r\n        }\r\n    }\r\n\r\n    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)\r\n    {\r\n        throw new NotImplementedException();\r\n    }\r\n\r\n    private static BitmapSource CreateBitmapSourceFromBitmap(Stream stream)\r\n    {\r\n        var bitmapDecoder = BitmapDecoder.Create(\r\n            stream,\r\n            BitmapCreateOptions.PreservePixelFormat,\r\n            BitmapCacheOption.OnLoad);\r\n\r\n        \/\/ This will disconnect the stream from the image completely...\r\n        var writable = new WriteableBitmap(bitmapDecoder.Frames.Single());\r\n        writable.Freeze();\r\n\r\n        return writable;\r\n    }\r\n}<\/pre>\n<h3>4.2 Xaml\u3067\u306e\u5229\u7528<\/h3>\n<pre class=\"prettyprint linenums\">&lt;Window x:Class=&quot;Shikalog.View.ProgressDialog&quot;\r\n        xmlns=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation&quot;\r\n        xmlns:x=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml&quot;\r\n        xmlns:d=&quot;http:\/\/schemas.microsoft.com\/expression\/blend\/2008&quot;\r\n        xmlns:mc=&quot;http:\/\/schemas.openxmlformats.org\/markup-compatibility\/2006&quot;\r\n        xmlns:local=&quot;clr-namespace:Shikalog.View&quot;\r\n        xmlns:converter=&quot;clr-namespace:Shikalog.Common.Converter&quot;\r\n        mc:Ignorable=&quot;d&quot;&gt;\r\n    &lt;Window.Resources&gt;\r\n        &lt;converter:WinFormBitmapConverter x:Key=&quot;BitmapConverter&quot;\/&gt;\r\n    &lt;\/Window.Resources&gt;\r\n    &lt;Grid&gt;\r\n        &lt;Grid.ColumnDefinitions&gt;\r\n            &lt;ColumnDefinition\/&gt;\r\n            &lt;ColumnDefinition\/&gt;\r\n        &lt;\/Grid.ColumnDefinitions&gt;\r\n        &lt;Grid.RowDefinitions&gt;\r\n            &lt;RowDefinition \/&gt;\r\n        &lt;\/Grid.RowDefinitions&gt;\r\n        &lt;Image Grid.Row=&quot;0&quot; Grid.Column=&quot;0&quot; Source=&quot;{Binding IconImageSource, Converter={StaticResource BitmapConverter}, Mode=OneWay}&quot; Width=&quot;24&quot;\/&gt;\r\n        &lt;TextBlock Grid.Row=&quot;0&quot; Grid.Column=&quot;1&quot; Text=&quot;{Binding Message}&quot; TextWrapping=&quot;Wrap&quot; \/&gt;\r\n    &lt;\/Grid&gt;\r\n&lt;\/Window&gt;<\/pre>\n<p>\u4ee5\u4e0a<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[WPF Tips] WPF \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u4f5c\u6210\u3067\u5f97\u305fTips\u3092\u30e1\u30e2\u3002 1.\u72b6\u6cc1 \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u30d7\u30ed\u30d1\u30c6\u30a3\u306e\u30ea\u30bd\u30fc\u30b9(Resource.resx)\u306b\u753b\u50cf\u3092\u767b\u9332\u3057\u3066\u304a\u3044\u3066\u3001 \u767b\u9332\u3057\u305f\u753b\u50cf\u306e\u30d3\u30eb\u30c9\u30a2\u30af\u30b7\u30e7\u30f3\u30d7\u30ed\u30d1\u30c6\u30a3\u3092Res [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[93],"tags":[],"class_list":["post-482","post","type-post","status-publish","format-standard","hentry","category-wpf"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=482"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/482\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}