Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: NullReferenceException @ Image.AsJpeg() / Document.Save() #5

Open
mknotzer opened this issue May 11, 2021 · 2 comments
Open

Comments

@mknotzer
Copy link

Saving a View with Images is not working on Android

myDocument.Save() -> exception
myImage.AsJpeg() -> exception
myImage.AsBitmap() -> works

PDFsharp 1.50.5147
PdfSharp.Xamarin.Forms 1.0.0

System.NullReferenceException: Object reference not set to an instance of an object.
at PdfSharp.Xamarin.Forms.Droid.AndroidImageSourceImpl.SaveAsJpeg (System.IO.MemoryStream ms) [0x00045] in E:\My Xamarin plugin\PdfSharp.Xamarin.Forms\PdfSharp.Xamarin.Forms\PdfSharp.Xamarin.Forms.Android\AndroidImageSourceImpl.cs:111
at PdfSharpCore.Drawing.XImage.AsJpeg () [0x00006] in <11413e5833e04920b85b1578e479c930>:0

@mknotzer
Copy link
Author

mknotzer commented May 11, 2021

Bug fixed:

	public AndroidImageSourceImpl(string name, Func<Stream> streamSource, int quality)
	{
		Name = name;
		_streamSource = streamSource;
		_quality = quality;
		using (var stream = streamSource.Invoke())
		{
			Orientation = Orientation.Normal;
			stream.Seek(0, SeekOrigin.Begin);

			var options = new Options { InJustDecodeBounds = true };
			using (DecodeStream(stream, null, options))
				Bitmap = BitmapFactory.DecodeStream(_streamSource.Invoke()); // bugfix
			Width = Orientation == Orientation.Normal || Orientation == Orientation.Rotate180 ? options.OutWidth : options.OutHeight;
			Height = Orientation == Orientation.Normal || Orientation == Orientation.Rotate180 ? options.OutHeight : options.OutWidth;
		}
	}

@mknotzer
Copy link
Author

@Roceh could you please take a look at this and release an update?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant