public static BitmapHolder ToRounded(BitmapHolder source, int 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); } double cropX = ((sourceWidth - desiredWidth) / 2); double cropY = ((sourceHeight - desiredHeight) / 2); BitmapHolder bitmap = null; if (cropX != 0 || cropY != 0) { bitmap = CropTransformation.ToCropped(source, (int)cropX, (int)cropY, (int)(desiredWidth), (int)(desiredHeight)); } else { bitmap = new BitmapHolder(source.PixelData, source.Width, source.Height); } if (rad == 0) { rad = (int)(Math.Min(desiredWidth, desiredHeight) / 2); } else { rad = (int)(rad * (desiredWidth + desiredHeight) / 2 / 500); } int w = (int)desiredWidth; int h = (int)desiredHeight; var transparentColor = ColorHolder.Transparent; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (x <= rad && y <= rad) { //top left corner if (!CheckRoundedCorner(rad, rad, rad, Corner.TopLeftCorner, x, y)) { bitmap.SetPixel(y * w + x, transparentColor); } } else if (x >= w - rad && y <= rad) { // top right corner if (!CheckRoundedCorner(w - rad, rad, rad, Corner.TopRightCorner, x, y)) { bitmap.SetPixel(y * w + x, transparentColor); } } else if (x >= w - rad && y >= h - rad) { // bottom right corner if (!CheckRoundedCorner(w - rad, h - rad, rad, Corner.BottomRightCorner, x, y)) { bitmap.SetPixel(y * w + x, transparentColor); } } else if (x <= rad && y >= h - rad) { // bottom left corner if (!CheckRoundedCorner(rad, h - rad, rad, Corner.BottomLeftCorner, x, y)) { bitmap.SetPixel(y * w + x, transparentColor); } } } } //TODO draws a border - we should optimize that and add some anti-aliasing if (borderSize > 0d) { borderSize = (borderSize * (desiredWidth + desiredHeight) / 2d / 500d); var borderColor = ColorHolder.Transparent; try { borderColor = borderHexColor.ToColorFromHex(); } catch (Exception) { } int intBorderSize = (int)Math.Ceiling(borderSize); for (int i = 2; i < intBorderSize; i++) { CircleAA(bitmap, i, borderColor); } } return(bitmap); }
public static BitmapHolder ToTransformedCorners(BitmapHolder source, double topLeftCornerSize, double topRightCornerSize, double bottomLeftCornerSize, double bottomRightCornerSize, CornerTransformType cornersTransformType, double cropWidthRatio, double cropHeightRatio) { double num = source.Width; double num2 = source.Height; double num3 = num; double num4 = num2; double num5 = cropWidthRatio / cropHeightRatio; double num6 = num / num2; if (num6 > num5) { num3 = cropWidthRatio * num2 / cropHeightRatio; } else if (num6 < num5) { num4 = cropHeightRatio * num / cropWidthRatio; } double num7 = (num - num3) / 2.0; double num8 = (num2 - num4) / 2.0; BitmapHolder bitmapHolder = null; bitmapHolder = ((num7 == 0.0 && num8 == 0.0) ? new BitmapHolder(source.PixelData, source.Width, source.Height) : CropTransformation.ToCropped(source, (int)num7, (int)num8, (int)num3, (int)num4)); topLeftCornerSize = topLeftCornerSize * (num3 + num4) / 2.0 / 100.0; topRightCornerSize = topRightCornerSize * (num3 + num4) / 2.0 / 100.0; bottomLeftCornerSize = bottomLeftCornerSize * (num3 + num4) / 2.0 / 100.0; bottomRightCornerSize = bottomRightCornerSize * (num3 + num4) / 2.0 / 100.0; int num9 = (int)topLeftCornerSize; int num10 = (int)topRightCornerSize; int num11 = (int)bottomLeftCornerSize; int num12 = (int)bottomRightCornerSize; int width = bitmapHolder.Width; int height = bitmapHolder.Height; ColorHolder transparent = ColorHolder.Transparent; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j <= num9 && i <= num9) { if (!CheckCorner(num9, num9, num9, cornersTransformType, Corner.TopLeftCorner, j, i)) { bitmapHolder.SetPixel(i * width + j, transparent); } } else if (j >= width - num10 && i <= num10 && num10 > 0) { if (!CheckCorner(width - num10, num10, num10, cornersTransformType, Corner.TopRightCorner, j, i)) { bitmapHolder.SetPixel(i * width + j, transparent); } } else if (j >= width - num12 && i >= height - num12 && num12 > 0) { if (!CheckCorner(width - num12, height - num12, num12, cornersTransformType, Corner.BottomRightCorner, j, i)) { bitmapHolder.SetPixel(i * width + j, transparent); } } else if (j <= num11 && i >= height - num11 && num11 > 0 && !CheckCorner(num11, height - num11, num11, cornersTransformType, Corner.BottomLeftCorner, j, i)) { bitmapHolder.SetPixel(i * width + j, transparent); } } } return(bitmapHolder); }
public static BitmapHolder ToRounded(BitmapHolder source, int rad, double cropWidthRatio, double cropHeightRatio, double borderSize, string borderHexColor) { double num = source.Width; double num2 = source.Height; double num3 = num; double num4 = num2; double num5 = cropWidthRatio / cropHeightRatio; double num6 = num / num2; if (num6 > num5) { num3 = cropWidthRatio * num2 / cropHeightRatio; } else if (num6 < num5) { num4 = cropHeightRatio * num / cropWidthRatio; } double num7 = (num - num3) / 2.0; double num8 = (num2 - num4) / 2.0; BitmapHolder bitmapHolder = null; bitmapHolder = ((num7 == 0.0 && num8 == 0.0) ? new BitmapHolder(source.PixelData, source.Width, source.Height) : CropTransformation.ToCropped(source, (int)num7, (int)num8, (int)num3, (int)num4)); rad = ((rad != 0) ? ((int)((double)rad * (num3 + num4) / 2.0 / 500.0)) : ((int)(Math.Min(num3, num4) / 2.0))); int num9 = (int)num3; int num10 = (int)num4; ColorHolder transparent = ColorHolder.Transparent; for (int i = 0; i < num10; i++) { for (int j = 0; j < num9; j++) { if (j <= rad && i <= rad) { if (!CheckRoundedCorner(rad, rad, rad, Corner.TopLeftCorner, j, i)) { bitmapHolder.SetPixel(i * num9 + j, transparent); } } else if (j >= num9 - rad && i <= rad) { if (!CheckRoundedCorner(num9 - rad, rad, rad, Corner.TopRightCorner, j, i)) { bitmapHolder.SetPixel(i * num9 + j, transparent); } } else if (j >= num9 - rad && i >= num10 - rad) { if (!CheckRoundedCorner(num9 - rad, num10 - rad, rad, Corner.BottomRightCorner, j, i)) { bitmapHolder.SetPixel(i * num9 + j, transparent); } } else if (j <= rad && i >= num10 - rad && !CheckRoundedCorner(rad, num10 - rad, rad, Corner.BottomLeftCorner, j, i)) { bitmapHolder.SetPixel(i * num9 + j, transparent); } } } return(bitmapHolder); }
public static BitmapHolder ToTransformedCorners(BitmapHolder 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); } double cropX = ((sourceWidth - desiredWidth) / 2); double cropY = ((sourceHeight - desiredHeight) / 2); BitmapHolder bitmap = null; if (cropX != 0 || cropY != 0) { bitmap = CropTransformation.ToCropped(source, (int)cropX, (int)cropY, (int)(desiredWidth), (int)(desiredHeight)); } else { bitmap = new BitmapHolder(source.Pixels, source.Width, source.Height); } topLeftCornerSize = topLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; topRightCornerSize = topRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomLeftCornerSize = bottomLeftCornerSize * (desiredWidth + desiredHeight) / 2 / 100; bottomRightCornerSize = bottomRightCornerSize * (desiredWidth + desiredHeight) / 2 / 100; int topLeftSize = (int)topLeftCornerSize; int topRightSize = (int)topRightCornerSize; int bottomLeftSize = (int)bottomLeftCornerSize; int bottomRightSize = (int)bottomRightCornerSize; int w = bitmap.Width; int h = bitmap.Height; int transparentColor = Colors.Transparent.ToInt(); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (x <= topLeftSize && y <= topLeftSize) { //top left corner if (!CheckCorner(topLeftSize, topLeftSize, topLeftSize, cornersTransformType, Corner.TopLeftCorner, x, y)) { bitmap.Pixels[y * w + x] = transparentColor; } } else if (x >= w - topRightSize && y <= topRightSize && topRightSize > 0) { // top right corner if (!CheckCorner(w - topRightSize, topRightSize, topRightSize, cornersTransformType, Corner.TopRightCorner, x, y)) { bitmap.Pixels[y * w + x] = transparentColor; } } else if (x >= w - bottomRightSize && y >= h - bottomRightSize && bottomRightSize > 0) { // bottom right corner if (!CheckCorner(w - bottomRightSize, h - bottomRightSize, bottomRightSize, cornersTransformType, Corner.BottomRightCorner, x, y)) { bitmap.Pixels[y * w + x] = transparentColor; } } else if (x <= bottomLeftSize && y >= h - bottomLeftSize && bottomLeftSize > 0) { // bottom left corner if (!CheckCorner(bottomLeftSize, h - bottomLeftSize, bottomLeftSize, cornersTransformType, Corner.BottomLeftCorner, x, y)) { bitmap.Pixels[y * w + x] = transparentColor; } } } } return(bitmap); }
public static void ToRounded(BitmapHolder source, int 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); } double cropX = ((sourceWidth - desiredWidth) / 2); double cropY = ((sourceHeight - desiredHeight) / 2); if (cropX != 0 || cropY != 0) { CropTransformation.ToCropped(source, (int)cropX, (int)cropY, (int)(desiredWidth), (int)(desiredHeight)); } if (rad == 0) { rad = (int)(Math.Min(desiredWidth, desiredHeight) / 2); } else { rad = (int)(rad * (desiredWidth + desiredHeight) / 2 / 500); } int w = (int)desiredWidth; int h = (int)desiredHeight; int transparentColor = Colors.Transparent.ToInt(); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { if (x <= rad && y <= rad) { //top left corner if (!CheckRoundedCorner(rad, rad, rad, Corner.TopLeftCorner, x, y)) { source.Pixels[y * w + x] = transparentColor; } } else if (x >= w - rad && y <= rad) { // top right corner if (!CheckRoundedCorner(w - rad, rad, rad, Corner.TopRightCorner, x, y)) { source.Pixels[y * w + x] = transparentColor; } } else if (x >= w - rad && y >= h - rad) { // bottom right corner if (!CheckRoundedCorner(w - rad, h - rad, rad, Corner.BottomRightCorner, x, y)) { source.Pixels[y * w + x] = transparentColor; } } else if (x <= rad && y >= h - rad) { // bottom left corner if (!CheckRoundedCorner(rad, h - rad, rad, Corner.BottomLeftCorner, x, y)) { source.Pixels[y * w + x] = transparentColor; } } } } //TODO draws a border - we should optimize that and add some anti-aliasing if (borderSize > 0d) { borderSize = (borderSize * (desiredWidth + desiredHeight) / 2d / 500d); int borderColor = Colors.Transparent.ToInt(); try { if (!borderHexColor.StartsWith("#", StringComparison.Ordinal)) { borderHexColor.Insert(0, "#"); } borderColor = borderHexColor.ToColorFromHex().ToInt(); } catch (Exception) { } int intBorderSize = (int)Math.Ceiling(borderSize); for (int i = 2; i < intBorderSize; i++) { CircleAA(source, i, borderColor); } } }