Download Code

Xamarin Image Effects Example Android

Image Editting is a trend nowadays because people like their photos in attractive looks.In this xamarin image effects example android tutorial you will learn how to edit/apply image effects in android application.You will deal with Bitmap Image and it’s concerning methods in this tutorial.

So,let me give you the steps you will perform in this tutorial.

  • Pick Image from gallery.
  • Open edit options from menu.
  • Apply effect on that image.

Create new xamarin image effects example android application project.Go to Main.axml and add ImageView,Button.

Main.axml

xamarin image effects,xamarin image effects example,xamarin image effects example android,image effects example android,

Create new folder under Resources directory Menu.Within Menu folder create an xml file main.xml.

main.xml

xamarin image effects,xamarin image effects example,xamarin image effects example android,image effects example android,

Go to MainActivity.cs and do the following steps.

  • Create UI Component variables and Refer their id’s.
  • Apply click event to open gallery using ActionPick intent constant from MediaStore.
  • Apply effects on image by converting/Manipulating/Bitmap Image.

MainActivity.cs

namespace XamarinImageEditor
{
	[Activity(Label = "XamarinImageEditor", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity, Android.Views.View.IOnClickListener
	{
		Button pickImageBtn;
		ImageView pickedImage;
		Bitmap bitImage;
		Bitmap bitImg;
		Bitmap operations;

		protected override void OnCreate(Bundle savedInstanceState)
		{
			base.OnCreate(savedInstanceState);

			// Set our view from the "main" layout resource
			SetContentView(Resource.Layout.Main);
			initialize();
		}
		public void initialize()
		{
			pickedImage = (ImageView)FindViewById(Resource.Id.image);
			pickImageBtn = (Button)FindViewById(Resource.Id.pickImage);
			pickImageBtn.SetOnClickListener(this);

		}

		public void OnClick(View v)
		{
			switch (v.Id)
			{
				case Resource.Id.pickImage:
					Intent p = new Intent(Intent.ActionPick, Android.Provider.MediaStore.Images.Media.ExternalContentUri);
					StartActivityForResult(p, 1);
					break;
			}
		}

		protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
		{
			// TODO Auto-generated method stub

			if (requestCode == 1 && resultCode == Result.Ok && data != null) {
				Android.Net.Uri selectedImage = data.Data;
				String[] filePathColumn = { MediaStore.Images.Media.InterfaceConsts.Data };

				ICursor cursor = ContentResolver.Query(selectedImage,
						filePathColumn, null, null, null);
				cursor.MoveToFirst();

				int columnIndex = cursor.GetColumnIndex(filePathColumn[0]);
				String picturePath = cursor.GetString(columnIndex);
				cursor.Close();
				pickedImage.SetImageBitmap(BitmapFactory.DecodeFile(picturePath));

			}

		}
		public override Boolean OnCreateOptionsMenu(Android.Views.IMenu menu)
		{
			// Inflate the menu; this adds items to the action bar if it is present.
			MenuInflater.Inflate(Resource.Menu.main, menu);
			return true;
		}

		public override Boolean OnOptionsItemSelected(IMenuItem item)
		{
			int id = item.ItemId;
			switch (id)
			{
				case Resource.Id.Colored_Negative:
					BitmapDrawable bd = (BitmapDrawable)pickedImage.Drawable;    // get the image for customization
					bitImg = bd.Bitmap;    // rendering bitmap
												// create new bitmap
					operations = Bitmap.CreateBitmap(bitImg.Width, bitImg.Height, bitImg.GetConfig());
					// loops to get individual pixel and change it's color
					for (int i = 0; i < bitImg.Width; i++) {
						for (int j = 0; j < bitImg.Height; j++) {
							int p = bitImg.GetPixel(i, j);
							var col = new Color(p);
							int r = col.R;
							int g = col.G;
							int b = col.B;

							//subtracting value from pixel to change color
							r = r - 70;
							g = g - 60;
							b = b - 50;

							// now setting pixel into a bitmap
							operations.SetPixel(i, j, Color.Argb(40, r, g, b));  //applying image effects
						}
					}
					pickedImage.SetImageBitmap(operations); // setting bitmap Image
					break;
				case Resource.Id.negative:
					BitmapDrawable bD = (BitmapDrawable)pickedImage.Drawable;
					bitImg = bD.Bitmap;
					operations = Bitmap.CreateBitmap(bitImg.Width, bitImg.Height, bitImg.GetConfig());

					for (int i = 0; i < bitImg.Width; i++) {
						for (int j = 0; j < bitImg.Height; j++) {
							int p = bitImg.GetPixel(i, j);
							var col = new Color(p);
							int r = col.R;
							int g = col.G;
							int b = col.B;

							r = r - 70;
							g = g - 70;
							b = b - 70;

							operations.SetPixel(i, j, Color.Argb(40, r, g, b));  //applying image effects
						}
					}
					pickedImage.SetImageBitmap(operations);
					break;
			}
			return base.OnOptionsItemSelected(item);
		}
	}
}

Add WRITE_EXTERNAL_STROAGE permission to Android Manifest.xml

Run your xamarin image effects example android application project.I have taken a random picture.

xamarin image effects,xamarin image effects example,xamarin image effects example android,image effects example android,

That’s it for this tutorial.

For Java example visit Android Codec.

Thanks.