Beispiel #1
0
        /// <summary>
        /// Converts the path to an BitmapImage.
        /// This is a workaround to open also images that are stored on a separate drive.
        /// </summary>
        /// <param name="imgModel">The actual image the <paramref name="path"/> corresponds to.</param>
        /// <param name="path">The absolute path to the image.</param>
        /// <returns>The BitmapImage representation of the current path object.</returns>
        private async Task <BitmapImage> GetBitmapImageAsync(ChatMessageImageModel imgModel, string path)
        {
            if (path is null)
            {
                return(null);
            }

            try
            {
                StorageFile file = await StorageFile.GetFileFromPathAsync(path);

                if (file is null)
                {
                    return(null);
                }

                BitmapImage img = null;
                // Bitmap stuff has to be done in the UI thread,
                // so make sure we execute it there:
                Exception ex = null;
                await SharedUtils.CallDispatcherAsync(async() =>
                {
                    try
                    {
                        img = new BitmapImage();
                        img.SetSource(await file.OpenReadAsync());
                    }
                    catch (Exception e)
                    {
                        ex = e;
                    }
                });

                // If loading the image failed log the exception:
                if (!(ex is null))
                {
                    Logger.Error("Failed to load image: " + path, ex);
                    MODEL.ErrorText = "Failed to load image. Try downloading it again.";
                    imgModel.state  = DownloadState.ERROR;
                    imgModel.Update();
                    return(null);
                }

                return(img);
            }
            catch (Exception e)
            {
                Logger.Error("Failed to load image: " + path, e);
                MODEL.ErrorText = "Failed to load image. Try downloading it again.";
                imgModel.state  = DownloadState.ERROR;
                imgModel.Update();
                return(null);
            }
        }