C#实现绘制随机噪点和直线
短信预约 -IT技能 免费直播动态提醒
实践过程
效果
代码
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap image = new Bitmap(100, 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 2; i++)
{
Point tem_Point_1 = new Point(random.Next(image.Width), random.Next(image.Height));
Point tem_Point_2 = new Point(random.Next(image.Width), random.Next(image.Height));
g.DrawLine(new Pen(Color.Black), tem_Point_1, tem_Point_2);
}
//画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
Point tem_point = new Point(random.Next(image.Width), random.Next(image.Height));
image.SetPixel(tem_point.X, tem_point.Y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
pictureBox1.Image = image;
}
catch
{
}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
Bitmap bt = new Bitmap(pictureBox1.Image);
Graphics g = Graphics.FromImage(bt);
g.DrawLine(new Pen(Color.Red, 40), new Point(0, bt.Height / 2), new Point(bt.Width, bt.Height / 2));
g.DrawLine(new Pen(Color.Red, 40), new Point(bt.Width / 2, 0), new Point(bt.Width / 2, bt.Height));
g.DrawLine(new Pen(Color.Red, 40), new Point(0, 0), new Point(bt.Width, bt.Height));
g.DrawLine(new Pen(Color.Red, 40), new Point(0, bt.Height), new Point(bt.Width, 0));
pictureBox1.Image = bt;
}
}
}
到此这篇关于C#实现绘制随机噪点和直线的文章就介绍到这了,更多相关C#绘制随机噪点 直线内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341