Bug 13888 - Bitmap.CreateBitmap returns original image
Summary: Bitmap.CreateBitmap returns original image
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.8.x
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2013-08-09 11:34 UTC by Dave Wright
Modified: 2013-08-15 14:27 UTC (History)
1 user (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Dave Wright 2013-08-09 11:34:29 UTC
Hi there, not sure if this is a Xamarin issue or an Android issue but given that it's an issue with the C# keyword 'using' I thought I'd raise it here.  I have the following code:

using (Bitmap bmpScaled = BitmapFactory.DecodeFile(m_sPath)) {

	// Do various scaling/rotating operations using matrix
	(cut out as the details are irrelevant)

	// Return scaled/rotated bitmap
	return Bitmap.CreateBitmap (bmpScaled, 0, 0, bmpScaled.Width, bmpScaled.Height, matrix, true);

In 99% of cases this works fine, but if the various scaling/rotating options end up with an identity matrix, Bitmap.CreateBitmap doesn't seem to return a new bitmap at all but instead returns bmpScaled.  Normally this would be okay, except that at the end of the using bmpScaled becomes invalid.

I've managed to find a workaround, which is to change it to the following, but feel that Bitmap.CreateBitmap ought to create a bitmap no matter what the input parameters.

	(other code)
	if (matrix.IsIdentity)
		return BitmapFactory.DecodeFile(m_sPath);
	// Return scaled/rotated bitmap
	return Bitmap.CreateBitmap (bmpScaled, 0, 0, bmpScaled.Width, bmpScaled.Height, matrix, true);
Comment 1 Jonathan Pryor 2013-08-15 14:27:48 UTC
Our Binding mechanism aims for minimal semantic differences from the bound Java code, so if the Java code returns the same instance, the C# binding should as well. This allows for sane reading of Java-side documentation, examples, etc.

Consequently, we won't be changing Bitmap.CreateBitmap() to always return a new, unique, value.