Skip to content

Instantly share code, notes, and snippets.

@dimonovdd
Created February 14, 2021 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimonovdd/fddef6f5ba20d873a3b6b104318aca34 to your computer and use it in GitHub Desktop.
Save dimonovdd/fddef6f5ba20d873a3b6b104318aca34 to your computer and use it in GitHub Desktop.
ImageFromScrollView
<Grid RowDefinitions="300,10,50,10,50" RowSpacing="0" Margin="0" Padding="0">
<ScrollView x:Name="scroll"
Margin="0" Padding="0">
<StackLayout x:Name="stack"
Margin="0" Padding="0" Spacing="0">
<BoxView HeightRequest="200" Background="Violet"/>
<BoxView HeightRequest="200" Background="Green"/>
<BoxView HeightRequest="200" Background="Blue"/>
</StackLayout>
</ScrollView>
<Button Grid.Row="2" Text="Capture ScrollView" Background="#990000ff"
Command="{Binding CaptureCommand}" CommandParameter="{x:Reference scroll}"/>
<Button Grid.Row="4" Text="Capture StackLayout" Background="#990000ff"
Command="{Binding CaptureCommand}" CommandParameter="{x:Reference stack}"/>
</Grid>
public class MainViewModel : BaseNotifier
{
public MainViewModel()
=> CaptureCommand = new Command<VisualElement>(OnCapture);
public ICommand CaptureCommand { get; set; }
async void OnCapture(VisualElement element)
{
try
{
using var stream = await element.CaptureImageAsync();
var dir = FileSystem.CacheDirectory;
var filepath = Path.Combine(dir, "image.jpg");
if (File.Exists(filepath))
File.Delete(filepath);
using var fileStream = File.OpenWrite(filepath);
stream.CopyTo(fileStream);
fileStream.Close();
await Share.RequestAsync(new ShareFileRequest(new ShareFile(filepath)));
}
catch (Exception ex)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment