110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.IO;
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.Drawing2D;
using System.Drawing.Imaging;
namespace ID_photo_processing
{
public partial class Form1 : Form
{
public string FilePath;
public Form1()
{
InitializeComponent();
}
public string GetListBoxItem()
{
string filePath = string.Empty;
bool isSelected = IsListBoxSelected();
if (isSelected == true)
{
string listBoxItemValue = listBox1.SelectedItem.ToString();
filePath = listBoxItemValue;
}
else
{
MessageBox.Show("ListBox must be selected.");
}
return filePath;
}
public bool IsListBoxSelected()
{
bool selected;
if (listBox1.SelectedIndex == -1)//SelectedIndex==-1时,表示未选中任何项。
{
selected = false;
}
else
{
selected = true;
}
return selected;
}
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.All;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
for (int i = 0; i < s.Length; i++)
{
listBox1.Items.Add(s[i]);
}
}
//点击开始处理按钮遍历listBox中的元素进行处理的代码:
private void button1_Click(object sender, EventArgs e)
{
button2.Enabled = false;
textBoxTotal.Text = listBox1.Items.Count.ToString();
textBoxTotal.Refresh();
//问题1:遍历listbox中的元素,有什么办法比较好,我用for实际使用发现占用处理照片越多,占内存越大
for (int i = 0; i < listBox1.Items.Count; i++)
{
startprocess(i);
}
button2.Enabled = true;
}
//开始处理
private void startprocess(int i)
{
//当前在处理的文件计数器
int currentprocess = i + 1;
//当前在处理的文件计数显示
textBoxCurrent.Text = currentprocess.ToString();
textBoxCurrent.Refresh();
//当前在处理的文件路径
string path = listBox1.Items[i].ToString();
//当前在处理的文件名
string filename = System.IO.Path.GetFileNameWithoutExtension(path);
//当前在处理的文件所在文件夹
string filepath = System.IO.Path.GetDirectoryName(path);
DoPhoto(path, filename, filepath);
textBox1.Text = textBox1.Text + path + "处理完毕" + "\r\n";
textBox1.Refresh();
}
//处理照片关键代码 实际使用时速度是每分钟17张 能否换方法提高效率?
private void DoPhoto(string _path,string _filename,string _filepath)
{
string initcolor = "White";
//读取原始文件(要处理的文件)
Bitmap initBitmap = new Bitmap(_path);
//创建空白图像
Bitmap newBitmap = new Bitmap(1512, 1002);
//将图像初始化为白色
for (int i = 0; i < newBitmap.Width; i++)
for (int j = 0; j < newBitmap.Height; j++)
{
Color white = Color.FromName(initcolor);
newBitmap.SetPixel(i, j, white);
}
//读取原始图像,写入到newBitmap中,共写入8次
//理论只读取一次原始照片就好了,但是因为当时不知道如何做,所以用笨办法读了8次,如何做只读取一次?
for (int x = 10; x < 1512; x = x + initBitmap.Width + 20)
for (int y = 10; y < 1002; y = y + initBitmap.Height + 60)
for (int w = 1; w < initBitmap.Width; w++)
for (int h = 1; h < initBitmap.Height; h++)
{
Color color = initBitmap.GetPixel(w, h);
newBitmap.SetPixel(x + w, y + h, color);
}
//给处理后的图像添加文字,即身份证号和姓名
//给照片添加文字有什么比较高效的方法呢?
for (int x = 10; x < 1512; x = x + initBitmap.Width + 20)
for (int y = 10; y < 1002; y = y + initBitmap.Height + 60)
{
Graphics g = Graphics.FromImage(newBitmap);
Font font = new Font("宋体", 18);
SolidBrush sbrush = new SolidBrush(Color.Black);
g.DrawString(_filename, font, sbrush, new PointF(x, y + 441));
MemoryStream ms = new MemoryStream();
}
//保存处理完的文件
newBitmap.Save(_filepath+"\\"+"已处理"+_filename+".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
Refresh();
}
//清空所有文件
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
textBoxTotal.Text = "";
textBoxCurrent.Text = "";
textBox1.Text = "";
}
//关于
private void button3_about_Click(object sender, EventArgs e)
{
MessageBox.Show(@"
针对公司照片处理需求专门编写的程序,
可批量将含有身份证信息的jpg图片文件制作成
2*4张并将身份证号码及姓名附加在照片下方");
}
}
}
using System;
using System.IO;
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.Drawing2D;
using System.Drawing.Imaging;
namespace ID_photo_processing
{
public partial class Form1 : Form
{
public string FilePath;
public Form1()
{
InitializeComponent();
}
public string GetListBoxItem()
{
string filePath = string.Empty;
bool isSelected = IsListBoxSelected();
if (isSelected == true)
{
string listBoxItemValue = listBox1.SelectedItem.ToString();
filePath = listBoxItemValue;
}
else
{
MessageBox.Show("ListBox must be selected.");
}
return filePath;
}
public bool IsListBoxSelected()
{
bool selected;
if (listBox1.SelectedIndex == -1)//SelectedIndex==-1时,表示未选中任何项。
{
selected = false;
}
else
{
selected = true;
}
return selected;
}
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.All;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
for (int i = 0; i < s.Length; i++)
{
listBox1.Items.Add(s[i]);
}
}
//点击开始处理按钮遍历listBox中的元素进行处理的代码:
private void button1_Click(object sender, EventArgs e)
{
button2.Enabled = false;
textBoxTotal.Text = listBox1.Items.Count.ToString();
textBoxTotal.Refresh();
//for (int i = 0; i < listBox1.Items.Count; i++)
//{
// startprocess(i);
//}
int i = 0;
foreach (string path in listBox1.Items)
{
string filename = System.IO.Path.GetFileNameWithoutExtension(path);
string filepath = System.IO.Path.GetDirectoryName(path);
DoPhoto(path, filename, filepath);
textBox1.Text = textBox1.Text + path + "处理完毕" + "\r\n";
//textBox1.Text = textBox1.Text + path + DateTime.Now.ToString("hh:mm:ss");
textBox1.Refresh();
i= i + 1;
textBoxCurrent.Text = i.ToString();
textBoxCurrent.Refresh();
}
button2.Enabled = true;
MessageBox.Show("处理完毕");
}
//处理照片关键代码
private void DoPhoto(string _path,string _filename,string _filepath)
{
//读取原始文件(要处理的文件)
Bitmap initBitmap = new Bitmap(_path);
//创建空白图像
Bitmap newBitmap = new Bitmap(1512, 1002);
Graphics newjpg = Graphics.FromImage(newBitmap);
newjpg.Clear(Color.White);
//初始化字体
Font font = new Font("宋体", 18);
SolidBrush sbrush = new SolidBrush(Color.Black);
for (int x = 10; x < 1512; x = x + initBitmap.Width + 20)
for (int y = 10; y < 1002; y = y + initBitmap.Height + 60)
{
newjpg.DrawImage(initBitmap, x, y, 358, 441);
newjpg.DrawString(_filename, font, sbrush, new PointF(x, y + 446));
}
//保存处理完的文件
newBitmap.Save(_filepath+"\\"+"已处理"+_filename+".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
initBitmap.Dispose();
newBitmap.Dispose();
newjpg.Dispose();
Refresh();
}
//清空所有文件
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
textBoxTotal.Text = "";
textBoxCurrent.Text = "";
textBox1.Text = "";
}
就是还有个问题想再请教一下,为何点击开始处理按钮后,前几十张的时候程序还能响应,往后程序就挂起了呢?虽然挂起了,但是照片其实还是在正常处理中的。如何解决这个问题?让程序不论处理多少张照片都运算自如,不被挂起呢? public static void GetNewMap(string path)
{
Bitmap b1 = new Bitmap(path);
int width = b1.Width, height = b1.Height;
//设置好需要的宽高
Bitmap b = new Bitmap(width*4 + 16, height*2 + 4);
//取得画刷
Graphics g = Graphics.FromImage(b);
//画八张图
for (int i = 0; i < 4; i++)
{
g.DrawImage(b1, (width + 2)*i, 0);
g.DrawImage(b1, (width + 2)*i, height + 2);
}
//把合并后的位图保存到文件流中
int tempHigh = b1.Height;
FileStream fs = new FileStream("e:\\result.jpg", FileMode.Create);
b.Save(fs, ImageFormat.Jpeg);
fs.Flush();
fs.Close();
}