{"id":836,"date":"2021-03-14T18:52:43","date_gmt":"2021-03-14T09:52:43","guid":{"rendered":"https:\/\/todosoft.net\/blog\/?p=836"},"modified":"2021-03-14T23:55:09","modified_gmt":"2021-03-14T14:55:09","slug":"post-836","status":"publish","type":"post","link":"https:\/\/todosoft.net\/blog\/?p=836","title":{"rendered":"\u753b\u50cfURL\u304b\u3089Base64\u6587\u5b57\u5217\u3092\u4f5c\u6210"},"content":{"rendered":"\n<pre class=\"wp-block-luxe-blocks-syntaxhighlighter line-numbers language-csharp\"><code class=\"language-csharp\">public static class ImageURLToBase64\n{\n    \/\/\/ &lt;summary>\n    \/\/\/ \u753b\u50cfURL\u304b\u3089Base64\u6587\u5b57\u5217\u3092\u53d6\u5f97\n    \/\/\/ &lt;\/summary>\n    \/\/\/ &lt;param name=\"strImgeURL\">&lt;\/param>\n    \/\/\/ &lt;returns>&lt;\/returns>\n    public static async Task&lt;string> GetBase64(string strImgeURL)\n    {\n        string strResult = string.Empty;\n        byte[] bs = null;\n\n        using (WebClient objWebClient = new WebClient())\n        {\n            bs = await objWebClient.DownloadDataTaskAsync(strImgeURL);\n        }\n\n        if(bs != null)\n        {\n            strResult = Convert.ToBase64String(bs);\n\n            string strType = GetImageTypeFromByte(bs);\n            if (strType != string.Empty)\n            {\n                strResult = \"data:image\/\" + strType + \";base64,\" + strResult;\n            }\n        }\n\n        return strResult;\n    }\n\n    \/\/\/ &lt;summary>\n    \/\/\/ \u30d0\u30a4\u30c8\u914d\u5217\u3092\u8aad\u3093\u3067\u753b\u50cf\u7a2e\u5225\u3092\u5224\u5b9a\n    \/\/\/ &lt;\/summary>\n    \/\/\/ &lt;param name=\"arrayBytes\">&lt;\/param>\n    \/\/\/ &lt;returns>&lt;\/returns>\n    private static string GetImageTypeFromByte(byte[] arrayBytes)\n    {\n        Dictionary&lt;string, string>  ImageTypes = new Dictionary&lt;string, string>();\n        ImageTypes.Add(\"FFD8\", \"jpg\");\n        ImageTypes.Add(\"424D\", \"bmp\");\n        ImageTypes.Add(\"474946\", \"gif\");\n        ImageTypes.Add(\"89504E470D0A1A0A\", \"png\");\n\n        string strBytes = BitConverter.ToString(arrayBytes).Replace(\"-\", string.Empty);\n\n        KeyValuePair&lt;string,string> SelectImageType = ImageTypes.Where(x => strBytes.ToLower().StartsWith(x.Key.ToLower())).First();\n\n        return SelectImageType.Value.ToString().ToLower();\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,15],"tags":[102,101],"class_list":["post-836","post","type-post","status-publish","format-standard","hentry","category-c","category-15","tag-base64","tag-image"],"_links":{"self":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/836","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=836"}],"version-history":[{"count":3,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/836\/revisions"}],"predecessor-version":[{"id":839,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/836\/revisions\/839"}],"wp:attachment":[{"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=836"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=836"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/todosoft.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=836"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}