C#,ソフトウェア開発

public static class ImageURLToBase64
{
    /// <summary>
    /// 画像URLからBase64文字列を取得
    /// </summary>
    /// <param name="strImgeURL"></param>
    /// <returns></returns>
    public static async Task<string> GetBase64(string strImgeURL)
    {
        string strResult = string.Empty;
        byte[] bs = null;

        using (WebClient objWebClient = new WebClient())
        {
            bs = await objWebClient.DownloadDataTaskAsync(strImgeURL);
        }

        if(bs != null)
        {
            strResult = Convert.ToBase64String(bs);

            string strType = GetImageTypeFromByte(bs);
            if (strType != string.Empty)
            {
                strResult = "data:image/" + strType + ";base64," + strResult;
            }
        }

        return strResult;
    }

    /// <summary>
    /// バイト配列を読んで画像種別を判定
    /// </summary>
    /// <param name="arrayBytes"></param>
    /// <returns></returns>
    private static string GetImageTypeFromByte(byte[] arrayBytes)
    {
        Dictionary<string, string>  ImageTypes = new Dictionary<string, string>();
        ImageTypes.Add("FFD8", "jpg");
        ImageTypes.Add("424D", "bmp");
        ImageTypes.Add("474946", "gif");
        ImageTypes.Add("89504E470D0A1A0A", "png");

        string strBytes = BitConverter.ToString(arrayBytes).Replace("-", string.Empty);

        KeyValuePair<string,string> SelectImageType = ImageTypes.Where(x => strBytes.ToLower().StartsWith(x.Key.ToLower())).First();

        return SelectImageType.Value.ToString().ToLower();
    }
}

C#,ソフトウェア開発

  • Freezeを行わないと、別スレッドから触った際にエラーとなる
  • DeleteObjectを行わないとメモリリークが発生する
public static class BitmapToImageSource
{
    [DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool DeleteObject([In] IntPtr hObject);

    /// <summary>
    /// BitmapからImageSourceへコンバート
    /// </summary>
    /// <param name="_Bitmap"></param>
    /// <returns></returns>
    public static ImageSource Convert(Bitmap _Bitmap)
    {
        ImageSource objResult = null;

        if (_Bitmap != null)
        {
            var objHandle = _Bitmap.GetHbitmap();

            try
            {
                objResult = Imaging.CreateBitmapSourceFromHBitmap(objHandle, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                objResult.Freeze();
            }
            finally
            {
                DeleteObject(objHandle);
            }
        }

        return objResult;
    }
}

C#,WPF,ソフトウェア開発

MainWindow.xaml

<Window x:Class="WpfApp8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp8"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        
        <ListView ItemsSource="{Binding Path=ArrayData}">
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    
                    <!-- ボーダーの設定 -->
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ContentControl}">
                                <Border Background="{TemplateBinding Background}">
                                    <ContentPresenter />
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    
                    <!-- 選択時の背景色 -->
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="Transparent" />
                        </Trigger>
                    </Style.Triggers>
                    
                </Style>
            </ListView.ItemContainerStyle>

            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

    </Grid>
</Window>

C#,WPF,ソフトウェア開発

MainWindow.xaml

<Window x:Class="WpfApp8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp8"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel Orientation="Horizontal">
        
        <!-- 縦並び -->
        <ListView ItemsSource="{Binding Path=ArrayData}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <!-- 横並び -->
        <ListView ItemsSource="{Binding Path=ArrayData}">
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

    </StackPanel>
</Window>

C#,ソフトウェア開発

アプリケーション終了時に、構造体の変数をシリアライズ化(文字列化)して設定ファイルに書き込み、
次回起動時にシリアライズ解除(オブジェクト化)することで、変数をまるごと維持することができる。

using System.Web.UI;

namespace CommonLibrary
{
    public static class ObjectSerialize
    {
        private static ObjectStateFormatter formatter = new ObjectStateFormatter();

        /// <summary>
        /// オブジェクトのシリアライズ
        /// </summary>
        /// <param name="objTarget"></param>
        /// <returns></returns>
        public static string Serialize(object objTarget)
        {
            return formatter.Serialize(objTarget);
        }

        /// <summary>
        /// オブジェクトのデシリアライズ
        /// </summary>
        /// <param name="strTarget"></param>
        /// <returns></returns>
        public static object Deserialize(string strTarget)
        {
            return (strTarget == string.Empty) ? null : formatter.Deserialize(strTarget);
        }
    }
}