locked
How to store the image into the local file system

    Question

  • the following is the code to get the image from hard disk to image control.

    abc.xaml

     <Image Height="168" Stretch="Fill" x:Name="images" HorizontalAlignment="Left" Margin="500,-250,0,0"  Width="229"/>
                <Button x:Name="Scenario2Button1" Content="Select Image" FontSize="20" FontFamily="Times New Roman" HorizontalAlignment="Left" Margin="530,-25,0,0" Height="40" Width="182" Click="SelectImageButton_Click"/>

    abc.xaml.cs

    private async void SelectImageButton_Click(object sender, RoutedEventArgs e)  

            {

                FileOpenPicker open = new FileOpenPicker();
                open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                open.ViewMode = PickerViewMode.Thumbnail;
                 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)
                {
                    using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                    {
                        BitmapImage bitmapImage = new BitmapImage();

                        await bitmapImage.SetSourceAsync(fileStream);
                        images.Source = bitmapImage;
                    }
                }
             }

    And i want this image to get stored into the local file system and how to retrieve the image from the local file system.please let me if anyone knows


    amair

    Wednesday, July 03, 2013 12:41 PM

Answers

  • Hi Amair,

    Have a look at this...

    FileOpenPicker open = new FileOpenPicker(); open.SuggestedStartLocation = PickerLocationId.PicturesLibrary; open.ViewMode = PickerViewMode.Thumbnail; open.FileTypeFilter.Clear(); open.FileTypeFilter.Add(".bmp"); open.FileTypeFilter.Add(".png"); open.FileTypeFilter.Add(".jpeg"); open.FileTypeFilter.Add(".jpg"); StorageFile file = await open.PickSingleFileAsync(); string s = ApplicationData.Current.LocalFolder.Path; string fileName = string.Empty; if (file != null) { fileName = file.Name; await file.CopyAsync(ApplicationData.Current.LocalFolder); using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapImage bitmapImage = new BitmapImage(); await bitmapImage.SetSourceAsync(fileStream); images.Source = bitmapImage; } }

    //code to get the file from Local folder

    StorageFolder localFolder = ApplicationData.Current.LocalFolder; bool result = await DoesFileExistAsync(fileName); if (result) { StorageFile localFile = await localFolder.GetFileAsync(fileName); using (IRandomAccessStream fileStream = await localFile.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapImage bitmapImage = new BitmapImage(); await bitmapImage.SetSourceAsync(fileStream); images1.Source = bitmapImage; } }


    static async Task<bool> DoesFileExistAsync(string fileName)
            {
                try
                {
                    await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
    
     <ScrollViewer Grid.Row="2">
                <StackPanel Orientation="Vertical">
                    <Image x:Name="images" Width="100" Height="100" Stretch="Fill" />
                    <Image x:Name="images1" Margin="50,0,0,0" Width="200" Height="200" Stretch="Fill"/>
                </StackPanel>
     </ScrollViewer>

    Here I am displaying the image which is selected from FilePicker and also I am displaying the same image by reading it from the localfolder...

    I am passing the name of the file to read it from local folder.

    You can get this name from DB and pass that value to read the file from Local folder...

    Hope it helps...


    Nazia

    Thursday, July 04, 2013 10:59 AM

All replies

  • To copy file into app's local folder

    await file.CopyAsync(ApplicationData.Current.LocalFolder);

    To retrieve file reference again:

    StorageFile localFile = await ApplicationData.Current.LocalFolder.GetFileAsync("file name here");


    Blog: http://lunarfrog.com/blog

    Wednesday, July 03, 2013 1:18 PM
  • To copy file into app's local folder

    await file.CopyAsync(ApplicationData.Current.LocalFolder);

    To retrieve file reference again:

    StorageFile localFile = await ApplicationData.Current.LocalFolder.GetFileAsync("file name here");


    Blog: http://lunarfrog.com/blog

    The above logic where should i write in my  program.and i want the image name to get stored in sqlite database .how to stored the image in application local fiel system and how to retrieve the image with the file name stored in the sqlite database.can u please send me the sample logic

    amair


    • Edited by shaik amair Thursday, July 04, 2013 5:07 AM text missing
    Thursday, July 04, 2013 5:04 AM
  • Hi Amair,

    Have a look at this...

    FileOpenPicker open = new FileOpenPicker(); open.SuggestedStartLocation = PickerLocationId.PicturesLibrary; open.ViewMode = PickerViewMode.Thumbnail; open.FileTypeFilter.Clear(); open.FileTypeFilter.Add(".bmp"); open.FileTypeFilter.Add(".png"); open.FileTypeFilter.Add(".jpeg"); open.FileTypeFilter.Add(".jpg"); StorageFile file = await open.PickSingleFileAsync(); string s = ApplicationData.Current.LocalFolder.Path; string fileName = string.Empty; if (file != null) { fileName = file.Name; await file.CopyAsync(ApplicationData.Current.LocalFolder); using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapImage bitmapImage = new BitmapImage(); await bitmapImage.SetSourceAsync(fileStream); images.Source = bitmapImage; } }

    //code to get the file from Local folder

    StorageFolder localFolder = ApplicationData.Current.LocalFolder; bool result = await DoesFileExistAsync(fileName); if (result) { StorageFile localFile = await localFolder.GetFileAsync(fileName); using (IRandomAccessStream fileStream = await localFile.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapImage bitmapImage = new BitmapImage(); await bitmapImage.SetSourceAsync(fileStream); images1.Source = bitmapImage; } }


    static async Task<bool> DoesFileExistAsync(string fileName)
            {
                try
                {
                    await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
    
     <ScrollViewer Grid.Row="2">
                <StackPanel Orientation="Vertical">
                    <Image x:Name="images" Width="100" Height="100" Stretch="Fill" />
                    <Image x:Name="images1" Margin="50,0,0,0" Width="200" Height="200" Stretch="Fill"/>
                </StackPanel>
     </ScrollViewer>

    Here I am displaying the image which is selected from FilePicker and also I am displaying the same image by reading it from the localfolder...

    I am passing the name of the file to read it from local folder.

    You can get this name from DB and pass that value to read the file from Local folder...

    Hope it helps...


    Nazia

    Thursday, July 04, 2013 10:59 AM
  • Hi Amair,

    Have a look at this...

    FileOpenPicker open = new FileOpenPicker(); open.SuggestedStartLocation = PickerLocationId.PicturesLibrary; open.ViewMode = PickerViewMode.Thumbnail; open.FileTypeFilter.Clear(); open.FileTypeFilter.Add(".bmp"); open.FileTypeFilter.Add(".png"); open.FileTypeFilter.Add(".jpeg"); open.FileTypeFilter.Add(".jpg"); StorageFile file = await open.PickSingleFileAsync(); string s = ApplicationData.Current.LocalFolder.Path; string fileName = string.Empty; if (file != null) { fileName = file.Name; await file.CopyAsync(ApplicationData.Current.LocalFolder); using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapImage bitmapImage = new BitmapImage(); await bitmapImage.SetSourceAsync(fileStream); images.Source = bitmapImage; } }

    //code to get the file from Local folder

    StorageFolder localFolder = ApplicationData.Current.LocalFolder; bool result = await DoesFileExistAsync(fileName); if (result) { StorageFile localFile = await localFolder.GetFileAsync(fileName); using (IRandomAccessStream fileStream = await localFile.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapImage bitmapImage = new BitmapImage(); await bitmapImage.SetSourceAsync(fileStream); images1.Source = bitmapImage; } }


    static async Task<bool> DoesFileExistAsync(string fileName)
            {
                try
                {
                    await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
     <ScrollViewer Grid.Row="2">
                <StackPanel Orientation="Vertical">
                    <Image x:Name="images" Width="100" Height="100" Stretch="Fill" />
                    <Image x:Name="images1" Margin="50,0,0,0" Width="200" Height="200" Stretch="Fill"/>
                </StackPanel>
     </ScrollViewer>

    Here I am displaying the image which is selected from FilePicker and also I am displaying the same image by reading it from the localfolder...

    I am passing the name of the file to read it from local folder.

    You can get this name from DB and pass that value to read the file from Local folder...

    Hope it helps...


    Nazia

    Error 4 The return type of an async method must be void, Task or Task<T> C:\Users\admin\Documents\Visual Studio 2012\Projects\Smart\Smart\Views\Gro.xaml.cs 183 18 Smart
    Error 3 The type or namespace name 'Task' could not be found (are you missing a using directive or an assembly reference?) C:\Users\admin\Documents\Visual Studio 2012\Projects\Smart\Smart\Views\Gro.xaml.cs 183 18 Smart

    amair

    Thursday, July 04, 2013 11:42 AM
  • use this

    using System.Threading.Tasks;


    Nazia

    Thursday, July 04, 2013 12:20 PM
  • use this

    using System.Threading.Tasks;


    Nazia

          When iam selecting the same image  next time the error is displaying 

    Error:Cannot create a file when that file already exists


    amair

    Thursday, July 04, 2013 12:56 PM
  • You need to make sure that an image with the same name doesnot exists....if it exists you can append some unique value for the image name and save it with that new name and also store the same in DB


    Nazia

    Thursday, July 04, 2013 1:05 PM
  • No i want to replace the exixting image.if i want the image in the image control then i will get back with the name.how it is possible.

    is there is any function to replace the existing image if the image name is same


    amair

    Friday, July 05, 2013 4:53 AM
  • No i want to replace the exixting image.if i want the image in the image control then i will get back with the name.how it is possible.

    is there is any function to replace the existing image if the image name is same


    amair

    Yes i got it

    private async void SelectImageButton_Click(object sender, RoutedEventArgs e)  
            {

                FileOpenPicker open = new FileOpenPicker();
                open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
                open.ViewMode = PickerViewMode.Thumbnail;
                open.FileTypeFilter.Clear();
                open.FileTypeFilter.Add(".bmp");
                open.FileTypeFilter.Add(".png");
                open.FileTypeFilter.Add(".jpeg");
                open.FileTypeFilter.Add(".jpg");
                StorageFile file = await open.PickSingleFileAsync();
                string s = ApplicationData.Current.LocalFolder.Path;
                StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                fileName = file.Name;
                bool result = await DoesFileExistAsync(fileName);
                if (result)
                {
                    StorageFile localFile =
                    await localFolder.GetFileAsync(fileName);
                    using (IRandomAccessStream fileStream = await localFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
                    {
                        BitmapImage bitmapImage = new BitmapImage();

                        await bitmapImage.SetSourceAsync(fileStream);
                        images.Source = bitmapImage;
                    }
                }
            else if (result==false)
                {
                    fileName = file.Name;
                    await file.CopyAsync(ApplicationData.Current.LocalFolder);
                    using (IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
                    {
                        BitmapImage bitmapImage = new BitmapImage();

                        await bitmapImage.SetSourceAsync(fileStream);
                        images.Source = bitmapImage;
                    }
               } 

              

            }

        static async Task<bool>  DoesFileExistAsync(string fileName)
            {
                try
                {
                    await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }


    amair

    Friday, July 05, 2013 6:10 AM