Convert local image to iText.Layout.Element image xamarin forms

mike 1 Reputation point
2021-04-29T15:45:07.98+00:00

I am having problems in Xamarin forms in android when converting an image that resides in the drawable repository of resources, but at the time of loading the image in a variable to later make the conversion to iText.Layout.Element.Image (which later this image it will go to a pdf using itext 7), they give me compatibility errors only for shitting the image in the variable, according to the documentation.

I suppose that later in the apk, when it is not executed in debug mode, these images will be available. There are some similar questions, but I have not seen any where the image starts as local

var image5 = new iText.Layout.Element.Image("logo.jpg");

using (MemoryStream memoryStream1 = new MemoryStream())
{
image5.Save(memoryStream1, ImageFormat.Png);
byte[] bytes = memoryStream1.ToArray();
}
To later process the image to incorporate it into the pdf document

ImageData imageData = ImageDataFactory.Create(bytes);

iText.Layout.Element.Image image = new iText.Layout.Element.Image(imageData);

document.Add(image);
But I get the Save() method error from System.Drawing and not how to fix it

The other code I have is:

var image20 = new Xamarin.Forms.Image { Source = "logo.PNG" };
var assembly = this.GetType().GetTypeInfo().Assembly;
byte[] imgByteArray = null;

using(var s = assembly.GetManifestResourceStream("logo.PNG"))
{
if (s != null)
{
var length = s.Length;
imgByteArray = new byte[length];
s.Read(imgByteArray, 0, (int)length);

           image20.Source = ImageSource.FromStream(() => s);
  }

}

ImageData imageData9 = ImageDataFactory.Create(imgByteArray);

iText.Layout.Element.Image image9 = new iText.Layout.Element.Image(imageData9);

Developer technologies .NET Xamarin
Developer technologies VB
Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Cole Xia (Shanghai Wicresoft Co,.Ltd.) 6,756 Reputation points
    2021-04-30T09:42:38.15+00:00

    Hello,

    Welcome to Microsoft Q&A!

    The correct path is Project name + folder name + file name .

    For example

    92829-capture.png

       var image20 = new Xamarin.Forms.Image();  
         
       Assembly assembly = this.GetType().GetTypeInfo().Assembly;  
       byte[] imgByteArray = null;  
         
       using (var s = assembly.GetManifestResourceStream("FormsA.dog2.png"))  
       {  
          if (s != null)  
          {  
            var length = s.Length;  
            imgByteArray = new byte[length];  
            s.Read(imgByteArray, 0, (int)length);  
         
            MemoryStream newM = new MemoryStream(buffer);  
            image20.Source = ImageSource.FromStream(() => newM);  
           }  
       }  
    

    Best Regards,
    Cole Xia


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.