I'm wondering that how to get thumbnails in UWP using C#
I want to get all thumbnail images of image files (gif, jpg etc) in my folder
I read quite codes about getting thumbnails and refered to this and the other samples
But i couldn't fully understand the process with Xaml
Can you please tell me how to get thumbnail from my library folder?
Once you have access to a folder through user selection with a FolderPicker.you can retrieve the thumbnails from the system. You can use the GetScaledImageAsThumbnailAsync() for that.
For instance:
private async Task<BitmapImage> GetThumbnail(StorageFile file)
{
if (file != null)
{
StorageItemThumbnail thumb = await file.GetScaledImageAsThumbnailAsync(ThumbnailMode.VideosView);
if (thumb != null)
{
BitmapImage img = new BitmapImage();
await img.SetSourceAsync(thumb);
return img;
}
}
return null;
}
Related
I have a List View with some ListViewItems and each one contains a TextBox and an Image.
The user can add or remove these ListViewItems, so I would like to save them in the settings each time the app is closed and load each time it is opened.
I managed to save the TextBoxes' texts in the settings and load them, but how to save and load Images / Image Sources / File Streams?
The images are created when the user chooses an image in a FileOpenPicker and then a Bitmap Image is created with a File Stream to that chosen Image, as the ListViewItem's image source.
private async void openFileInputEvent(object sender, RoutedEventArgs e)
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
BitmapImage tmpimg = new BitmapImage();
await tmpimg.SetSourceAsync(fileStream);
imglist.Add(tmpimg);
}
}
else
{
var dialog = new MessageDialog("File not chosen", "No chosen Image");
await dialog.ShowAsync();
imglist.Add(new BitmapImage());
}
}
The bitmap images for all the ListViewItems' images are stored in a list of bitmap images (List<BitmapImage>) and they are chosen by choosing the last item on the list (imglist[imglist.Count-1])
I would like to save and load this Images / their source / file stream to their source, is there any way to do that?
I could save paths to the images, but I can't later create an Image with the path as its source, it has to be a file stream.
How to save an Image or File Stream in application's settings? UWP C#
LocalSettings does not support store the big data, I'm afraid you can't store the BitmapImage into LocalSettings directly, for your scenario, we suggest you store your image in the app's LocalFolder and access them with uri scheme.
I implemented a File Open Picker into a Universal Windows Platform Application for choosing an image to display in a list item.
However, after getting the image path from the file open picker, the path can't be set as an image source neither as a URI nor network path (tried "file:///" + path).
Is there a way to show an image from a file open picker selected path?
If not, is there any way to open a local image from the computer in the app?
I implemented a File Open Picker into a Universal Windows Platform Application for choosing an image to display in a list item.
UWP does not support file:// uri scheme, if you open the file with file open picker, you could open the file as stream, and convert it to BitmapImage like the following.
try
{
var picker = new Windows.Storage.Pickers.FileOpenPicker
{
ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary
};
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
Windows.Storage.StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
BitmapImage bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(fileStream);
MyImage.Source = bitmapImage;
}
}
}
catch (Exception ex)
{
throw ex;
}
Now I am working on edit operation in my app. So in edit operation there is an ability to change image and in the same time name of the file that keep image of the contact can not be renamed(during one session of operation image files will be changing but all this file will take same name).
And I used overload of CopyAsync method (docs to this method here) that as I have understand must to replace file to the existing file with the same name.
I get imageFile variable by FileOpenPicker. Code below runs every time I have choosed image by FileOpenPicker. And when I re-choose image at the result Image UI control show me previous chosen image. I expect that Image will view last image that I have choosed but this does not occurred.
public BitmapImage Image { set; get; }
//Copy of the file that saved in temporary storage
StorageFile fileForView = await imageFile.CopyAsync
(ApplicationData.Current.TemporaryFolder,
fileName,
NameCollisionOption.ReplaceExisting);
Image = new BitmapImage(new Uri(fileForView.Path));
Maybe I understand logic of CopyAsync method not correctly if I am in this case please show me how make my plan working with this method if it is possible. Otherwise offer your solution cause I have no idea now how to do this.
Also I tried to do this with CopyAndReplaceAsync method. But still no result. I do it in this way:
if (null != await ApplicationData.Current.TemporaryFolder.TryGetItemAsync(fileName))
{
StorageFile storageFile = await ApplicationData.Current.TemporaryFolder.GetFileAsync(fileName);
await storageFile.CopyAndReplaceAsync(imageFile);
}
I expect that Image will view last image that I have choosed but this does not occurred.
The above CopyAsync method is correct, and I have tested with the following code, it works well.
private async void ListOption_ItemClick(object sender, ItemClickEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
StorageFile fileForView = await file.CopyAsync(ApplicationData.Current.TemporaryFolder, file.Name, NameCollisionOption.ReplaceExisting);
Image = new BitmapImage(new Uri(fileForView.Path));
TestImg.Source = Image;
}
else
{
}
}
I want to get a thumbnail image of all photo files in a specific folder.
(Example: My C: \ Mypic)
I found another way to get a single thumbnail image, but this isn't exactly what i want
async private Task<BitmapImage> Thumbnail_call()
{
var files = await KnownFolders.PicturesLibrary.GetFilesAsync();
var thumb = await files[0].GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView);
var bitm = new BitmapImage();
bitm.SetSource(thumb);
return bitm;
}
I think that i have to use foreach sentence
Can you give me a solution to this problem?
In UWP app, you can access certain file system locations by default. Apps can also access additional locations through the file or folder picker, or by declaring capabilities. See File access permissions for more details about accessing the folders or files.
After you get the specific folders, you can get all thumbnails in it as the following code.
async private Task<List<BitmapImage>> GetThumbnails(StorageFolder folder)
{
List<BitmapImage> BitmapImageList = new List<BitmapImage>();
var files = await folder.GetFilesAsync();
foreach (var file in files)
{
var thumb = await file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.PicturesView);
var bitmap = new BitmapImage();
bitmap.SetSource(thumb);
BitmapImageList.Add(bitmap);
}
return BitmapImageList;
}
Most solutions here use System.Drawing which is not available in UWP afaik. GetThumbnailAsync() does the job but only for non-image files. With image files i always get a scaled preview, regardless of passed arguments.
I'd like to have a shell file icon instead of a tiny preview to the left of the file name, like here:
Any thoughts?
Thanks
PS: I have found a hack: create a 0 byte temp file with the same extension and make a thumbnail of it. I hope though there is a better solution...
Well here's a helper metod which uses the dummy file approach (place it in some static class):
public async static Task<StorageItemThumbnail> GetFileIcon(this StorageFile file, uint size = 32)
{
StorageItemThumbnail iconTmb;
var imgExt = new[] { "bmp", "gif", "jpeg", "jpg", "png" }.FirstOrDefault(ext => file.Path.ToLower().EndsWith(ext));
if (imgExt != null)
{
var dummy = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("dummy." + imgExt, CreationCollisionOption.ReplaceExisting); //may overwrite existing
iconTmb = await dummy.GetThumbnailAsync(ThumbnailMode.SingleItem, size);
}
else
{
iconTmb = await file.GetThumbnailAsync(ThumbnailMode.SingleItem, size);
}
return iconTmb;
}
Usage example:
var icon = await file.GetFileIcon();
var img = new BitmapImage();
img.SetSource(icon);