private void Gallery_Click(object sender, object e)
{
view = CoreApplication.GetCurrentView();
var filePicker = new FileOpenPicker
{
SuggestedStartLocation = PickerLocationId.PicturesLibrary,
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");
mediaCapture.StopPreviewAsync();
filePicker.PickSingleFileAndContinue();
view.Activated += ViewActivated;
}
private async void ViewActivated(CoreApplicationView sender, IActivatedEventArgs args)
{
var arguments = args as FileOpenPickerContinuationEventArgs;
if (arguments != null && arguments.Files.Count != 0)
{
view.Activated -= ViewActivated;
var storageFile = arguments.Files[0];
var file =
await
ApplicationData.Current.LocalFolder.CreateFileAsync("Photo.jpg",
CreationCollisionOption.GenerateUniqueName);
await storageFile.CopyAndReplaceAsync(file);
var bmpImage = new BitmapImage(new Uri(file.Path));
UseThePhoto(bmpImage);
}
else
await mediaCapture.StartPreviewAsync();
}
I have this code above. When I choose an image from a gallery I can use it in an Image control that is on the same page. However, if I want to navigate to any other page, I get an error. No details from it. The code ends in App.g.i.cs
Problem solved. I was using not the blank page template but the basic page. And for some reason the method OnNavigatedFrom invoked this error so i created an override and let it empty, so it couldn't call the navigation helper class.
Related
I'm trying to store a model informations in my Data Base, this informations contain an image, so I created a simple view with some entrys, with a media picker button to pick it and an image witch display the picked one.
This is the button method and the store method:
async void Button_Clicked(System.Object sender, System.EventArgs e)
{
var result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
{
Title = "Please pick a photo"
});
if (result != null)
{
var stream = await result.OpenReadAsync();
resultImage.Source = ImageSource.FromStream(() => stream);
}
}
public async Task SaveMachine()
{
var machine = new Machine
{
Machine_Name = nom.Text,
Machine_Qr = qr.Text,
Files = resultImage
};
await _rest.AddMachine(machine);
await Shell.Current.GoToAsync("..");
}
But I can't create Files = resultImage because the Files in the model is in IFormFile.
What I'm trying to do in a UWP app with Win2D:
User pressed a button to add an image and picks their file.
That file gets loaded as a resource for a Canvas Control.
The image then gets rendered to the current drawing session
When the button is clicked:
private async void btnAddPicture_Click(object sender, RoutedEventArgs e)
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
overlayPictureFile = await picker.PickSingleFileAsync();
if (overlayPictureFile == null)
{
txbNotification.Text = "File Picking cancelled";
return;
}
else
{
txbNotification.Text = "Picture Loaded";
}
using (IRandomAccessStream stream = await overlayPictureFile.OpenAsync(FileAccessMode.Read))
{
var device = new CanvasDevice();
createdBitmap = await CanvasBitmap.LoadAsync(device, stream);
}
}
In the drawing function:
void CanvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
if (createdBitmap != null)
{
args.DrawingSession.DrawImage(createdBitmap, Drawing.FindDefaultRect());
}
drawingCanvas.Invalidate();
}
Everything will compile but the moment I press the button to add an image it breaks here.
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif
I'm already loading some image in this, but those are all part of the program and are created before the canvas is created with these. Not sure how to do that with ones the user picks.
private void drawingCanvas_CreateResources(CanvasControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args)
{
args.TrackAsyncAction(CreateResourcesAsync(sender).AsAsyncAction());
}
private async Task CreateResourcesAsync(CanvasControl sender)
{
logo = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Pictures/Logo_BlackBorders.png"));
}
Update:
Where I currently am drawing things. This is the canvas I'm trying to add the image to.
void CanvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
//Drawing a bunch of stuff
}
private void drawingCanvas_CreateResources(CanvasControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args)
{
args.TrackAsyncAction(CreateResourcesAsync(sender).AsAsyncAction());
}
private async Task CreateResourcesAsync(CanvasControl sender)
{
logo = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Pictures/Logo.png"));
}
Load an Image to a Canvas Control from a File Picker
For your scenario, you could get CanvasDrawingSession with CreateDrawingSession method. And then use this drawingsession to draw picked image to current CanvasControl.
For example.
private async void btnAddPicture_Click(object sender, RoutedEventArgs e)
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
var overlayPictureFile = await picker.PickSingleFileAsync();
if (overlayPictureFile == null)
{
return;
}
else
{
}
using (IRandomAccessStream stream = await overlayPictureFile.OpenAsync(FileAccessMode.Read))
{
//get canvascontrol's Device property.
CanvasDevice device = drawingCanvas.Device;
createdBitmap = await CanvasBitmap.LoadAsync(device, stream);
//use device property to make renderer
var renderer = new CanvasRenderTarget(device,
createdBitmap.SizeInPixels.Width,
createdBitmap.SizeInPixels.Height, createdBitmap.Dpi);
//make ds with above renderer.
using (var ds = renderer.CreateDrawingSession())
{
ds.DrawImage(createdBitmap, 0, 0);
}
}
}
A similar question got asked here, but I also would like to know if there is a way to limit this filePicker to 4 images or would I have to implement this by my own?
Thank You
It's not pretty but it works for now.
I've declared two global variables of the type ObservableCollection called myimageList and alt-myImageList. I've just check the size of these collections and proceed from this point on.
That's the code:
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;
foreach (var item in args.Files)
{
// instead of item args.Files[0];
StorageFile storageFile = item;
var stream = await storageFile.OpenAsync(FileAccessMode.Read);
var bitmapImage = new BitmapImage();
await bitmapImage.SetSourceAsync(stream);
var wbImage = new WriteableBitmap(bitmapImage.PixelWidth, bitmapImage.PixelHeight);
wbImage.SetSource(stream);
//var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
//bitmapImage.UriSource = new Uri(item.Path, UriKind.Absolute);
if (myImageList.Count < 4)
{
myImageList.Add(bitmapImage);
alt_myImageList.Add(item);
ErrorMessage.Text = "";
}
else
{
ErrorMessage.Text = "Please pick not more than 4 pictures";
}
}
}
I want to workk with an image picked by file picker in windows phone 8.1.I picked an image using this code
private async void gallery_Tapped(object sender, TappedRoutedEventArgs 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");
// Launch file open picker and caller app is suspended and may be terminated if required
openPicker.PickSingleFileAndContinue();
}
public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
if (args.Files.Count > 0)
{
StorageFile file=args.Files[0];
IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(fileStream);
MyImage.Source=bitmapImage;
}
else
{
}
}
ContinueFileOpenPicker not executing I tried this but unable to understand that.Can any here guide me step by step what should I do to make it work.Thanks
protected override void OnActivated(IActivatedEventArgs args)
{
var root = Window.Current.Content as Frame;
var mainPage = root.Content as MainPage;
if (mainPage != null && args is FileOpenPickerContinuationEventArgs)
{
mainPage.ContinueFileOpenPicker(args as FileOpenPickerContinuationEventArgs);
}
}
Put this code in app.xaml.cs it will work..
I'm new to C# and I want to create an application metro who can take picture and save themself in localstorage. I know, i need to use isolated storage but i really don't understand how to use it for image. I saw a lot of examples for string but not for picture.
If anyone know how to do it ? Actually i take a picture and i ask the user to record it where he wants. But I want an auto record after the user take the picture. This my code for the moment :
private async void Camera_Clicked(object sender, TappedRoutedEventArgs e)
{
CameraCaptureUI camera = new CameraCaptureUI();
camera.PhotoSettings.CroppedAspectRatio = new Size(16, 9);
StorageFile photo = await camera.
CaptureFileAsync(CameraCaptureUIMode.Photo);
if (photo != null)
{
BitmapImage bmp = new BitmapImage();
IRandomAccessStream stream = await photo.
OpenAsync(FileAccessMode.Read);
bmp.SetSource(stream);
ImageSource.Source = bmp;
ImageSource.Visibility = Visibility.Visible;
appSettings[photoKey] = photo.Path;
FileSavePicker savePicker = new FileSavePicker();
savePicker.FileTypeChoices.Add
("jpeg image", new List<string>() { ".jpeg" });
savePicker.SuggestedFileName = "New picture";
StorageFile ff = await savePicker.PickSaveFileAsync();
if (ff != null)
{
await photo.MoveAndReplaceAsync(ff);
}
}
}
All what you need to do is to replace File Picker logic with retrieving of StorageFile object in Local folder, for example like this:
private async void Camera_Clicked(object sender, TappedRoutedEventArgs e)
{
CameraCaptureUI camera = new CameraCaptureUI();
camera.PhotoSettings.CroppedAspectRatio = new Size(16, 9);
StorageFile photo = await camera.
CaptureFileAsync(CameraCaptureUIMode.Photo);
if (photo != null)
{
var targetFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("some_file_name.jpg");
if (targetFile != null)
{
await photo.MoveAndReplaceAsync(targetFile);
}
}
}