【问题标题】:Syncfusion PDF : Adding image to pdf works in iOS but not in androidSyncfusion PDF:将图像添加到 pdf 在 iOS 中有效,但在 android 中无效
【发布时间】:2019-05-14 21:04:17
【问题描述】:

我正在使用 Syncfusion 和 Xamarin Forms 动态创建 PDF。在 iOS 中一切正常,但在 Android 中,它无法找到图像(徽标)。

跟随 syncfusion PDF 开始,但即使它不起作用。图片位于 android 的资源/drawable 中。我确实也在资产中添加了它。

这是 xamarin 表单 iOS 中的工作代码

//Load the image 
Stream imageStream = System.IO.File.OpenRead("logo.jpg");
//Load the image from the stream 
PdfBitmap image = new PdfBitmap(imageStream); 

不幸的是,即使这在 Android 上也不起作用

//Load the image 
Stream imageStream = DependencyService.Get<ISave>().LoadFromFile(@"logo.jpg");
//Load the image from the stream 
PdfBitmap image = new PdfBitmap(imageStream); 

感谢您的帮助,因为我已经坚持了 3 天了。

【问题讨论】:

  • 最好在syncfusion网站上问这个问题,如果它是一个错误,他们往往会很快回答
  • 你把你的图片作为“嵌入式资源”了吗?只是在黑暗中拍摄,但我在使用 pdf 文件时遇到了类似的问题,我不知道不嵌入 ios 是否仍然有效,因为我只尝试过使用 android
  • 另一种可能是:你在android上实现了ISave接口吗?因为你没有在这里提到我假设它没有实现
  • @Ricardo Dias Morais,不幸的是,我都做了。

标签: c# xamarin.forms syncfusion


【解决方案1】:

在进一步分析 Xamarin Android 中的“从 drawable 文件夹加载图像”时,我们发现无法直接从 drawable 文件夹加载图像。而不是我们可以从资源中获取 if 。详情请看以下代码sn-p,

Xamarin.Android:

var context = Android.App.Application.Context;
using (var drawable = 
Xamarin.Forms.Platform.Android.ResourceManager.GetDrawable(context, fileName))
using (var bitmap = ((BitmapDrawable)drawable).Bitmap)
{
var stream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
bitmap.Recycle();
return stream.ToArray();
}

我们已经创建了相同的示例,可以从下面的链接下载,

Sample for loading images from resource

【讨论】:

  • 这个对我有用。虽然它仅适用于 Android。谢谢你。你为我节省了大量时间。
【解决方案2】:

我刚刚关注了the syncfusion PDF getting started,我认为你在错误的地方添加了图片。图片应保存在 Xamarin.Forms 项目 中,可以使用以下代码 sn-p 访问:

 Stream imageStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("GettingStarted.Assets.logo.jpg");

GettingStarted.Assets.logo.jpg

GettingStarted 表示您的项目名称。

Assets 表示这片大鱼里面的图片。

logo.jpg是图片的名字。

确保将您的图片设置为“嵌入资源”和“始终复制”

我在这里加了一个截图来说明清楚:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 2020-04-03
    • 1970-01-01
    • 2012-01-19
    相关资源
    最近更新 更多