PCL Save Image File to local File System
Asked Answered
H

3

9

I am looking for a way to store image files in my local filesystem using the PCL Storage plugin in my core Project for Windows Phone, Xamarin.Android and Xamarin.iOS. However, the plugin does just provide methods for writing Text. Nothing about bytes. Is there a way to save byte arrays?

Holozoic answered 19/3, 2014 at 7:47 Comment(0)
P
22

How about something like this:

IFile file = await FileSystem.Current.GetFileFromPathAsync(fs.filepath);
byte[] buffer = new byte[100];
using (System.IO.Stream stream = await file.OpenAsync(FileAccess.ReadAndWrite))
{
    stream.Write(buffer, 0, 100);
}

Edit: Some code taken from http://pclstorage.codeplex.com/

IFile file = await folder.CreateFileAsync("myfile.abc", CreationCollisionOption.ReplaceExisting);

Once you've got the IFile object you should then be able to use this in the same way:

IFile file = await folder.CreateFileAsync("myfile.abc", CreationCollisionOption.ReplaceExisting);
byte[] buffer = new byte[100];
using (System.IO.Stream stream = await file.OpenAsync(FileAccess.ReadAndWrite))
{
    stream.Write(buffer, 0, 100);
}
Percipient answered 19/3, 2014 at 16:27 Comment(3)
This will be quite useful later for opening the file. Right now i'm looking for a way to actually write it.Holozoic
I tried your solution and it is creating files with same byte size, I have given byte size byte[3500], so all images are creating with 3500 byte size while images size varies.Tollefson
@Ashish Jain where 100 is in stream.write(), replace with buffer.length.Embassy
O
3

You can also check out Paul Betts ' Splat library for cross platform image loading/saving.

https://github.com/paulcbetts/splat

Oestrogen answered 20/3, 2014 at 14:34 Comment(1)
Looks nice, but didn't find the way to save images cross-platform. Have you guys?Pallid
R
0

To take a picture and store it with date and time on it, I used a combination of:

  1. Plugin.Media
  2. Plugin.Media.Abstractions
  3. PCLStorage
  4. SkiaSharp

I found this Using SkiaSharp, how to save a SKBitmap ? and used it as a base for this:

// I get my bitmap from the camera (Plugin.Media allows to pick it from filesystem too)
var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
    Directory = "MyFolder",
    Name = "MyPic001",
    PhotoSize = PhotoSize.Medium,
    CompressionQuality = 40,
    SaveToAlbum = true
});
// If succesfully taken...
if (file != null)
{
    // Start using SkiaSharp to write text on it
    var bitmap = SKBitmap.Decode(file.Path);
    var canvas = new SKCanvas(bitmap);
    var font = SKTypeface.FromFamilyName("Arial");
    var brush = new SKPaint
    {
        Typeface = font,
        TextSize = Convert.ToInt64(40),
        IsAntialias = true,
        Color = new SKColor(255, 255, 255, 255)
    };
    // Write on top of the image
    canvas.DrawText(DateTime.Now.ToString("dd/MM/yyyy HH:mm"), 10, 60, brush);
    var imageSK = SKImage.FromBitmap(bitmap);
    // Get rootFolder and FileName from Plugin.Media's file.Path
    string A = file.Path;
    string P = A.Substring(0, A.LastIndexOf("/"));
    string F = A.Substring(A.LastIndexOf("/") + 1, A.Length - (A.LastIndexOf("/") + 1));
    // Start using PCLStorage
    IFolder rootFolder = await FileSystem.Current.GetFolderFromPathAsync(P);
    IFile myFile = await rootFolder.GetFileAsync(F);
    // Use PCLStorage file opening to create an IO.Stream
    using (Stream s = await myFile.OpenAsync(FileAccess.ReadAndWrite))
    {
        // Use SkiaSharp SKImage and SKData to Save the image+text on file.Path
        SKData d = imageSK.Encode(SKEncodedImageFormat.Jpeg, 40);
        d.SaveTo(s);
    }
}
Rhizogenic answered 17/10, 2017 at 21:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.