Reputation: 177
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?
Upvotes: 9
Views: 14796
Reputation: 1
To take a picture and store it with date and time on it, I used a combination of:
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);
}
}
Upvotes: 0
Reputation: 1142
You can also check out Paul Betts ' Splat library for cross platform image loading/saving.
https://github.com/paulcbetts/splat
Upvotes: 3
Reputation: 2308
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);
}
Upvotes: 22