I want to pick an image from my pictures album in windows phone 8.1 . For this I used this code but its gives error
private async void gallery_Tapped(object sender, TappedRoutedEventArgs e)
{
FileOpenPicker opener = new FileOpenPicker();
opener.ViewMode = PickerViewMode.Thumbnail;
opener.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
opener.FileTypeFilter.Add(".jpg");
opener.FileTypeFilter.Add(".jpeg");
opener.FileTypeFilter.Add(".png");
StorageFile file = await opener.PickSingleFileAsync();
if (file != null)
{
// We've now got the file. Do something with it.
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
await bitmapImage.SetSourceAsync(stream);
var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
MyImage.Source=bitmapImage;
}
else
{
//OutputTextBlock.Text = "The operation may have been cancelled.";
}
}
Error
I think you can handle the OnActivated event even in the page where you required. Something like this
CoreApplicationView view = CoreApplication.GetCurrentView();
ImagePath=string.Empty;
FileOpenPicker filePicker = new FileOpenPicker();
filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
filePicker.ViewMode = PickerViewMode.Thumbnail;
// Filter to include a sample subset of file types
filePicker.FileTypeFilter.Clear();
filePicker.FileTypeFilter.Add(".bmp");
filePicker.FileTypeFilter.Add(".png");
filePicker.FileTypeFilter.Add(".jpeg");
filePicker.FileTypeFilter.Add(".jpg");
filePicker.PickSingleFileAndContinue();
view.Activated += viewActivated;
private void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1)
{
FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;
if (args != null)
{
if (args.Files.Count == 0) return;
view.Activated -= viewActivated;
storageFileWP = args.Files[0];
}
}
When you select the files from the picker the above method will be called. I believe it helps you.
Using FileOpenPicker in Windows Phone 8.1 to choose picture from Picture Gallery.
Step 1: Add Picture Library Capability in your Windows Phone 8.1 app.
Step 2: Add File Open Picker as a declaration.
Step 3: Add a button and image to MainPage.xaml.
<Grid>
<Image Name="img"/>
<Button Content="click me" Click="Button_Click"/>
</Grid>
Step 4: Add global variable view.
CoreApplicationView view;
Step 4.1 Initialize in page constructor.
view = CoreApplication.GetCurrentView();
Step 5: Add the code to call the File Open Picker on Button Click event.
private void Button_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker filePicker = new FileOpenPicker();
filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
filePicker.ViewMode = PickerViewMode.Thumbnail;
// Filter to include a sample subset of file types
filePicker.FileTypeFilter.Clear();
filePicker.FileTypeFilter.Add(".bmp");
filePicker.FileTypeFilter.Add(".png");
filePicker.FileTypeFilter.Add(".jpeg");
filePicker.FileTypeFilter.Add(".jpg");
filePicker.PickSingleFileAndContinue();
view.Activated += viewActivated;
}
Step 6: On View activated event set the image to the MainPage.
private async void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1)
{
FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;
if (args != null)
{
if (args.Files.Count == 0) return;
view.Activated -= viewActivated;
StorageFile storageFile = args.Files[0];
var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
await bitmapImage.SetSourceAsync(stream);
var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
img.Source=bitmapImage;
}
}
It also allows you to take a photo and use it.
Reference:
Using FileOpenPicker in Windows Phone 8.1 to choose picture from Picture Gallery
Use RoutedEventArgs instead of TappedRoutedEventArgs for button click in wp 8.1 xaml
Don't use async key word
private void OpenImageFile(object sender, RoutedEventArgs e)
{
FileOpenPicker filePicker = new FileOpenPicker();
filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
filePicker.ViewMode = PickerViewMode.Thumbnail;
// Filter to include a sample subset of file types
filePicker.FileTypeFilter.Clear();
filePicker.FileTypeFilter.Add(".bmp");
filePicker.FileTypeFilter.Add(".png");
filePicker.FileTypeFilter.Add(".jpeg");
filePicker.FileTypeFilter.Add(".jpg");
filePicker.PickSingleFileAndContinue();
view.Activated += viewActivated;
}
private void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1)
{
FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;
if (args != null)
{
if (args.Files.Count == 0) return;
view.Activated -= viewActivated;
StorageFile SelectedImageFile = args.Files[0];
}
}
And use CoreApplicationView view; Any where in the class out side of every method as global
Don't forget to use view = CoreApplication.GetCurrentView(); inside the constructor of the relevant page class after InitializeComponent(); method
I think this will help :) Thanks
var fill = await StorageFile.GetFileFromPathAsync(selectItem.FolderPath);
BitmapImage bit = new BitmapImage();
if (fill != null)
{
// We've now got the file. Do something with it.
var stream = await fill.OpenAsync(Windows.Storage.FileAccessMode.Read);
//var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
//await bitmapImage.SetSourceAsync(stream);
bit.SetSource(stream);
imgTeste.Source = bit;
pvMestre.SelectedIndex = 1;
}
else
{
//OutputTextBlock.Text = "The operation may have been cancelled.";
}
Related
I have a Button on a Page with the following method on the click event of the button:
StorageFile _sourceFile;
private string _sourceToken;
private async void btnSelect_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker fop = new FileOpenPicker();
fop.FileTypeFilter.Add(".mp4");
StorageFile inFile = await fop.PickSingleFileAsync();
_sourceToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(inFile);
_sourceFile = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(_sourceToken);
mediaElement.AutoPlay = false;
IRandomAccessStream stream = await _outFile.OpenAsync(FileAccessMode.ReadWrite);
mediaElement.SetSource(stream, _outFile.ContentType);
}
If I click play on the MediaElement the video I select plays fine.
I also have another button which has the following code on its click event:
private async void btnExport_Click(object sender, RoutedEventArgs e)
{
StorageFile outFile = await KnownFolders.VideosLibrary.CreateFileAsync("Outfie.mp4", CreationCollisionOption.ReplaceExisting);
MediaEncodingProfile profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD1080p);
MediaTranscoder transcoder = new MediaTranscoder();
PrepareTranscodeResult prepareOp = await transcoder.PrepareFileTranscodeAsync(_sourceFile, outFile, profile);
if (prepareOp.CanTranscode)
{
var transcodeOp = prepareOp.TranscodeAsync();
transcodeOp.Progress += new AsyncActionProgressHandler<double>(TranscodeProgress);
transcodeOp.Completed += new AsyncActionWithProgressCompletedHandler<double>(TranscodeComplete);
}
else
{
switch (prepareOp.FailureReason)
{
case TranscodeFailureReason.CodecNotFound:
System.Diagnostics.Debug.WriteLine("Codec not found.");
break;
case TranscodeFailureReason.InvalidProfile:
System.Diagnostics.Debug.WriteLine("Invalid profile.");
break;
default:
System.Diagnostics.Debug.WriteLine("Unknown failure.");
break;
}
}
}
Unfortunately the line transcoder.PrepareFileTranscodeAsync throws an UnauthorizedAccessException. But if I use the following instead of _sourceFile it works:
StorageFile sourceFile = await KnownFolders.VideosLibrary.GetFileAsync("sourceFile.mp4");
The error being thrown is:
System.UnauthorizedAccessException: 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'
To be clear, I am selecting files OUTSIDE the KnownFolders Enumeration, hence I am using Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.
Can anyone explain why?
EDIT:
If I change the source file to be the result of a FileOpenPicker then it works. So it begs the question, why is the FutureAccessList not working??
private async void btnExport_Click(object sender, RoutedEventArgs e)
{
StorageFile outFile = await KnownFolders.VideosLibrary.CreateFileAsync("Outfie.mp4", CreationCollisionOption.ReplaceExisting);
FileOpenPicker fop = new FileOpenPicker();
fop.SuggestedStartLocation = PickerLocationId.ComputerFolder;
fop.FileTypeFilter.Add(".mp4");
StorageFile sourceFile = await fop.PickSingleFileAsync();
MediaEncodingProfile profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.HD1080p);
MediaTranscoder transcoder = new MediaTranscoder();
PrepareTranscodeResult prepareOp = await transcoder.PrepareFileTranscodeAsync(sourceFile, outFile, profile);
if (prepareOp.CanTranscode)
{
var transcodeOp = prepareOp.TranscodeAsync();
transcodeOp.Progress += new AsyncActionProgressHandler<double>(TranscodeProgress);
transcodeOp.Completed += new AsyncActionWithProgressCompletedHandler<double>(TranscodeComplete);
}
else
{
switch (prepareOp.FailureReason)
{
case TranscodeFailureReason.CodecNotFound:
System.Diagnostics.Debug.WriteLine("Codec not found.");
break;
case TranscodeFailureReason.InvalidProfile:
System.Diagnostics.Debug.WriteLine("Invalid profile.");
break;
default:
System.Diagnostics.Debug.WriteLine("Unknown failure.");
break;
}
}
}
Do you have access to the file you're trying to write to? Maybe it's read-only or created by another user other than yourself? (Right click + Properties on the file in Explorer should give you a clearer picture of the file permissions)
Also, you might get that exception if you're trying to write to a folder whom you don't have access to.
Check your credentials, I would guess it's something related to that.
So it seems the fact I was opening the source file in ReadWrite mode
IRandomAccessStream stream = await _outFile.OpenAsync(FileAccessMode.ReadWrite);
Was the cause of the issues. According to this page
Use read/write mode only when you're ready to write immediately in order to avoid conflicts with other operations.
So I changed to this and all works well
IRandomAccessStream stream = await _outFile.OpenAsync(FileAccessMode.Read);
I have an image SurfaceImageSource and I would convert it to PNG.
I tried to use this project:
link
I tried to use SharpDX library, but I did not succeed.
private void initialize()
{
StorageFolder folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("folder", CreationCollisionOption.OpenIfExists);
StorageFile imagePng = await folder.CreateFileAsync("file.png", CreationCollisionOption.ReplaceExisting);
if (imagePng != null)
{
//surfaceImageSource to PNG method
surfaceToPng(surfaceImage,imagePng);
}
}
private void surfaceToPng(SurfaceImageSource surface,StorageFile imagePng){
IRandomAccessStream stream = await imagePng.OpenAsync(FileAccessMode.ReadWrite);
//.....//
}
The sample you linked is about "How to save SurfaceImageSource target as an image in Universal app" which is just what you want. It create a C++ Windows Runtime Component named "MyImageSourceComponent" and provide a sealed class named "MyImageSource" which contains the method public void SaveSurfaceImageToFile(IRandomAccessStream randomAccessStream); You can call this method to save the SurfaceImageSource to png.
uint imageWidth;
uint imageHeight;
MyImageSource myImageSource;
public MainPage()
{
this.InitializeComponent();
imageWidth = (uint)this.MyImage.Width;
imageHeight = (uint)this.MyImage.Height;
myImageSource = new MyImageSource(imageWidth, imageHeight, true);
this.MyImage.Source = myImageSource;
}
private async void btnSave_Click(object sender, RoutedEventArgs e)
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.FileTypeChoices.Add("Png file", new List<string>() { ".png" });
savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.ReadWrite);
myImageSource.SaveSurfaceImageToFile(stream);
}
}
Although this sample is for Windows 8.1, it also should be able work with uwp app. I helped convert the sample into uwp app here you can reference. I created a new uwp app with a windows runtime component and reference the code of the "MyImageSouceComponent" in the sample. Then add the runtime component as reference to the uwp project. At last use the above code call the SaveSurfaceImageToFile method.
Application works when its connected to PC and run with debugger. The problem starts when I disconnect phone from PC, run app from phone and try to open gallery and set image to image control. I tried to write error in a file on try/catch but catch is never called, like there is no error on app executing.
This is code where i select img:
private async void galleryBtn_Click(object sender, RoutedEventArgs e)
{
try
{
FileOpenPicker filePicker = new FileOpenPicker();
filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
filePicker.ViewMode = PickerViewMode.Thumbnail;
// Filter to include a sample subset of file types
filePicker.FileTypeFilter.Clear();
filePicker.FileTypeFilter.Add(".bmp");
filePicker.FileTypeFilter.Add(".png");
filePicker.FileTypeFilter.Add(".jpeg");
filePicker.FileTypeFilter.Add(".jpg");
filePicker.PickSingleFileAndContinue();
view.Activated += viewActivated;
}
catch (Exception err)
{
string error = err.StackTrace.ToString();
await saveStringToLocalFile("test11", error);
}
}
And than it goes to :
private async void viewActivated(CoreApplicationView sender, IActivatedEventArgs args1)
{
try
{
FileOpenPickerContinuationEventArgs args = args1 as FileOpenPickerContinuationEventArgs;
if (args != null)
{
if (args.Files.Count == 0) return;
view.Activated -= viewActivated;
StorageFile storageFile = args.Files[0];
var stream = await storageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
await bitmapImage.SetSourceAsync(stream);
var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
var obj = App.Current as App;
obj.ImageToEdit = bitmapImage;
obj.fileTransfer = storageFile;
checkTorch = -1;
await newCapture.StopPreviewAsync();
Frame.Navigate(typeof(EditImage));
}
}
catch (Exception err) {
string error = err.StackTrace.ToString();
await saveStringToLocalFile("test11", error);
}
}
When img is selected i open screen for image editing and run this
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
try
{
var obj = App.Current as App;
slika = obj.ImageToEdit;
original = obj.ImageToEdit;
ImagePreview.Source = slika;
RotateTransform myRotateTransform = new RotateTransform();
myRotateTransform.Angle = 0;
ImagePreview.RenderTransform = myRotateTransform;
var localSettings = ApplicationData.Current.LocalSettings;
}
catch (Exception err)
{
string error = err.StackTrace.ToString();
await saveStringToLocalFile("test11", error);
}
}
That is all, any advice is appreciated;
Problem was with my MediaCapture. First use mediaCapture.stopPreviewAsync(); to stop preview and than you must release the mediaCapture.
Before you call fileOpener use this code:
newCapture.Dispose();
In order to catch unhandled exceptions you can use a global exception catcher,
in App.xaml.cs file define:
public App()
{
this.InitializeComponent();
this.Suspending += this.OnSuspending;
this.UnhandledException += UnhandledExceptionHandler;
}
private void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
log.Critical(e.Exception);
}
It's important to understand that not all exceptions can be caught using try\catch such as Corrupt State Exceptions:
https://msdn.microsoft.com/en-us/magazine/dd419661.aspx#id0070035
In this case you can debug the issue by viewing the .dmp file generated by your application found in: {Phone}\Documents\Debug
I want to open an image, edit it, and then save it. I am able to open a file, but I have problems saving it. The way I have written the code, I can only save a file with .jpg but there is nothing in it.
Please explain to me how to save the image I have opened and edited(not made yet).
public sealed partial class MainPage : Page
{
BitmapImage originalImage = new BitmapImage();
public MainPage()
{
this.InitializeComponent();
}
private async void OpenButton_Click(object sender, RoutedEventArgs e)
{
var filePicker = new FileOpenPicker();
filePicker.FileTypeFilter.Add(".jpg");
filePicker.FileTypeFilter.Add(".jpeg");
filePicker.FileTypeFilter.Add(".gif");
filePicker.ViewMode = PickerViewMode.Thumbnail;
filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
filePicker.SettingsIdentifier = "PicturePicker";
filePicker.CommitButtonText = "Select File";
StorageFile selectedFile = await filePicker.PickSingleFileAsync();
var stream = await selectedFile.OpenAsync(FileAccessMode.Read);
if (selectedFile != null)
{
originalImage.SetSource(stream);
pictureBox.Source = originalImage;
}
}
private async void SaveButton_Click(object sender, RoutedEventArgs e)
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
savePicker.FileTypeChoices.Add("jpeg image", new List<string>() { ".jpg" });
savePicker.SuggestedFileName = "EditedImage";
StorageFile file = await savePicker.PickSaveFileAsync();
}
}
After creating an Images file you need to Update it see FileSavePicker class.
Add the following code in your SaveButton_Click method and try modify it.
This will let you update your created file into real image file.
if (file != null)
{
// Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
CachedFileManager.DeferUpdates(file);
// write to file
await FileIO.WriteTextAsync(file, file.Name);
// Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
// Completing updates may require Windows to ask for user input.
FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
if (status == FileUpdateStatus.Complete)
{
OutputTextBlock.Text = "File " + file.Name + " was saved.";
}
else
{
OutputTextBlock.Text = "File " + file.Name + " couldn't be saved.";
}
}
else
{
OutputTextBlock.Text = "Operation cancelled.";
}
I want to open FileOpenPicker for two times one after the other.i.e)After selecting the image from FileOpenPicker for first time it returns back to the page with the selected image,that time a MessageDialog is shown,And clicking OK button on MessageDialog I am opening the FileOpenPicker again,this time I get a weird crash on
open.FileTypeFilter.Clear();
as 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'.And My code is below.
private async void PickImage()
{
FileOpenPicker open = new FileOpenPicker();
open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
open.ViewMode = PickerViewMode.Thumbnail;
// Filter to include a sample subset of file types
open.FileTypeFilter.Clear();
open.FileTypeFilter.Add(".bmp");
open.FileTypeFilter.Add(".png");
open.FileTypeFilter.Add(".jpeg");
open.FileTypeFilter.Add(".jpg");
StorageFile file = await open.PickSingleFileAsync();
if (file != null)
{
if (prop1.Source == null)
{
Dialogpopup();
return;
}
else if (prop2.Source == null)
{
Dialogpopup();
return;
}
}
}
private async void Dialogpopup()
{
MessageDialog msgDialog = new MessageDialog("Would you like to add additional photos?");
UICommand okBtn = new UICommand("Yes");
okBtn.Invoked = OkBtnClick;
msgDialog.Commands.Add(okBtn);
UICommand cancelBtn = new UICommand("No");
cancelBtn.Invoked = CancelBtnClick;
msgDialog.Commands.Add(cancelBtn);
//Show message
msgDialog.ShowAsync();
}
private async void OkBtnClick(IUICommand command)
{
img_PointerPressed(img_galary, null);
}
private void img_PointerPressed(object sender, PointerRoutedEventArgs e)
{
if (sender == img_galary)
{
PickImage();
}
}