十. 油畫效果
原理: 對圖像中某一范圍內的像素引入隨機值.
效果圖:
實現代碼:
油畫效果
private void button1_Click(object sender, EventArgs e)
{
//以油畫效果顯示圖像
Graphics g = this.panel1.CreateGraphics();
//Bitmap bitmap = this.MyBitmap;
//取得圖片尺寸
int width = MyBitmap.Width;
int height = MyBitmap.Height;
RectangleF rect = new RectangleF(0, 0, width, height);
Bitmap img = MyBitmap.Clone(rect, System.Drawing.Imaging.PixelFormat.DontCare);
//產生隨機數序列
Random rnd = new Random();
//取不同的值決定油畫效果的不同程度
int iModel = 2;
int i = width - iModel;
while (i > 1)
{
int j = height - iModel;
while (j > 1)
{
int iPos = rnd.Next(100000) % iModel;
//將該點的RGB值設置成附近iModel點之內的任一點
Color color = img.GetPixel(i + iPos, j + iPos);
img.SetPixel(i, j, color);
j = j - 1;
}
i = i - 1;
}
//重新繪制圖像
g.Clear(Color.White);
g.DrawImage(img, new Rectangle(0, 0, width, height));
}