如何将Stacklayout中的内容转换为Xamarin格式的Pdf或png格式



我想将StackLayout中的内容转换为。png或。pdf格式,并需要从我的iOS设备打印相同的内容。我的打印机只支持图像和pdf格式。我如何在xamarin形式中实现这一点。请帮帮我。这是我的Xaml。

<StackLayout HorizontalOptions="FillAndExpand" Padding="20" VerticalOptions="CenterAndExpand">
<Frame HasShadow="False" BorderColor="LightGray"
CornerRadius="0" VerticalOptions="Center"
HorizontalOptions="Center">
<StackLayout HorizontalOptions="Center" Spacing="30">
<Label HorizontalTextAlignment="Center">
<Label.FormattedText>
<FormattedString>
<Span Text="ID : " FontAttributes="Bold"
TextColor="Black"/>
<Span Text="1234" TextColor="Green" FontAttributes="Bold"/>
</FormattedString>
</Label.FormattedText>
</Label>
<Frame CornerRadius="1" BorderColor="Green"
HasShadow="False" WidthRequest="40" HorizontalOptions="Center">
<Image Source="ProfileImg" HeightRequest="70"
WidthRequest="50"/>
</Frame>
<Label Text="Xamarin" HorizontalTextAlignment="Center"
FontSize="40" TextColor="Black"
FontAttributes="Bold"/>

<Label HorizontalTextAlignment="Center">
<Label.FormattedText>
<FormattedString>
<Span Text="Phone No : " TextColor="Gray" FontAttributes="Bold"/>
<Span Text="1234567890" TextColor="Green" FontAttributes="Bold"/>
</FormattedString>
</Label.FormattedText>
</Label>
</StackLayout>
</Frame>
</StackLayout>

你当然可以这么做。你可以使用ImageFromXamarinUINuget。让我给你一个例子:

在xaml文件中,为想要捕获的框架设置一个名称。我们把它命名为"myfame":

<Frame x:Name="myframe"    HasShadow="False" BorderColor="LightGray"
CornerRadius="0" VerticalOptions="Center"
HorizontalOptions="Center">
在.cs文件中,首先使用新的Nuget:
using ImageFromXamarinUI;

然后使用以下代码:

async void TapGestureRecognizer_Tapped(System.Object sender, System.EventArgs e)
{
var imgStream = await myframe.CaptureImageAsync(); // take the content of myframe as a screenshot

string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "mypic.png");
using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
imgStream.CopyTo(fileStream);
}
}

在iOS中,你可以通过Files App找到png。

你也应该设置LSSupportsOpeningDocumentsInPlace应用程序支持iTunes共享的键是.

通过这种方式,您可以捕获任何可视元素来截图并将其保存到png文件中。

=================== 起源的帖子 ===============

正如@Jason所说,你可以使用Xamarin。要点:截图。试试下面的代码:

async void TapGestureRecognizer_Tapped(System.Object sender, System.EventArgs e)  // i save the png when tapping the frame
{
var screenshot = await Screenshot.CaptureAsync();
var stream = await screenshot.OpenReadAsync();
string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "mypic.png");
using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
stream.CopyTo(fileStream);
} 
}

在iOS中,你可以通过Files App找到png。

你也应该设置LSSupportsOpeningDocumentsInPlace应用程序支持iTunes共享的键是.

有关更多信息,您可以参考Xamarin。要点:Xamarin.iOS中的截图和文件系统访问

希望它对你有用。

我使用依赖服务实现了这一点。

在iOS中添加了以下代码,并将byte[]从contentpage.cs转换为imagesce。

public byte[] LayoutToImage()
{
var view = UIApplication.SharedApplication.KeyWindow;
UIGraphics.BeginImageContext(view.Frame.Size);
view.DrawViewHierarchy(view.Frame, true);
var image = UIGraphics.GetImageFromCurrentImageContext();
UIGraphics.EndImageContext();
using (var imageData = image.AsPNG())
{
var bytes = new byte[imageData.Length];
System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, bytes, 0, Convert.ToInt32(imageData.Length));
return bytes;
}

最新更新