private RoundCornerImage ( Image image, int cornerRadius, bool topLeft = false, bool topRight = false, bool bottomLeft = false, bool bottomRight = false ) : |
||
image | Image | The image to add corners too |
cornerRadius | int | The radius of the corners. |
topLeft | bool | If the top left corner will have a rounded corner? |
topRight | bool | If the top right corner will have a rounded corner? |
bottomLeft | bool | If the bottom left corner will have a rounded corner? |
bottomRight | bool | If the bottom right corner will have a rounded corner? |
return |
private Bitmap RoundCornerImage(Image image, int cornerRadius, bool topLeft = false, bool topRight = false, bool bottomLeft = false, bool bottomRight = false)
{
int width = image.Width;
int height = image.Height;
int cornerDiameter = cornerRadius * 2;
// Create a new empty bitmap to hold rotated image
Bitmap newImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppPArgb);
newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
// Make a graphics object from the empty bitmap
using (Graphics graphics = Graphics.FromImage(newImage))
{
GraphicsHelper.SetGraphicsOptions(graphics);
// Add rounded corners
using (GraphicsPath path = new GraphicsPath())
{
// Determined if the top left has a rounded corner
if (topLeft)
{
path.AddArc(0, 0, cornerDiameter, cornerDiameter, 180, 90);
}
else
{
path.AddLine(0, 0, 0, 0);
}
// Determined if the top right has a rounded corner
if (topRight)
{
path.AddArc(0 + width - cornerDiameter, 0, cornerDiameter, cornerDiameter, 270, 90);
}
else
{
path.AddLine(width, 0, width, 0);
}
// Determined if the bottom left has a rounded corner
if (bottomRight)
{
path.AddArc(0 + width - cornerDiameter, 0 + height - cornerDiameter, cornerDiameter, cornerDiameter, 0, 90);
}
else
{
path.AddLine(width, height, width, height);
}
// Determined if the bottom right has a rounded corner
if (bottomLeft)
{
path.AddArc(0, 0 + height - cornerDiameter, cornerDiameter, cornerDiameter, 90, 90);
}
else
{
path.AddLine(0, height, 0, height);
}
using (Brush brush = new TextureBrush(image))
{
graphics.FillPath(brush, path);
}
}
}
image.Dispose();
return newImage;
}