public static Bitmap ToRounded(Bitmap source, float rad, double cropWidthRatio, double cropHeightRatio, double borderSize, string borderHexColor) { double sourceWidth = source.Width; double sourceHeight = source.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); else if (currentRatio < desiredRatio) desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); float cropX = (float)((sourceWidth - desiredWidth) / 2d); float cropY = (float)((sourceHeight - desiredHeight) / 2d); if (rad == 0) rad = (float)(Math.Min(desiredWidth, desiredHeight) / 2d); else rad = (float)(rad * (desiredWidth + desiredHeight) / 2d / 500d); Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, Bitmap.Config.Argb8888); using (Canvas canvas = new Canvas(bitmap)) using (Paint paint = new Paint()) using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp)) using (Matrix matrix = new Matrix()) { if (cropX != 0 || cropY != 0) { matrix.SetTranslate(-cropX, -cropY); shader.SetLocalMatrix(matrix); } paint.SetShader(shader); paint.AntiAlias = true; RectF rectF = new RectF(0f, 0f, (float)desiredWidth, (float)desiredHeight); canvas.DrawRoundRect(rectF, rad, rad, paint); if (borderSize > 0d) { borderSize = (borderSize * (desiredWidth + desiredHeight) / 2d / 500d); paint.Color = borderHexColor.ToColor(); ; paint.SetStyle(Paint.Style.Stroke); paint.StrokeWidth = (float)borderSize; paint.SetShader(null); RectF borderRectF = new RectF((float)(0d + borderSize/2d), (float)(0d + borderSize/2d), (float)(desiredWidth - borderSize/2d), (float)(desiredHeight - borderSize/2d)); canvas.DrawRoundRect(borderRectF, rad, rad, paint); } return bitmap; } }
public Bitmap Transform(Bitmap source) { int size = Math.Min(source.Width, source.Height); int width = (source.Width - size) / 2; int height = (source.Height - size) / 2; var bitmap = Bitmap.CreateBitmap(size, size, Bitmap.Config.Argb4444); var canvas = new Canvas(bitmap); var paint = new Paint(); var shader = new BitmapShader(source, BitmapShader.TileMode.Clamp, BitmapShader.TileMode.Clamp); if (width != 0 || height != 0) { // source isn't square, move viewport to center var matrix = new Matrix(); matrix.SetTranslate(-width, -height); shader.SetLocalMatrix(matrix); } paint.SetShader(shader); paint.AntiAlias = true; float r = size / 2f; canvas.DrawCircle(r, r, r, paint); source.Recycle(); return bitmap; }
public static Bitmap ToRounded(Bitmap source, float rad) { int size = Math.Min(source.Width, source.Height); int dx = (source.Width - size) / 2; int dy = (source.Height - size) / 2; Bitmap bitmap = Bitmap.CreateBitmap(size, size, Bitmap.Config.Argb8888); using (Canvas canvas = new Canvas(bitmap)) using (Paint paint = new Paint()) using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp)) using (Matrix matrix = new Matrix()) { if (dx != 0 || dy != 0) { // source isn't square, move viewport to centre matrix.SetTranslate(-dx, -dy); shader.SetLocalMatrix(matrix); } paint.SetShader(shader); paint.AntiAlias = true; RectF rectF = new RectF(0, 0, size, size); canvas.DrawRoundRect(rectF, rad, rad, paint); return bitmap; } }
public static Bitmap ToRounded(Bitmap source, float rad, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Width; double sourceHeight = source.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); else if (currentRatio < desiredRatio) desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); float cropX = (float)((sourceWidth - desiredWidth) / 2); float cropY = (float)((sourceHeight - desiredHeight) / 2); if (rad == 0) rad = (float)(Math.Min(desiredWidth, desiredHeight) / 2); Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, Bitmap.Config.Argb8888); using (Canvas canvas = new Canvas(bitmap)) using (Paint paint = new Paint()) using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp)) using (Matrix matrix = new Matrix()) { if (cropX != 0 || cropY != 0) { matrix.SetTranslate(-cropX, -cropY); shader.SetLocalMatrix(matrix); } paint.SetShader(shader); paint.AntiAlias = true; RectF rectF = new RectF(0, 0, (int)desiredWidth, (int)desiredHeight); canvas.DrawRoundRect(rectF, rad, rad, paint); return bitmap; } }
public RoundedDrawable(Bitmap bitmap) { _mBitmapWidth = bitmap.Width; _mBitmapHeight = bitmap.Height; _mBitmapRect.Set(0, 0, _mBitmapWidth, _mBitmapHeight); _mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.Clamp, Shader.TileMode.Clamp); _mBitmapShader.SetLocalMatrix(_mShaderMatrix); _mBitmapPaint = new Paint(); _mBitmapPaint.SetStyle(Paint.Style.Fill); _mBitmapPaint.AntiAlias = (true); _mBitmapPaint.SetShader(_mBitmapShader); _mBorderPaint = new Paint(); _mBorderPaint.SetStyle(Paint.Style.Stroke); _mBorderPaint.AntiAlias = (true); _mBorderPaint.Color = Color.Black; _mBorderPaint.StrokeWidth = (_mBorderWidth); }
public static Bitmap ToCropped(Bitmap source, double zoomFactor, double xOffset, double yOffset, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Width; double sourceHeight = source.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); else if (currentRatio < desiredRatio) desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); desiredWidth = zoomFactor * desiredWidth; desiredHeight = zoomFactor * desiredHeight; float cropX = (float)(((sourceWidth - desiredWidth) / 2) + xOffset); float cropY = (float)(((sourceHeight - desiredHeight) / 2) + yOffset); Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, Bitmap.Config.Argb8888); using (Canvas canvas = new Canvas(bitmap)) using (Paint paint = new Paint()) using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp)) using (Matrix matrix = new Matrix()) { matrix.SetTranslate(-cropX, -cropY); shader.SetLocalMatrix(matrix); paint.SetShader(shader); paint.AntiAlias = true; return bitmap; } }
public static Bitmap ToTransformedCorners(Bitmap source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Width; double sourceHeight = source.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) { desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); } else if (currentRatio < desiredRatio) { desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); } topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; float cropX = (float)((sourceWidth - desiredWidth) / 2); float cropY = (float)((sourceHeight - desiredHeight) / 2); Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, Bitmap.Config.Argb8888); using (Canvas canvas = new Canvas(bitmap)) using (Paint paint = new Paint()) using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp)) using (Matrix matrix = new Matrix()) using (var path = new Path()) { if (cropX != 0 || cropY != 0) { matrix.SetTranslate(-cropX, -cropY); shader.SetLocalMatrix(matrix); } paint.SetShader(shader); paint.AntiAlias = true; // TopLeft if (cornersTransformType.HasFlag(CornerTransformType.TopLeftCut)) { path.MoveTo(0, (float)topLeftCornerSize); path.LineTo((float)topLeftCornerSize, 0); } else if (cornersTransformType.HasFlag(CornerTransformType.TopLeftRounded)) { path.MoveTo(0, (float)topLeftCornerSize); path.QuadTo(0, 0, (float)topLeftCornerSize, 0); } else { path.MoveTo(0, 0); } // TopRight if (cornersTransformType.HasFlag(CornerTransformType.TopRightCut)) { path.LineTo((float)(desiredWidth - topRightCornerSize), 0); path.LineTo((float)desiredWidth, (float)topRightCornerSize); } else if (cornersTransformType.HasFlag(CornerTransformType.TopRightRounded)) { path.LineTo((float)(desiredWidth - topRightCornerSize), 0); path.QuadTo((float)desiredWidth, 0, (float)desiredWidth, (float)topRightCornerSize); } else { path.LineTo((float)desiredWidth ,0); } // BottomRight if (cornersTransformType.HasFlag(CornerTransformType.BottomRightCut)) { path.LineTo((float)desiredWidth, (float)(desiredHeight - bottomRightCornerSize)); path.LineTo((float)(desiredWidth - bottomRightCornerSize), (float)desiredHeight); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomRightRounded)) { path.LineTo((float)desiredWidth, (float)(desiredHeight - bottomRightCornerSize)); path.QuadTo((float)desiredWidth, (float)desiredHeight, (float)(desiredWidth - bottomRightCornerSize), (float)desiredHeight); } else { path.LineTo((float)desiredWidth, (float)desiredHeight); } // BottomLeft if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftCut)) { path.LineTo((float)bottomLeftCornerSize, (float)desiredHeight); path.LineTo(0, (float)(desiredHeight - bottomLeftCornerSize)); } else if (cornersTransformType.HasFlag(CornerTransformType.BottomLeftRounded)) { path.LineTo((float)bottomLeftCornerSize, (float)desiredHeight); path.QuadTo(0, (float)desiredHeight, 0, (float)(desiredHeight - bottomLeftCornerSize)); } else { path.LineTo(0, (float)desiredHeight); } path.Close(); canvas.DrawPath(path, paint); return bitmap; } }
public static Bitmap ToCropped(Bitmap source, double zoomFactor, double xOffset, double yOffset, double cropWidthRatio, double cropHeightRatio) { double sourceWidth = source.Width; double sourceHeight = source.Height; double desiredWidth = sourceWidth; double desiredHeight = sourceHeight; double desiredRatio = cropWidthRatio / cropHeightRatio; double currentRatio = sourceWidth / sourceHeight; if (currentRatio > desiredRatio) desiredWidth = (cropWidthRatio * sourceHeight / cropHeightRatio); else if (currentRatio < desiredRatio) desiredHeight = (cropHeightRatio * sourceWidth / cropWidthRatio); xOffset = xOffset * desiredWidth; yOffset = yOffset * desiredHeight; desiredWidth = desiredWidth / zoomFactor; desiredHeight = desiredHeight / zoomFactor; float cropX = (float)(((sourceWidth - desiredWidth) / 2) + xOffset); float cropY = (float)(((sourceHeight - desiredHeight) / 2) + yOffset); if (cropX < 0) cropX = 0; if (cropY < 0) cropY = 0; if (cropX + desiredWidth > sourceWidth) cropX = (float)(sourceWidth - desiredWidth); if (cropY + desiredHeight > sourceHeight) cropY = (float)(sourceHeight - desiredHeight); Bitmap bitmap = Bitmap.CreateBitmap((int)desiredWidth, (int)desiredHeight, source.GetConfig()); using (Canvas canvas = new Canvas(bitmap)) using (Paint paint = new Paint()) using (BitmapShader shader = new BitmapShader(source, Shader.TileMode.Clamp, Shader.TileMode.Clamp)) using (Matrix matrix = new Matrix()) { if (cropX != 0 || cropY != 0) { matrix.SetTranslate(-cropX, -cropY); shader.SetLocalMatrix(matrix); } paint.SetShader(shader); paint.AntiAlias = false; RectF rectF = new RectF(0, 0, (int)desiredWidth, (int)desiredHeight); canvas.DrawRect(rectF, paint); return bitmap; } }