Issue
I make a screenshot on page and I want to share it to social media. I do not know how to share image to the social media. I know only how to share text, but I need to share image.Please help me to share screenshot from program
<ContentPage.Content>
<StackLayout HeightRequest="30" WidthRequest="30">
<StackLayout x:Name="header">
<Frame
Padding="3"
BackgroundColor="White"
BorderColor="#DBDBDB"
CornerRadius="15"
HasShadow="False">
<StackLayout Orientation="Horizontal">
<StackLayout
Margin="5,0,0,0"
HorizontalOptions="Start"
Orientation="Horizontal">
<StackLayout VerticalOptions="Center">
<Label
FontSize="16"
HorizontalOptions="Start"
Text="USD"
TextColor="Black" />
<Label
FontSize="10"
HorizontalOptions="Start"
Text="US Dollar"
TextColor="Black" />
</StackLayout>
</StackLayout>
</StackLayout>
</Frame>
</StackLayout>
<Button x:Name="ShareButton" Text="Share" />
<Image x:Name="result" />
</StackLayout>
</ContentPage.Content>
public async void Button_Clicked(Object o,EventArgs e)
{
var image =await header.CaptureImageAsync();
result.Source = ImageSource.FromStream(() => image);
}
It is code how I make screenshot.Please help me to share image from program to social media
Solution
You can use Xamarin.Essentials for all that.
For example, if you want to share a screenshot you can save the screenshot to a file (demo code):
async Task<string> CaptureScreenshotAsync()
{
var screenshot = await Xamarin.Essentials.Screenshot.CaptureAsync();
var stream = await screenshot.OpenReadAsync();
var file = Path.Combine(FileSystem.CacheDirectory, "screenshot.png");
using (FileStream fs = File.Open(file, FileMode.CreateNew))
{
await stream.CopyToAsync(fs);
await fs.FlushAsync();
}
return file;
}
and then share this file (demo code):
async Task ShareFile(string filename, string filepath)
{
await Share.RequestAsync(new ShareFileRequest()
{
Title = filename,
File = new ShareFile(filepath)
});
}
Note: You should make sure the file extension is correct. I experienced some problems sharing or opening files with no or wrong file extensions.
Answered By - AlexS
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.