My application for Windows Phone 8.1.
I need to find a way to check image aviability in assets resources in my application.
At first, I had following solution:
var package = Windows.ApplicationModel.Package.Current.InstalledLocation;
var folder = await package.GetFolderAsync("Assets\\Makes");
var files = await folder.GetFilesAsync();
var result = files.FirstOrDefault(p => p.Name == imageName);
if (result != null)
{
Uri imageUri = new Uri("ms-appx:///Assets/Makes/" + imageName);
Image img = new Image();
BitmapImage bi = new BitmapImage(imageUri);
img.Source = bi;
btn.Content = img;
}
else
{
TextBlock label = new TextBlock();
label.Text = text;
btn.Content = label;
}
It works. But, unfortunately, very very slow.
Anyway, next part of code working even in case if asset is not existing:
Uri imageUri = new Uri("ms-appx:///Assets/Makes/" + imageName);
BitmapImage bi = new BitmapImage(imageUri);
In case if file not existing, the image is empty, but not null.
Is there are any good way, to check, if image created empty from resource?
Or a really fast way to check existing of packaged resource file?
Thank you
Related
In my base directory I have a folder for image. In this folder I have only one image and I want to get image name from it and load it to Image Source. In this code I get an error:
Could not find file'C:\Users\Santhosh\Documents\Visual Studio
2012\imagetest\imagetest\bin\Debug\ImageFolder\System.Linq.Enumerable+WhereSelectArrayIterator`2[System.String,System.String].jpg'
How can I get an image name from base directory?
This is my code
string ImageFiles=Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory+"\\ImageFolder\\",".jpg").Select(System.IO.Path.GetFileName).ToString();
image1.source== new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\ImageFolder\\"+ImageFiles+".jpg"));
Call FirstOrDefault to get the first file from the directory, or null if the directory does not contain a file with a matching name:
var imageFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ImageFolder");
var firstFile = Directory.EnumerateFiles(imageFolder, "*.jpg")
.Select(Path.GetFileName)
.FirstOrDefault();
if (firstFile != null)
{
image.Source = new BitmapImage(new Uri(Path.Combine(imageFolder, firstFile)));
}
Or shorter, without GetFileName and a subsequent Combine:
var imageFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ImageFolder");
var firstFile = Directory.EnumerateFiles(imageFolder, "*.jpg")
.FirstOrDefault();
if (firstFile != null)
{
image.Source = new BitmapImage(new Uri(firstFile));
}
This is how youre first line should look like :
string ImageFiles = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "\\ImageFolder\\").Select(System.IO.Path.GetFileName).First();
In my Windows Phone 8.1 App i take a photo with the camera and save this in the camera roll and save the image path in a temporary object:
var picture = library.SavePictureToCameraRoll(fileName, e.ImageStream);
geophoto.ImagePath = picture.GetPath();
In another page of my app i want to load this photo from the camera roll and set the saved path as the source of an Image object:
Uri uri = new Uri(App.Current.Geophoto.ImagePath, UriKind.Absolute);
ImageSource imgSource = new BitmapImage(uri);
this.ShutterImage.Source = imgSource;
The saved path of the image is e.g. "file:///C:/Data/Users/Public/Pictures/Camera Roll/201506191442443805.jpg"
In runtime the image goes blank when i try to set a new source. Is there something wrong with the path or with the code?
I figured out that i have no direct access to camera roll through the path. So i solved my problem with the following code:
private BitmapImage GetThumbnailFromCameraRoll(string path)
{
MediaLibrary mediaLibrary = new MediaLibrary();
var pictures = mediaLibrary.Pictures;
foreach (var picture in pictures)
{
var camerarollPath = picture.GetPath();
if (camerarollPath == path)
{
BitmapImage image = new BitmapImage();
image.SetSource(picture.GetThumbnail());
return image;
}
}
return null;
}
I'm trying to set image for my tile in the background agent for my application:
ShellTile t = ShellTile.ActiveTiles.First();
if (t != null)
{
var filePath = Path.Combine("Tiles", "test1.jpg");
StandardTileData tile = new StandardTileData();
tile.Title = "Title text here";
tile.BackgroundImage = new Uri(#"isostore:\" + filePath, UriKind.Absolute);
t.Update(tile);
}
but then on t.Update(tile) it throws NotSupportedException :-( Isnt the path ("isostore:\") correct?
new Uri(#"isostore:" + filePath, UriKind.Absolute);
Without the backslash.
I Working on Windows Phone 8 application.
string Image = "/MyData/" + myObject.ImageName + "big.png";
BitmapImage bmp = new BitmapImage(new Uri(Image , UriKind.Relative));
MyImage.Source = bmp;
I have a image in the folder MyData/, i have 2 sets of images like <imagename>big.png,<imagename>small.png.
So here what is happening is i want to check if <imagename>big.png exists in the location or not, if not pick <imagename>small.png.
How to do it ?
EDIT
I solved it myself, here is how.
File.Exists("path to file") here path should be `folderName/filenames` and not `/folderName/filenames`
Thanks for everyone who helped me.
string image = "/MyData/" + myObject.ImageName + "/big.png";
string fileName = Path.GetFileName(image);
if(!string.IsNullOrEmpty(fileName))
{
MessageBox.Show("File Exist -"+fileName);
}
else
{
MessageBox.Show("No File Exist -");
}
BitmapImage bmp = new BitmapImage(new Uri(image , UriKind.Relative));
if(bmp==null)
{
image = "/MyData/" + myObject.ImageName + "/small.png";
bmp = new BitmapImage(new Uri(image , UriKind.Relative));
}
MyImage.Source = bmp;
This is my code for Windows 8 metro apps, in which I copy 1 image from the local folder to my app storage folder and then it shows a tile notification. Please help me to auto copy all images from Picture Library and then these images shown in tile notifications.
i don't know how to access or copy all images from Picture Library... no user interface for copy images.
public sealed partial class BlankPage : Page
{
string imageRelativePath = String.Empty;
public BlankPage()
{
this.InitializeComponent();
CopyImages();
}
public async void CopyImages()
{
FileOpenPicker picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = PickerViewMode.Thumbnail;
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
picker.FileTypeFilter.Add(".png");
picker.CommitButtonText = "Copy";
StorageFile file = await picker.PickSingleFileAsync();
StorageFile newFile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(file.Name);
await file.CopyAndReplaceAsync(newFile);
this.imageRelativePath = newFile.Path.Substring(newFile.Path.LastIndexOf("\\") + 1);
IWideTileNotificationContent tileContent = null;
ITileWideImage wideContent = TileContentFactory.CreateTileWideImage();
wideContent.RequireSquareContent = false;
wideContent.Image.Src = "ms-appdata:///local/" + this.imageRelativePath;
wideContent.Image.Alt = "App data";
tileContent = wideContent;
tileContent.RequireSquareContent = false;
TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());
}
}
1st give the path of images folder and then make a list of these images through IReadOnlyList, and set loop on copy images to end, after that just set timer on TileUpdateManager. and it will work.
to enumerate files in PicturesLibrary:
// from my sample app "MetroContractSample" http://metrocontractsample.codeplex.com/documentation
var queryOptions = new QueryOptions(CommonFileQuery.DefaultQuery, new[] { ".jpg", ".png", ".bmp", ".gif", }) { FolderDepth = FolderDepth.Deep, };
StorageFileQueryResult query = KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(queryOptions);
var fileInfoFactory = new FileInformationFactory(query, ThumbnailMode.SingleItem);
IReadOnlyList<FileInformation> fileInfoList = await fileInfoFactory.GetFilesAsync();
NOTE: You have to declare the Capability for PicturesLibrary in Package.appxmanifest.