-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainPage.xaml.cs
64 lines (57 loc) · 2.62 KB
/
MainPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
namespace MauiDrawingViewSample;
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
private async void OnCounterClicked(object sender, EventArgs e)
{
using var stream = await DrawView.GetImageStream(1024, 1024);
using var memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
stream.Position = 0;
memoryStream.Position = 0;
#if WINDOWS
await System.IO.File.WriteAllBytesAsync(
@"C:\Users\joverslu\Desktop\DrawingView\Test.png", memoryStream.ToArray());
#elif ANDROID
var context = Platform.CurrentActivity;
if (OperatingSystem.IsAndroidVersionAtLeast(29))
{
Android.Content.ContentResolver resolver = context.ContentResolver;
Android.Content.ContentValues contentValues = new();
contentValues.Put(Android.Provider.MediaStore.IMediaColumns.DisplayName, "test.png");
contentValues.Put(Android.Provider.MediaStore.IMediaColumns.MimeType, "image/png");
contentValues.Put(Android.Provider.MediaStore.IMediaColumns.RelativePath, "DCIM/" + "test");
Android.Net.Uri imageUri = resolver.Insert(Android.Provider.MediaStore.Images.Media.ExternalContentUri, contentValues);
var os = resolver.OpenOutputStream(imageUri);
Android.Graphics.BitmapFactory.Options options = new();
options.InJustDecodeBounds = true;
var bitmap = Android.Graphics.BitmapFactory.DecodeStream(stream);
bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, os);
os.Flush();
os.Close();
}
else
{
Java.IO.File storagePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
string path = System.IO.Path.Combine(storagePath.ToString(), "test.png");
System.IO.File.WriteAllBytes(path, memoryStream.ToArray());
var mediaScanIntent = new Android.Content.Intent(Android.Content.Intent.ActionMediaScannerScanFile);
mediaScanIntent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(path)));
context.SendBroadcast(mediaScanIntent);
}
#elif IOS || MACCATALYST
var image = new UIKit.UIImage(Foundation.NSData.FromArray(memoryStream.ToArray()));
image.SaveToPhotosAlbum((image, error) =>
{
});
#endif
}
private async void DrawingView_DrawingLineCompleted(object sender, CommunityToolkit.Maui.Core.DrawingLineCompletedEventArgs e)
{
var stream = await DrawView.GetImageStream(200, 200);
imageView.Source = ImageSource.FromStream(() => stream);
}
}