C#实现图片特效 ( 八)

C#实现图片特效 ( 八)

九.马赛克效果

原理: 确定图像的随机位置点和确定马赛克块的大小,然后马赛克块图像覆盖随机点即可.

效果图:

实现代码:

马赛克效果
  private void button1_Click(object sender, EventArgs e)
{
//以马赛克效果显示图像
 try
{
int dw = MyBitmap.Width / 50;
int dh = MyBitmap.Height / 50;
Graphics g = this.pictureBox1.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));
}
this.pictureBox1.Refresh();
this.pictureBox1.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));
}
this.pictureBox1.Refresh();
this.pictureBox1.Image = bitmap;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "信息提示");
}
}
 

十. 油画效果

原理: 对图像中某一范围内的像素引入随机值.

效果图:

实现代码:

油画效果
  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)); 
} 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#实现图片轮播可以使用 `Timer` 控件来定时切换图片。以下是一个简单的示例代码: ```csharp public partial class Form1 : Form { private int currentIndex = 0; // 当前显示的图片索引 private List<Image> images = new List<Image>(); // 图片列表 private Timer timer; // 定时器 public Form1() { InitializeComponent(); // 初始化图片列表 images.Add(Properties.Resources.pic1); images.Add(Properties.Resources.pic2); images.Add(Properties.Resources.pic3); // 初始化定时器 timer = new Timer(); timer.Interval = 2000; // 设置定时器间隔为 2 秒 timer.Tick += Timer_Tick; // 绑定定时器 Tick 事件处理程序 timer.Start(); // 启动定时器 } private void Timer_Tick(object sender, EventArgs e) { // 切换图片 currentIndex = (currentIndex + 1) % images.Count; pictureBox1.Image = images[currentIndex]; } } ``` 在这个示例代码中,我们使用了 `List<Image>` 来存储图片,并使用 `Timer` 控件来定时切换图片。在窗体的构造函数中,我们初始化了图片列表和定时器,并将定时器启动。定时器每隔 2 秒触发一次 `Tick` 事件,在事件处理程序中切换当前显示的图片。当图片索引达到列表末尾时,会循环回到列表头部。 上述代码中,`pictureBox1` 是一个 `PictureBox` 控件,用于显示图片。在窗体设计器中,我们需要将 `pictureBox1` 的 `SizeMode` 属性设置为 `StretchImage`,以便图片能够适应控件大小进行缩放。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值