Saved image does not appear in photos app - c#

I am downloading and saving an image from internet, image gets saved into Saved Pictures folder.
Heres part of the code:
var imagefile = await KnownFolders.SavedPictures.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
var filestream = await imagefile.OpenAsync(FileAccessMode.ReadWrite);
var writer = new DataWriter(filestream.GetOutputStreamAt(0));
writer.WriteBytes(await response.Content.ReadAsByteArrayAsync());
I can see images there if I navigate to the folder via pc, they can be opened and seem to work just fine.
What am I missing?

Related

How to render media compostion to existing file

I want to edit a video file with UWP media composition and then save it back to the existing file. However, when I select the same file the rendering doesn't work because the data stream get corrupt.
As a workaround I render the video to a temporary file and move it then to the existing location:
// Create temp file
var tempFolder = ApplicationData.Current.TemporaryFolder;
StorageFile tempFile = await tempFolder.CreateFileAsync(Guid.NewGuid().ToString(), CreationCollisionOption.ReplaceExisting);
// Render video to temp file so stream get not corrupted
mediaComposition.RenderToFileAsync(tempFile, MediaTrimmingPreference.Precise);
// If rendering completed move temp file to existing video
await tempFile.MoveAndReplaceAsync(existingFile);
My workaround works fine except that the temporary folder is on my local disk and when I edit large video files on my external disk I can run out of storage on my local disk.
Is there another possibility to override an existing file with RenderToFileAsync()?
Update:
As suggested in the comments I set the file size to zero with the following code but then I get a FileNotFoundException:
var stream = await file.OpenAsync(FileAccessMode.ReadWrite);
stream.Size = 0;
stream.Dispose();

C# saves a captured photo

I am trying to save an captured photo into a folder as following:
CameraCaptureUI captureUI = new CameraCaptureUI();
captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200);
StorageFile photo = await
captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
if (photo == null)
{
// User cancelled photo capture
return;
}
StorageFolder destinationFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("ProfilePhotoFolder", CreationCollisionOption.OpenIfExists);
await photo.CopyAsync(destinationFolder, "ProfilePhoto.jpg", NameCollisionOption.ReplaceExisting);
await photo.DeleteAsync();
However, I cannot find the ProfilePhotoFolder in my file system. Could any one please tell me where is this folder. This is an Universal Windows project.
When you write to UWP LocalFolders the data is placed into Isolated Storage which is only accessible by your application, and thus you won't simply be able to fire up Windows Explorer and start digging into the files.
The recommended way for Windows 10 devices is to set your device to Developer Mode then use the web browser interface they've created (see https://blogs.windows.com/buildingapps/2016/06/08/using-the-app-file-explorer-to-see-your-app-data/#6O50PWljxSKfKCAm.97).
Try:
StorageFolder storageFolder = KnownFolders.PicturesLibrary;
Docs

Download PDF and Word file in UWP

Currently i'm working on UWP apps, In that i need to download the pdf and word url to the system downloads folder, for that need i'm trying backgrounddownloader class as shown in below code.
Uri source = new Uri(selectedfile.DocumentPath);
StorageFile destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync(
selectedfile.DocumentName, CreationCollisionOption.GenerateUniqueName);
BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(source, destinationFile);
An also i'm trying another way to download the urls here is the code
var request = WebRequest.CreateHttp(selectedfile.DocumentPath);
var sampleFile = await KnownFolders.PicturesLibrary.CreateFileAsync(selectedfile.DocumentName, CreationCollisionOption.GenerateUniqueName);
var cli = new HttpClient();
var str = await cli.GetStreamAsync(request.RequestUri);
var dst = await sampleFile.OpenStreamForWriteAsync();
await str.AsInputStream().AsStreamForRead().CopyToAsync(dst);
For using above two methods url can be downloaded as pdf and word but i'm trying to open the pdf file and word file but it couldn't open, it showing error like as shown in below figure trying to open the open downloaded file it shown like this. Can any one help to solve this issue. I want to download the file and also downloaded file should be open.
The BackgroundDownloader.CreateDownload method initializes a DownloadOperation object that contains the specified Uri and the file that the response is written to.
When the DownloadOperation is created, it does not starts the download operation.
We should be able to use DownloadOperation.StartAsync to start the download operation.
For example:
Uri source = new Uri(selectedfile.DocumentPath);
StorageFile destinationFile = await KnownFolders.PicturesLibrary.CreateFileAsync("ab.pdf", CreationCollisionOption.GenerateUniqueName);
BackgroundDownloader downloader = new BackgroundDownloader();
DownloadOperation download = downloader.CreateDownload(source, destinationFile);
await download.StartAsync();
If you want to know more detail about down load file, please refer the Background transfer sample.

Saving an image asset to a storage file in a local folder in UWP

I have an image in the Assets folder of my UWP project ("Assets/Nophoto.jpg") that I would like to save as a StorageFile in the app's local folder.
The only way I have seen to save an image in a StorageFile is with the FilePicker.
FileOpenPicker FilePicker = new FileOpenPicker();
FilePicker.FileTypeFilter.Add(".jpeg");
FilePicker.FileTypeFilter.Add(".png");
FilePicker.FileTypeFilter.Add(".jpg");
FilePicker.ViewMode = PickerViewMode.Thumbnail;
FilePicker.CommitButtonText = "Picture";
FilePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
PictureFile = await FilePicker.PickSingleFileAsync();
How would I go about storing an image file(.jpeg, .png, .bmp, ect..) in a StorageFile
Answering your question:
I have an image in the Assets folder of my UWP project ("Assets/Nophoto.jpg") that I would like to save as a StorageFile in the app's local folder.
You can do it for example like this - getting file via Uri (note that it can also be accessed via InstaledLocation folder):
StorageFile sourceFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/Nophoto.jpg"));
await sourceFile.CopyAsync(ApplicationData.Current.LocalFolder);
Once you have a StorageFile (no matter if obtained via picker/from installation folder/other) then you can copy it to other StorageFolder. If you have an image via a stream or pixel buffer, then you can create a file and write to it by using a stream.

Error when using Bitmap.SetSource

I'm working on an app which loads images from a chosen folder to a gridView.
when I use SetSource to set the BitmapImage source I receive an error "insufficient memory" after loading some of the images.
when I use the constructor with a Uri path it works fine. but it will only display images from the projects directory.
StorageFolder folder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync(RecentToken);
StorageFile file = await folder.GetFileAsync(path);
IRandomAccessStream stream = await file.OpenReadAsync();
image = new BitmapImage(new Uri(file.Path));//loads only within project directory
image.SetSource(stream);//causes insufficient memory
stream.Dispose();
Found the solution!
I set the BitmaPimage's DecodePixelHeight to the size of the Displayed image and it works fine now!

Categories

Resources