예제 #1
0
파일: ImageUtil.cs 프로젝트: alexfordc/Au
        /// <summary>
        /// Loads image and returns its data in .bmp file format.
        /// Returns null if fails, for example file not found or invalid Base64 string.
        /// </summary>
        /// <param name="s">Depends on t. File path or resource name without prefix or Base64 image data without prefix.</param>
        /// <param name="t">Image type and string format.</param>
        /// <param name="searchPath">Use <see cref="AFile.SearchPath"/></param>
        /// <remarks>Supports environment variables etc. If not full path, searches in <see cref="AFolders.ThisAppImages"/>.</remarks>
        public static byte[] BmpFileDataFromString(string s, ImageType t, bool searchPath = false)
        {
            //AOutput.Write(t, s);
            try {
                switch (t)
                {
                case ImageType.Bmp:
                case ImageType.PngGifJpg:
                case ImageType.Cur:
                    if (searchPath)
                    {
                        s = AFile.SearchPath(s, AFolders.ThisAppImages);
                        if (s == null)
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        if (!APath.IsFullPathExpandEnvVar(ref s))
                        {
                            return(null);
                        }
                        s = APath.Normalize(s, AFolders.ThisAppImages);
                        if (!AFile.ExistsAsFile(s))
                        {
                            return(null);
                        }
                    }
                    break;
                }

                switch (t)
                {
                case ImageType.Base64CompressedBmp:
                    return(AConvert.Decompress(Convert.FromBase64String(s)));

                case ImageType.Base64PngGifJpg:
                    using (var stream = new MemoryStream(Convert.FromBase64String(s), false)) {
                        return(_ImageToBytes(Image.FromStream(stream)));
                    }

                case ImageType.Resource:
                    return(_ImageToBytes(AResources.GetAppResource(s) as Image));

                case ImageType.Bmp:
                    return(File.ReadAllBytes(s));

                case ImageType.PngGifJpg:
                    return(_ImageToBytes(Image.FromFile(s)));

                case ImageType.Ico:
                case ImageType.IconLib:
                case ImageType.ShellIcon:
                case ImageType.Cur:
                    return(_IconToBytes(s, t == ImageType.Cur, searchPath));
                }
            }
            catch (Exception ex) { ADebug.Print(ex.Message + "    " + s); }
            return(null);
        }