using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TuPianJinDuTiao
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.ImageLocation = @"C:\Documents and Settings\Administrator\桌面\mnn.jpg";
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
}
public class ImageClass
{
/// <summary>马赛克效果</summary>
/// <param name="bmp">Bitmap 对象</param>
/// <param name="picBox">PictureBox 对象</param>
public static void MaSaiKe(Bitmap MyBitmap, PictureBox picBox)
{ //以马赛克效果显示图像
try
{
int dw = MyBitmap.Width / 50;
int dh = MyBitmap.Height / 50;
Graphics g = picBox.CreateGraphics();
g.Clear(Color.Gray);
Point[] MyPoint = new Point[2500];
for (int x = 0; x < 50; x++)
for (int y = 0; y < 50; y++)
{
MyPoint[x * 50 + y].X = x * dw;
MyPoint[x * 50 + y].Y = y * dh;
}
Bitmap bitmap = new Bitmap(MyBitmap.Width, MyBitmap.Height);
for (int i = 0; i < 10000; i++)
{
System.Random MyRandom = new Random();
int iPos = MyRandom.Next(2500);
for (int m = 0; m < dw; m++)
for (int n = 0; n < dh; n++)
{
bitmap.SetPixel(MyPoint[iPos].X + m, MyPoint[iPos].Y + n, MyBitmap.GetPixel(MyPoint[iPos].X + m, MyPoint[iPos].Y + n));
}
picBox.Refresh();
picBox.Image = bitmap;
}
for (int i = 0; i < 2500; i++)
for (int m = 0; m < dw; m++)
for (int n = 0; n < dh; n++)
{
bitmap.SetPixel(MyPoint[i].X + m, MyPoint[i].Y + n, MyBitmap.GetPixel(MyPoint[i].X + m, MyPoint[i].Y + n));
}
picBox.Refresh();
picBox.Image = bitmap;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
}
}
}
private void btnMaSaiKe_Click(object sender, EventArgs e)
{
ImageClass.MaSaiKe(new System.Drawing.Bitmap(this.pictureBox1.Image), pictureBox1);
}
}
}
该程序实现了把一张图片加入马赛克的特效。现在的问题是,图片的马赛克效果处理过程有点慢,如何加入一个进度条来显示图像的完成进度?要求有具体的实现代码。