public static Bitmap ToGrayscale(Bitmap source) { using (var colorMatrix = new ColorMatrix()) { colorMatrix.SetSaturation(0f); return ColorSpaceTransformation.ToColorSpace(source, colorMatrix); } }
public static Bitmap ToSepia(Bitmap source) { using (ColorMatrix saturation = new ColorMatrix()) using (ColorMatrix rgbFilter = new ColorMatrix()) { saturation.SetSaturation(0f); rgbFilter.SetScale(1.0f, 0.95f, 0.82f, 1.0f); saturation.SetConcat(rgbFilter, saturation); return ColorSpaceTransformation.ToColorSpace(source, saturation); } }
public static Bitmap ToGreyScale(this Bitmap bmp) { if (bmp == null) return null; var height = bmp.Height; var width = bmp.Width; var bmpGrayscale = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888); var canvas = new Canvas(bmpGrayscale); var paint = new Paint(); var cm = new ColorMatrix(); cm.SetSaturation(0); var filter = new ColorMatrixColorFilter(cm); paint.SetColorFilter(filter); canvas.DrawBitmap(bmp, 0, 0, paint); return bmpGrayscale; }
protected override Bitmap Transform(Bitmap source) { int width = source.Width; int height = source.Height; Bitmap bitmap = Bitmap.CreateBitmap(width, height, Bitmap.Config.Argb8888); Canvas canvas = new Canvas(bitmap); ColorMatrix saturation = new ColorMatrix(); saturation.SetSaturation(0f); Paint paint = new Paint(); paint.SetColorFilter(new ColorMatrixColorFilter(saturation)); canvas.DrawBitmap(source, 0, 0, paint); source.Recycle(); return bitmap; }
protected override void OnElementChanged(ElementChangedEventArgs<Image> e) { base.OnElementChanged(e); if (Element == null) { return; } var matrixColor = new ColorMatrix(); filterColor = new ColorMatrixColorFilter(matrixColor); var matrixGray = new ColorMatrix(); matrixGray.SetSaturation(0); filterGray = new ColorMatrixColorFilter(matrixGray); }
public Bitmap Transform(Bitmap source) { Bitmap result = Bitmap.CreateBitmap(source.Width, source.Height, source.GetConfig()); Bitmap noise; try { noise = picasso.Load(Resource.Drawable.noise).Get(); } catch (Exception) { throw new Exception("Failed to apply transformation! Missing resource."); } BitmapShader shader = new BitmapShader(noise, Shader.TileMode.Repeat, Shader.TileMode.Repeat); ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.SetSaturation(0); ColorMatrixColorFilter filter = new ColorMatrixColorFilter(colorMatrix); Paint paint = new Paint(PaintFlags.AntiAlias); paint.SetColorFilter(filter); Canvas canvas = new Canvas(result); canvas.DrawBitmap(source, 0, 0, paint); paint.SetColorFilter(null); paint.SetShader(shader); paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.Multiply)); canvas.DrawRect(0, 0, canvas.Width, canvas.Height, paint); source.Recycle(); noise.Recycle(); return result; }
private ImageView GetBrushImageWithColor(ImageView brushImage, Android.Graphics.Color color) { ImageView toReturn = new ImageView (context); using (Bitmap sourceBitmap = brushImage.DrawingCache) { if (sourceBitmap != null) { float R = (float)color.R; float G = (float)color.G; float B = (float)color.B; float[] colorTransform = { R / 255f ,0 ,0 ,0, 0, // R color 0, G / 255f , 0 ,0 ,0 // G color ,0 ,0 , B / 255f, 0, 0 // B color ,0 ,0 ,0 ,1f ,0f }; ColorMatrix colorMatrix = new ColorMatrix (); colorMatrix.SetSaturation (0f); //Remove Colour colorMatrix.Set (colorTransform); ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter (colorMatrix); Paint paint = new Paint (); paint.SetColorFilter (colorFilter); Bitmap mutableBitmap = sourceBitmap.Copy (Bitmap.Config.Argb8888, true); toReturn.SetImageBitmap (mutableBitmap); Canvas canvas = new Canvas (mutableBitmap); canvas.DrawBitmap (mutableBitmap, 0, 0, paint); } } return toReturn; }
public static Bitmap changeBitmapContrastBrightness (Bitmap bmp, float contrast, float brightness) { ColorMatrix cm = new ColorMatrix (new float[] { contrast, 0, 0, 0, brightness, 0, contrast, 0, 0, brightness, 0, 0, contrast, 0, brightness, 0, 0, 0, 1, 0 }); cm.SetSaturation (0); Bitmap ret = Bitmap.CreateBitmap (bmp.Width, bmp.Height, bmp.GetConfig ()); Canvas canvas = new Canvas (ret); Paint paint = new Paint (); paint.SetColorFilter (new ColorMatrixColorFilter (cm)); canvas.DrawBitmap (bmp, 0, 0, paint); return ret; }