繁体   English   中英

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

[英]How to convert the contents in the Stacklayout to Pdf or png form in Xamarin forms

我想将 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>

你当然可以那样做。 您可以使用ImageFromXamarinUI Nuget 来实现它。 让我举一个例子:

在 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 中,您可以通过文件应用程序找到 png。

您还应该将LSSupportsOpeningDocumentsInPlace键和Application supports iTunes sharing key 设置为Yes

通过这种方式,您可以捕获任何视觉元素进行屏幕截图并将其保存为 png 文件。

===================原帖===============

正如@Jason 所说,您可以使用Xamarin.Essentials: Screenshot 试试下面的代码:

    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 中,您可以通过文件应用程序找到 png。

您还应该将LSSupportsOpeningDocumentsInPlace键和Application supports iTunes sharing key 设置为Yes

有关详细信息,您可以参考Xamarin.Essentials:Xamarin.iOS 中的屏幕截图和文件系统访问

希望对你有效。

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

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

 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;
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM