分享一个C#做的图像处理的小程序,模仿今天看到的电梯广告牌的效果

threenewbee 2019-02-22 12:55:28




下载地址:https://download.csdn.net/download/caozhy/10970359

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace B87874403
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
label1.Text = dlg.FileName;
}
}

private void button2_Click(object sender, EventArgs e)
{
DownloadDialog dlg = new DownloadDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
label1.Text = dlg.FileName;
}
}

private void button4_Click(object sender, EventArgs e)
{
if (label1.Text == "")
{
MessageBox.Show("请先选择一个图片");
return;
}
int span = (int)numericUpDown1.Value;
Bitmap input = new Bitmap(label1.Text);
Bitmap output = new Bitmap(input.Width, input.Height);
var g = Graphics.FromImage(output);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.FillRectangle(Brushes.Black, new Rectangle(0, 0, input.Width, input.Height));
float dx = 0, dy = 0, dw = 0, dh = 0;
Color brushcolor = Color.White;
for (int i = 0; i < input.Width / span; i++)
{
for (int j = 0; j < input.Height / span; j++)
{
float avgcolor = 0;
for (int k = i * span; k < i * span + span; k++)
{
for (int l = j * span; l < j * span + span; l++)
{
Color c = input.GetPixel(k,l);
avgcolor += c.GetBrightness();
}
}
avgcolor /= (span * span);
dx = (1 - avgcolor) / 2 * span;
dy = dx;
dw = avgcolor * span;
dh = dw;
g.FillEllipse(new SolidBrush(brushcolor), i * span + dx, j * span + dy, dw, dh);
}
}
pictureBox1.Image = output;
}

private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "bitmap|*.bmp";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Image img = pictureBox1.Image;
img.Save(sfd.FileName, ImageFormat.Bmp);
}
}

private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Text.RegularExpressions;
using System.Reflection;
using System.IO;

namespace B87874403
{
public partial class DownloadDialog : Form
{
public DownloadDialog()
{
InitializeComponent();
}

public string FileName { get; set; }

private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
pictureBox1.ImageLocation = textBox1.Text;
}
catch { }
}

private void button1_Click(object sender, EventArgs e)
{
WebClient wc = new WebClient();
FileName = "";
try
{
string fn = RndName(textBox1.Text);
wc.DownloadFile(textBox1.Text, fn);
FileName = fn;
}
catch {
MessageBox.Show("无法下载图片,请重新选择");
}
}

private string RndName(string url)
{
Uri u = new Uri(url);
string fn = u.GetLeftPart(UriPartial.Path);
string pattern = @"\.\w+$";
string rnd = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + new TimeSpan(DateTime.Now.Ticks - new DateTime(1970, 1, 1).Ticks).TotalSeconds.ToString();
if (Regex.IsMatch(fn, pattern))
fn = rnd + Regex.Match(fn, pattern).Value;
else
fn = rnd + ".jpg";
return fn;
}
}

}


修改了下,增加一个背景图的功能



新程序的下载 https://download.csdn.net/download/caozhy/10970439

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace B87874403
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
label1.Text = dlg.FileName;
}
}

private void button2_Click(object sender, EventArgs e)
{
DownloadDialog dlg = new DownloadDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
label1.Text = dlg.FileName;
}
}

private void button4_Click(object sender, EventArgs e)
{
if (label1.Text == "")
{
MessageBox.Show("请先选择一个图片");
return;
}
int span = (int)numericUpDown1.Value;
Bitmap input = new Bitmap(label1.Text);
Bitmap output = new Bitmap(input.Width, input.Height);
Bitmap bg = null;
if (label4.Text != "")
{
bg = new Bitmap(input.Width / span, input.Height / span);
var imgbg = new Bitmap(label4.Text);
var bgg = Graphics.FromImage(bg);
bgg.DrawImage(imgbg, 0, 0, input.Width / span, input.Height / span);
}
var g = Graphics.FromImage(output);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.FillRectangle(Brushes.Black, new Rectangle(0, 0, input.Width, input.Height));
float dx = 0, dy = 0, dw = 0, dh = 0;
Color brushcolor = Color.White;
for (int i = 0; i < input.Width / span; i++)
{
for (int j = 0; j < input.Height / span; j++)
{
float avgcolor = 0;
for (int k = i * span; k < i * span + span; k++)
{
for (int l = j * span; l < j * span + span; l++)
{
Color c = input.GetPixel(k,l);
avgcolor += c.GetBrightness();
}
}
avgcolor /= (span * span);
dx = (1 - avgcolor) / 2 * span;
dy = dx;
dw = avgcolor * span;
dh = dw;
if (bg != null)
brushcolor = bg.GetPixel(i, j);
g.FillEllipse(new SolidBrush(brushcolor), i * span + dx, j * span + dy, dw, dh);
}
}
pictureBox1.Image = output;
}

private void btnSave_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "bitmap|*.bmp";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Image img = pictureBox1.Image;
img.Save(sfd.FileName, ImageFormat.Bmp);
}
}

private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void label4_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
label4.Text = dlg.FileName;
}
else
{
label4.Text = "";
}
}
}
}
...全文
397 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lindexi_gd 2019-02-23
  • 打赏
  • 举报
回复
夜风可知冷 2019-02-22
  • 打赏
  • 举报
回复
  • 打赏
  • 举报
回复
shen_wei 2019-02-22
  • 打赏
  • 举报
回复

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧