[點(diǎn)晴永久免費(fèi)OA]C# 將圖片截取成一個(gè)圓形
Image imgPhoto = Image.fromFile(@"big.jpg");
int Width = imgPhoto.Width;
int Height = imgPhoto.Height;//以上三句代碼是為了獲取圖片長和寬
int si = 0;//聲明裁剪尺寸由于是需要正方形 所以只需要一個(gè)變量
if (Width > Height)//下面判斷長和寬哪個(gè)值小就用哪個(gè)值 因?yàn)橐眉粢粋€(gè)正方形
{
si = Height;
}
else
{
si = Width;
}
Rectangle theRectangle = new Rectangle(new Point(0, 0), new Size(si, si));//裁剪位置 后面是裁剪尺寸
Bitmap bmp = new Bitmap(imgPhoto);//轉(zhuǎn)換到可操作的內(nèi)存中
Bitmap cloneBitmap = bmp.Clone(theRectangle, PixelFormat.DontCare);
// 計(jì)算圓形區(qū)域
for (int y = 0; y < si; y++)
{
for (int x = 0; x < si; x++)
{
if ((x - si / 2) * (x - si / 2) + (y - si / 2) * (y - si / 2) > (si / 2) * (si / 2))
{
//符合以上條件的為圓以外的區(qū)域
// 將這些區(qū)域設(shè)為指定顏色
cloneBitmap.SetPixel(x, y, Color.fromArgb(0, 32, 152, 213));//后面三個(gè)參數(shù)是rgb顏色 三個(gè)0表示黑色
}
}
}
// 綁定到圖片顯示控件
cloneBitmap.Save("Big1.jpg",ImageFormat.Jpeg);
該文章在 2022/11/25 15:48:05 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |