upload photo using the FileOpenPicker windows8.1 - c#

I'm trying to upload photo to my application using the FileOpenPicker and when I write the following code:
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");
// Open a stream for the selected file
StorageFile file = await open.PickSingleFileAsync();
// ImageSource im = (new Uri (file.Path));
ChildPic.Source = new BitmapImage(new Uri(file.Path));
it did not give me an error but the Image control is left blank.
there is value in the Path: C:\Users\Pictures\New folder(15).jpg

new BitmapImage(Uri) will not work with any path. It supports only URI having protocol http, ms-appx, ms-appdata. You have to use stream.
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");
// Open a stream for the selected file
StorageFile file = await open.PickSingleFileAsync();
// ImageSource im = (new Uri (file.Path));
var bmp = new BitmapImage();
using (var strm = await file.OpenReadAsync())
{
bmp.SetSource(strm);
ChildPic.Source = bmp;
}

Related

How to get file path of opened file in UWP Application?

I want to get path of opened file with FileOpenPicker in my UWP application. I have a OpenFile named method. OpenFile methods code:
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add(".txt");
StorageFile storageFile = await openPicker.PickSingleFileAsync();
if (storageFile != null)
{
var stream = await storageFile.OpenAsync(FileAccessMode.ReadWrite);
using (StreamReader sReader = new StreamReader(stream.AsStream()))
{
Document.Text = sReader.ReadToEnd();
Document.IsSaved = true;
}
}
What do I need to do get file path.
storageFile.Path will give you the path to the file, if it has one (which it should in this case).
See https://learn.microsoft.com/uwp/api/windows.storage.storagefile.path

UWP video import

I am trying to import a video into a UWP canvas.
I have this code that successfully imports a picture:
private async void AddImageButton_Click(object sender, RoutedEventArgs e)
{
Image MyImage = new Image();
var picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".png");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", file);
// var files = await file.GetFilesAsync();
var bitmap = new BitmapImage();
var stream = await file.OpenReadAsync();
// AddHandler(, new ExceptionRoutedEventHandler(Bitmap_ImageFailed), true);
bitmap.ImageFailed += Bitmap_ImageFailed;
await bitmap.SetSourceAsync(stream);
MyImage.Source = bitmap;
AddHandler(ManipulationStartedEvent, new ManipulationStartedEventHandler(Image_ManipulationStarted), true);
AddHandler(ManipulationDeltaEvent, new ManipulationDeltaEventHandler(Image_ManipulationDelta), true);
AddHandler(ManipulationCompletedEvent, new ManipulationCompletedEventHandler(Image_ManipulationCompleted), true);
ManipulationMode = ManipulationModes.All;
MyImage.RenderTransform = ImageTransforms;
parentCanvas.Children.Add(MyImage);
}
}
I tried adapting this to import a video but got stuck when converting the bitmap to a MediaPlayerElement. Any suggestions?
Thanks!
According your above code of adding image to the Canvas, you can try the following code to add the MediaPlayerElement to the Canvas and use the FileOpenPicker to picker a media file as the MediaPlayerElement's source. You can make some modification to meet your requirement.
private async void AddMediaPlayerElementButton_Click_1(object sender, RoutedEventArgs e)
{
var picker = new FileOpenPicker();
picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
picker.FileTypeFilter.Add(".mp4");
StorageFile file = await picker.PickSingleFileAsync();
MediaPlayerElement mediaPlayer = new MediaPlayerElement() { AreTransportControlsEnabled = true };
if (file != null)
{
mediaPlayer.Source = MediaSource.CreateFromStorageFile(file);
}
parentCanvas.Children.Add(mediaPlayer);
}

What´s the best practice for barcode scanners under Windows Phone 8.1?

I am currently working on a barcode scanner for Code128 in my Windows Phone 8.1 App. I tried the Libary ZXing.Net 14.0.1.0, but my implementation only recognizes QR-Codes.
Has somebody experience with my problem or knows the best practice for barcode scanners on Windows Phone 8.1? Are there other Libarys available?
MediaCapture captureManager = new MediaCapture();
async private void Capture_Photo_Click(object sender, RoutedEventArgs e)
{
//Create JPEG image Encoding format for storing image in JPEG type
ImageEncodingProperties imgFormat = ImageEncodingProperties.CreateJpeg();
// create storage file in local app storage
StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync("Photo" + counter + ".jpg", CreationCollisionOption.ReplaceExisting);
// take photo and store it on file location.
await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file);
//// create storage file in Picture Library
//StorageFile file = await KnownFolders.PicturesLibrary.CreateFileAsync("Photo.jpg",CreationCollisionOption.GenerateUniqueName);
// Get photo as a BitmapImage using storage file path.
BitmapImage bmpImage = new BitmapImage(new Uri(file.Path));
// show captured image on Image UIElement.
imagePreivew.Source = bmpImage;
var stream = await file.OpenReadAsync();
var writeableBmp = new WriteableBitmap(1, 1);
writeableBmp.SetSource(stream);
writeableBmp = new WriteableBitmap(writeableBmp.PixelWidth, writeableBmp.PixelHeight);
stream.Seek(0);
writeableBmp.SetSource(stream);
ZXing.IBarcodeReader reader = new BarcodeReader
{
AutoRotate = true
};
reader.Options.TryHarder = true;
var result = reader.Decode(writeableBmp);
}

get path of selected image by fileopenpicker windows phone 8.1 C#

I got an image by using FileOpenPicker. I want to save the path of the image in database, so that by using that path I can get that image any other screen,,
Now I simply send my image path to second page. but with the same Path I'm not able to get image.
Here is my code.
private async void button_Click(object sender, RoutedEventArgs e)
{
ImagePath = string.Empty;
FileOpenPicker filePicker = new FileOpenPicker();
filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
filePicker.ViewMode = PickerViewMode.Thumbnail;
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 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);
**//ImagePath is global string. I get the image path here**
ImagePath = storageFile.Path;
var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
await bitmapImage.SetSourceAsync(stream);
var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
//image is my image control in Xaml.
image.Source = bitmapImage;
}
}
private void image_click(object sender, TappedRoutedEventArgs e)
{
this.Frame.Navigate(typeof(detail), ImagePath);
}
By tapping on Image I move to detail screen. where I have another image control where I wan to show the same image ,,,,getting through path.
My Detail screen code is:
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
var imagePath = e.Parameter as string;
image2.Source = new BitmapImage(new Uri(imagePath, UriKind.RelativeOrAbsolute));
}
The above code didn't work.
Then I tried another way,,
var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri(imagePath));
Stream stream = await file.OpenStreamForReadAsync();
using (var memStream = new MemoryStream())
{
await stream.CopyToAsync(memStream);
memStream.Position = 0;
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(memStream.AsRandomAccessStream());
// create a new stream and encoder for the new image
InMemoryRandomAccessStream mrAccessStream = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(mrAccessStream, decoder);
// convert the bitmap to a 400px by 600px bitmap
encoder.BitmapTransform.ScaledHeight = 400;
encoder.BitmapTransform.ScaledWidth = 600;
try
{
await encoder.FlushAsync();
}
catch (Exception ex)
{
string s = ex.ToString();
}
// render the stream to the screen
WB = new WriteableBitmap(500, 500);
WB.SetSource(mrAccessStream);
image2.Source = WB;
This also didn't work.
I think the problem is in path. I got path awkward path like
"C:\\Data\\Users\\Public\\Pictures\\Saved Pictures\\Image-130872081698409356.jpeg".
Please help me to get rid of this problem.
This is by design and it is a security issue. Imagine what would happen if just knowing the path to a file you could access that file. The security model of store apps would be broken.
Note that is the file picker that provides you with a file that you have access to. You can start from a file picker to get access to a file. You cannot start from a path to get access to a file. A file picker guarantees that the user will be involved and hence aware of what is going on. If you were allowed to start from a path and programmatically access a file that would violate the security rules for app stores.

Convert byte Array or File Storage to Bitmap Image

After I pick file to storage file , How can I convert this file to be שn image in order to display it like profile picture?
I converted the file to byte array but don't know what to do next or there is an other way?
Here is my code :
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
openPicker.FileTypeFilter.Add(".png");
openPicker.FileTypeFilter.Add(".Jpeg");
openPicker.FileTypeFilter.Add(".Jpg");
StorageFile file = await openPicker.PickSingleFileAsync();
var stream = await file.OpenReadAsync();
using (var dataReader = new DataReader(stream))
{
var bytes = new byte[stream.Size];
await dataReader.LoadAsync((uint)stream.Size);
dataReader.ReadBytes(bytes);
var stream2 = new MemoryStream(bytes);
}
Below code converts bytes into BitmapImage
BitmapImage image1 = new BitmapImage();
InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream();
ms.WriteAsync(tBytes.AsBuffer());
ms.FlushAsync().AsTask().Wait();
ms.Seek(0);
image1.SetSource(ms);
image.Source = image1;
I got this from somewhere, Try this if it helps
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".cmp");
openPicker.FileTypeFilter.Add(".png");
openPicker.FileTypeFilter.Add(".tif");
openPicker.FileTypeFilter.Add(".gif");
openPicker.FileTypeFilter.Add(".bmp");
StorageFile file = await openPicker.PickSingleFileAsync();
IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
BitmapImage bmp = new BitmapImage();
bmp.SetSource(stream);
Image1.Source = bmp;

Categories