Winform扫描+上传
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WIA;
using System.IO;
using System.Net;
using System.Threading;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;
namespace TwainGui
{
public partial class ScanInterface : Form
{
public ScanInterface()
{
InitializeComponent();
}
string selectedFileNmae = string.Empty;
string inifile = "d:/trentfile/";//System.Configuration.ConfigurationSettings.AppSettings["LocalPath"];
string ServIP = System.Configuration.ConfigurationSettings.AppSettings["ServIP"];
private ProgressWindow myProcessBar = null;
private delegate bool IncreaseHandle(int nValue);
private IncreaseHandle myIncrease = null;
private int sleeptime = 500;
private void ShowProcessBar()
{
myProcessBar = new ProgressWindow();
// Init increase event
myIncrease = new IncreaseHandle(myProcessBar.Increase);
myProcessBar.ShowDialog();
myProcessBar = null;
}
private void ThreadFun()
{
MethodInvoker mi = new MethodInvoker(ShowProcessBar);
this.BeginInvoke(mi);
//Thread.Sleep(1000);//Sleep a while to show window
bool blnIncreased = false;
object objReturn = null;
do
{
Thread.Sleep(sleeptime);
objReturn = this.Invoke(this.myIncrease,
new object[] { 2 });
blnIncreased = (bool)objReturn;
}
while (blnIncreased);
}
private void FillPanel()
{
if (Directory.Exists(@inifile))
{
lv.Controls.Clear();
string[] files = Directory.GetFiles(@inifile);
PictureBox pic = null;
Panel panel = null;
Label label = null;
int i = 0, j = 0;
foreach (string dir in files)
{
panel = new Panel();
panel.Size = new Size(200, 220);
panel.Left = i;
panel.Top = j;
panel.BackColor = Color.Red;
i += 200;
if (i > 0 && i % 600 == 0)
{
i = 0;
j += 220;
}
label = new Label();
label.Size = new Size(200, 20);
label.Top = 200;
FileInfo fi = new FileInfo(dir);
label.Text = fi.Name;
label.BackColor = Color.Gray;
label.Font = new Font(label.Font.FontFamily, 13);
pic = new PictureBox();
pic.ContextMenuStrip = cm;
pic.BorderStyle = BorderStyle.None;
pic.Size = new Size(200, 200);
pic.SizeMode = PictureBoxSizeMode.StretchImage;
using (FileStream stream = new FileStream(dir, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
pic.Image = Image.FromStream(stream);
pic.Name = stream.Name;
}
panel.Controls.Add(label);
panel.Controls.Add(pic);
lv.Controls.Add(panel);
}
}
}
private void DelPic(object sender,System.EventArgs e)
{
PictureBox pb = (PictureBox)sender;
if (DialogResult.OK == MessageBox.Show("你确定要删除此扫描件?","删除提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning))
{
//点击了OK
File.Delete(pb.Name);
}
else
{
//点击了Cancel
}
//File.Delete(@"D:/web/12.jpg");
FillPanel();
}
private void exit_Click(object sender, EventArgs e)
{
Close();
}
private void UpLoad_Click(object sender, EventArgs e)
{
new Thread(new ThreadStart(uploadfunction)).Start();
}
private delegate void getback();
private string nfn ="";
private void uploadfunction()//上传文件方法调用
{
if (Directory.Exists(@inifile))
{
if (Directory.GetFiles(@inifile).Length > 0)
{
new Thread(new ThreadStart(ThreadFun)).Start();
bool bl = sendBytes();
if (bl)
{
sleeptime = 0;
MessageBox.Show("扫描件已保存到指定文件夹中");
//UpLoad.Visible = false;
//Exit.Visible = true;
getback gb = new getback(uploaded);
Invoke(gb);
}
else
{
sleeptime = 0;
MessageBox.Show("扫描件保存失败,请稍后重试!");
}
//lv.Controls.Clear();
}
else
{
MessageBox.Show("没有任何扫描件被扫描", "保存提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show("没有任何扫描件被扫描", "保存提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void uploaded()
{
lv.Controls.Clear();
UpLoad.Visible = false;
Exit.Visible = true;
nfn = "";
}
private void getuplaodfilename()//获取上传文件名
{
EnterFileName ef = new EnterFileName();
ef.StartPosition = FormStartPosition.CenterScreen;
ef.Show(this);
}
private void ScanButton_Click(object sender, EventArgs e)
{
sleeptime =500;
ScanButton.Visible = false;
Exit.Visible = false;
CommonDialogClass cdc = new WIA.CommonDialogClass();
ImageFile imageFile = null;
try
{
//imageFile = cdc.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType,
//WIA.WiaImageIntent.TextIntent,
//WIA.WiaImageBias.MaximizeQuality,
//"{00000000-0000-0000-0000-000000000000}",
//true,
//true,
//false);B96B3CAB-0728-11D3-9D7B-0000F81EF32E
DeviceManager manager = new DeviceManagerClass();
Device device = null;
foreach (DeviceInfo info in manager.DeviceInfos)
{
if (info.Type != WiaDeviceType.ScannerDeviceType) continue;
device = info.Connect();
break;
}
if (device == null)
{
MessageBox.Show("没有发现任何可供使用的扫描仪");
Exit.Visible = true;
}
else
{
Item item = device.Items[1];
imageFile = cdc.ShowTransfer(item, "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}", true) as ImageFile;
}
}
catch (System.Runtime.InteropServices.COMException)
{
imageFile = null;
}
if (imageFile != null)
{
DateTime dt = DateTime.Now;
string fname = "scan" + dt.ToString("yyyyMMddhhmmss") + ".jpg";//文件名
var buffer = imageFile.FileData.get_BinaryData() as byte[];
using (MemoryStream ms = new MemoryStream())
{
if (!Directory.Exists(@inifile))
{
Directory.CreateDirectory(@inifile);
}
Stream s = new FileStream(@inifile + fname, FileMode.Create);
ms.Write(buffer, 0, buffer.Length);
Bitmap bm = new Bitmap(ms);
Compress(bm, s, 50);
bm.Dispose();
s.Dispose();
}
FillPanel();
if (DialogResult.OK == MessageBox.Show("是否继续扫描下一个扫描件?", "扫描提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
{
//点击了OK
ScanButton_Click(sender, e);
UpLoad.Visible = false;
}
else
{
ScanButton.Visible = false;
//点击了Cancel
if (nfn != "")
{
UpLoad.Visible = true;
}
else
{
getuplaodfilename();
}
}
}
}
public static void Compress(Bitmap srcBitmap, Stream destStream, long level)//图片压缩
{
ImageCodecInfo myImageCodecInfo;
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
// Get an ImageCodecInfo object that represents the JPEG codec.
myImageCodecInfo = GetEncoderInfo("image/jpeg");
// Create an Encoder object based on the GUID
// for the Quality parameter category.
myEncoder = System.Drawing.Imaging.Encoder.Quality;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
myEncoderParameters = new EncoderParameters(1);
// Save the bitmap as a JPEG file with 给定的 quality level
myEncoderParameter = new EncoderParameter(myEncoder, level);
myEncoderParameters.Param[0] = myEncoderParameter;
srcBitmap.Save(destStream, myImageCodecInfo, myEncoderParameters);
}
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
private void deletPic_Click(object sender, EventArgs e)//删除扫描件
{
if (DialogResult.OK == MessageBox.Show("你确定要删除此扫描件?", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning))
{
//点击了OK
File.Delete(@selectedFileNmae);
FillPanel();
}
else
{
//点击了Cancel
}
}
private void alterName_Click(object sender, EventArgs e)//修改名称
{
NewName.Visible = true;
NameLabel.Visible = true;
AlterBtn.Visible = true;
ScanButton.Visible = false;
UpLoad.Visible = false;
AlterCancel.Visible = true;
}
private void cm_Opening(object sender, CancelEventArgs e)//获取右键菜单源
{
selectedFileNmae = ((sender as ContextMenuStrip).SourceControl.Name);
}
private bool sendBytes()//上传文件执行方法
{
bool bl = false;
try
{
if (nfn == "") {
getuplaodfilename(); }
else
{
ASCIIEncoding encoding = new ASCIIEncoding();
string url = @"http://" + ServIP + "/Handler/WinformUpload.ashx?filename=" + nfn;
foreach (string file in Directory.GetFiles(@inifile))
{
WebClient myWebClient = new WebClient();
string result = encoding.GetString(myWebClient.UploadFile(url, "POST", file));
if (result.Equals("success"))
{
sleeptime = 0;
File.Delete(@file);
bl = true;
}
else
{
sleeptime = 0;
bl = false;
break;
}
}
if (bl)
{
Directory.Delete(@inifile);
}
}
}
catch (Exception)
{
bl = false;
}
return bl;
}
private void emptyfile(){
try
{
foreach (string file in Directory.GetFiles(@inifile))
{
File.Delete(@file);
}
}
catch (Exception)
{
MessageBox.Show("图片扫描失败,请重试");
}
}
private void getFiles()
{
lock (this)
{
bool bl = false;
string[] files = Directory.GetFiles(@inifile);
try
{
foreach (string dir in files)
{
bl = uploadfile(dir); //是文件的话,可以加上你要的操作
Thread.Sleep(1000);
if (!bl)
{
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show("上传失败!" + ex.ToString());//防止有些文件无权限访问,屏蔽异常
}
if (bl)
{
MessageBox.Show("上传成功!");
}
else
{
MessageBox.Show("上传失败!");
}
}
}
private void AlterBtn_Click(object sender, EventArgs e)//修改文件名
{
string tem=NewName.Text.Trim();
if(!tem.Contains(".jpg")){
tem = tem + ".jpg";
}
if (tem!= ""&&tem.Length<255)
{
if(judgeEnter(tem)){
File.Move(@selectedFileNmae, @inifile + tem);
NewName.Visible = false;
NameLabel.Visible = false;
AlterBtn.Visible = false;
ScanButton.Visible = true;
UpLoad.Visible = true;
AlterCancel.Visible = false;
FillPanel();
}else
{
MessageBox.Show("文件名不正确");
}
}
else
{
MessageBox.Show("请输入文件名");
}
NewName.Text="";
}
private bool judgeEnter(string value)//判断文件名称
{
string pattern = @"[/\\<>*&^%$#@~?]";//正则式子
Match m = Regex.Match(value, pattern); // 匹配正则表达式,把this.textBox1.Text跟pattern正则对比
if (!m.Success) // 判断输入的是不是英文和数字,不是进入
{ return true;}
return false;
}
private void AlterCancel_Click(object sender, EventArgs e)//取消息修改操作
{
NewName.Visible = false;
NameLabel.Visible = false;
AlterBtn.Visible = false;
ScanButton.Visible = true;
UpLoad.Visible = true;
AlterCancel.Visible = false;
}
private void Exit_Click_1(object sender, EventArgs e)//退出操作
{
Close();
}
public void setNetNmae(string value)
{
nfn = value;
UpLoad.Visible = true;
ScanButton.Visible = true;
}
}
}
进度条:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TwainGui
{
public partial class ProgressWindow : Form
{
public ProgressWindow()
{
InitializeComponent();
}
public bool Increase(int nValue)
{
if (nValue > 0)
{
if (ProBar.Value + nValue < ProBar.Maximum)
{
ProBar.Value += nValue;
return true;
}
else
{
ProBar.Value = ProBar.Maximum;
this.Close();
return false;
}
}
return false;
}
}
}
弹出框:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace TwainGui
{
public partial class EnterFileName : Form
{
public EnterFileName()
{
InitializeComponent();
}
public EnterFileName efn;
private void nkeepbtn_Click(object sender, EventArgs e)
{
string tem = NetFileName.Text.Trim();
if (tem != "" && tem.Length < 255)
{
if (judgeEnter(tem))
{
ScanInterface sif = (ScanInterface)this.Owner;
sif.setNetNmae(tem);
this.Close();
}
else
{
MessageBox.Show("文件名不正确");
}
}
else
{
MessageBox.Show("请输入文件名");
}
}
private bool judgeEnter(string value)
{
string pattern = @"[/\\<>*&^%$#@~?]";//正则式子
Match m = Regex.Match(value, pattern); // 匹配正则表达式,把this.textBox1.Text跟pattern正则对比
if (!m.Success) // 判断输入的是不是英文和数字,不是进入
{ return true; }
return false;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WIA;
using System.IO;
using System.Net;
using System.Threading;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;
namespace TwainGui
{
public partial class ScanInterface : Form
{
public ScanInterface()
{
InitializeComponent();
}
string selectedFileNmae = string.Empty;
string inifile = "d:/trentfile/";//System.Configuration.ConfigurationSettings.AppSettings["LocalPath"];
string ServIP = System.Configuration.ConfigurationSettings.AppSettings["ServIP"];
private ProgressWindow myProcessBar = null;
private delegate bool IncreaseHandle(int nValue);
private IncreaseHandle myIncrease = null;
private int sleeptime = 500;
private void ShowProcessBar()
{
myProcessBar = new ProgressWindow();
// Init increase event
myIncrease = new IncreaseHandle(myProcessBar.Increase);
myProcessBar.ShowDialog();
myProcessBar = null;
}
private void ThreadFun()
{
MethodInvoker mi = new MethodInvoker(ShowProcessBar);
this.BeginInvoke(mi);
//Thread.Sleep(1000);//Sleep a while to show window
bool blnIncreased = false;
object objReturn = null;
do
{
Thread.Sleep(sleeptime);
objReturn = this.Invoke(this.myIncrease,
new object[] { 2 });
blnIncreased = (bool)objReturn;
}
while (blnIncreased);
}
private void FillPanel()
{
if (Directory.Exists(@inifile))
{
lv.Controls.Clear();
string[] files = Directory.GetFiles(@inifile);
PictureBox pic = null;
Panel panel = null;
Label label = null;
int i = 0, j = 0;
foreach (string dir in files)
{
panel = new Panel();
panel.Size = new Size(200, 220);
panel.Left = i;
panel.Top = j;
panel.BackColor = Color.Red;
i += 200;
if (i > 0 && i % 600 == 0)
{
i = 0;
j += 220;
}
label = new Label();
label.Size = new Size(200, 20);
label.Top = 200;
FileInfo fi = new FileInfo(dir);
label.Text = fi.Name;
label.BackColor = Color.Gray;
label.Font = new Font(label.Font.FontFamily, 13);
pic = new PictureBox();
pic.ContextMenuStrip = cm;
pic.BorderStyle = BorderStyle.None;
pic.Size = new Size(200, 200);
pic.SizeMode = PictureBoxSizeMode.StretchImage;
using (FileStream stream = new FileStream(dir, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
pic.Image = Image.FromStream(stream);
pic.Name = stream.Name;
}
panel.Controls.Add(label);
panel.Controls.Add(pic);
lv.Controls.Add(panel);
}
}
}
private void DelPic(object sender,System.EventArgs e)
{
PictureBox pb = (PictureBox)sender;
if (DialogResult.OK == MessageBox.Show("你确定要删除此扫描件?","删除提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning))
{
//点击了OK
File.Delete(pb.Name);
}
else
{
//点击了Cancel
}
//File.Delete(@"D:/web/12.jpg");
FillPanel();
}
private void exit_Click(object sender, EventArgs e)
{
Close();
}
private void UpLoad_Click(object sender, EventArgs e)
{
new Thread(new ThreadStart(uploadfunction)).Start();
}
private delegate void getback();
private string nfn ="";
private void uploadfunction()//上传文件方法调用
{
if (Directory.Exists(@inifile))
{
if (Directory.GetFiles(@inifile).Length > 0)
{
new Thread(new ThreadStart(ThreadFun)).Start();
bool bl = sendBytes();
if (bl)
{
sleeptime = 0;
MessageBox.Show("扫描件已保存到指定文件夹中");
//UpLoad.Visible = false;
//Exit.Visible = true;
getback gb = new getback(uploaded);
Invoke(gb);
}
else
{
sleeptime = 0;
MessageBox.Show("扫描件保存失败,请稍后重试!");
}
//lv.Controls.Clear();
}
else
{
MessageBox.Show("没有任何扫描件被扫描", "保存提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show("没有任何扫描件被扫描", "保存提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void uploaded()
{
lv.Controls.Clear();
UpLoad.Visible = false;
Exit.Visible = true;
nfn = "";
}
private void getuplaodfilename()//获取上传文件名
{
EnterFileName ef = new EnterFileName();
ef.StartPosition = FormStartPosition.CenterScreen;
ef.Show(this);
}
private void ScanButton_Click(object sender, EventArgs e)
{
sleeptime =500;
ScanButton.Visible = false;
Exit.Visible = false;
CommonDialogClass cdc = new WIA.CommonDialogClass();
ImageFile imageFile = null;
try
{
//imageFile = cdc.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType,
//WIA.WiaImageIntent.TextIntent,
//WIA.WiaImageBias.MaximizeQuality,
//"{00000000-0000-0000-0000-000000000000}",
//true,
//true,
//false);B96B3CAB-0728-11D3-9D7B-0000F81EF32E
DeviceManager manager = new DeviceManagerClass();
Device device = null;
foreach (DeviceInfo info in manager.DeviceInfos)
{
if (info.Type != WiaDeviceType.ScannerDeviceType) continue;
device = info.Connect();
break;
}
if (device == null)
{
MessageBox.Show("没有发现任何可供使用的扫描仪");
Exit.Visible = true;
}
else
{
Item item = device.Items[1];
imageFile = cdc.ShowTransfer(item, "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}", true) as ImageFile;
}
}
catch (System.Runtime.InteropServices.COMException)
{
imageFile = null;
}
if (imageFile != null)
{
DateTime dt = DateTime.Now;
string fname = "scan" + dt.ToString("yyyyMMddhhmmss") + ".jpg";//文件名
var buffer = imageFile.FileData.get_BinaryData() as byte[];
using (MemoryStream ms = new MemoryStream())
{
if (!Directory.Exists(@inifile))
{
Directory.CreateDirectory(@inifile);
}
Stream s = new FileStream(@inifile + fname, FileMode.Create);
ms.Write(buffer, 0, buffer.Length);
Bitmap bm = new Bitmap(ms);
Compress(bm, s, 50);
bm.Dispose();
s.Dispose();
}
FillPanel();
if (DialogResult.OK == MessageBox.Show("是否继续扫描下一个扫描件?", "扫描提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
{
//点击了OK
ScanButton_Click(sender, e);
UpLoad.Visible = false;
}
else
{
ScanButton.Visible = false;
//点击了Cancel
if (nfn != "")
{
UpLoad.Visible = true;
}
else
{
getuplaodfilename();
}
}
}
}
public static void Compress(Bitmap srcBitmap, Stream destStream, long level)//图片压缩
{
ImageCodecInfo myImageCodecInfo;
System.Drawing.Imaging.Encoder myEncoder;
EncoderParameter myEncoderParameter;
EncoderParameters myEncoderParameters;
// Get an ImageCodecInfo object that represents the JPEG codec.
myImageCodecInfo = GetEncoderInfo("image/jpeg");
// Create an Encoder object based on the GUID
// for the Quality parameter category.
myEncoder = System.Drawing.Imaging.Encoder.Quality;
// Create an EncoderParameters object.
// An EncoderParameters object has an array of EncoderParameter
// objects. In this case, there is only one
// EncoderParameter object in the array.
myEncoderParameters = new EncoderParameters(1);
// Save the bitmap as a JPEG file with 给定的 quality level
myEncoderParameter = new EncoderParameter(myEncoder, level);
myEncoderParameters.Param[0] = myEncoderParameter;
srcBitmap.Save(destStream, myImageCodecInfo, myEncoderParameters);
}
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
int j;
ImageCodecInfo[] encoders;
encoders = ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
return null;
}
private void deletPic_Click(object sender, EventArgs e)//删除扫描件
{
if (DialogResult.OK == MessageBox.Show("你确定要删除此扫描件?", "删除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning))
{
//点击了OK
File.Delete(@selectedFileNmae);
FillPanel();
}
else
{
//点击了Cancel
}
}
private void alterName_Click(object sender, EventArgs e)//修改名称
{
NewName.Visible = true;
NameLabel.Visible = true;
AlterBtn.Visible = true;
ScanButton.Visible = false;
UpLoad.Visible = false;
AlterCancel.Visible = true;
}
private void cm_Opening(object sender, CancelEventArgs e)//获取右键菜单源
{
selectedFileNmae = ((sender as ContextMenuStrip).SourceControl.Name);
}
private bool sendBytes()//上传文件执行方法
{
bool bl = false;
try
{
if (nfn == "") {
getuplaodfilename(); }
else
{
ASCIIEncoding encoding = new ASCIIEncoding();
string url = @"http://" + ServIP + "/Handler/WinformUpload.ashx?filename=" + nfn;
foreach (string file in Directory.GetFiles(@inifile))
{
WebClient myWebClient = new WebClient();
string result = encoding.GetString(myWebClient.UploadFile(url, "POST", file));
if (result.Equals("success"))
{
sleeptime = 0;
File.Delete(@file);
bl = true;
}
else
{
sleeptime = 0;
bl = false;
break;
}
}
if (bl)
{
Directory.Delete(@inifile);
}
}
}
catch (Exception)
{
bl = false;
}
return bl;
}
private void emptyfile(){
try
{
foreach (string file in Directory.GetFiles(@inifile))
{
File.Delete(@file);
}
}
catch (Exception)
{
MessageBox.Show("图片扫描失败,请重试");
}
}
private void getFiles()
{
lock (this)
{
bool bl = false;
string[] files = Directory.GetFiles(@inifile);
try
{
foreach (string dir in files)
{
bl = uploadfile(dir); //是文件的话,可以加上你要的操作
Thread.Sleep(1000);
if (!bl)
{
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show("上传失败!" + ex.ToString());//防止有些文件无权限访问,屏蔽异常
}
if (bl)
{
MessageBox.Show("上传成功!");
}
else
{
MessageBox.Show("上传失败!");
}
}
}
private void AlterBtn_Click(object sender, EventArgs e)//修改文件名
{
string tem=NewName.Text.Trim();
if(!tem.Contains(".jpg")){
tem = tem + ".jpg";
}
if (tem!= ""&&tem.Length<255)
{
if(judgeEnter(tem)){
File.Move(@selectedFileNmae, @inifile + tem);
NewName.Visible = false;
NameLabel.Visible = false;
AlterBtn.Visible = false;
ScanButton.Visible = true;
UpLoad.Visible = true;
AlterCancel.Visible = false;
FillPanel();
}else
{
MessageBox.Show("文件名不正确");
}
}
else
{
MessageBox.Show("请输入文件名");
}
NewName.Text="";
}
private bool judgeEnter(string value)//判断文件名称
{
string pattern = @"[/\\<>*&^%$#@~?]";//正则式子
Match m = Regex.Match(value, pattern); // 匹配正则表达式,把this.textBox1.Text跟pattern正则对比
if (!m.Success) // 判断输入的是不是英文和数字,不是进入
{ return true;}
return false;
}
private void AlterCancel_Click(object sender, EventArgs e)//取消息修改操作
{
NewName.Visible = false;
NameLabel.Visible = false;
AlterBtn.Visible = false;
ScanButton.Visible = true;
UpLoad.Visible = true;
AlterCancel.Visible = false;
}
private void Exit_Click_1(object sender, EventArgs e)//退出操作
{
Close();
}
public void setNetNmae(string value)
{
nfn = value;
UpLoad.Visible = true;
ScanButton.Visible = true;
}
}
}
进度条:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TwainGui
{
public partial class ProgressWindow : Form
{
public ProgressWindow()
{
InitializeComponent();
}
public bool Increase(int nValue)
{
if (nValue > 0)
{
if (ProBar.Value + nValue < ProBar.Maximum)
{
ProBar.Value += nValue;
return true;
}
else
{
ProBar.Value = ProBar.Maximum;
this.Close();
return false;
}
}
return false;
}
}
}
弹出框:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace TwainGui
{
public partial class EnterFileName : Form
{
public EnterFileName()
{
InitializeComponent();
}
public EnterFileName efn;
private void nkeepbtn_Click(object sender, EventArgs e)
{
string tem = NetFileName.Text.Trim();
if (tem != "" && tem.Length < 255)
{
if (judgeEnter(tem))
{
ScanInterface sif = (ScanInterface)this.Owner;
sif.setNetNmae(tem);
this.Close();
}
else
{
MessageBox.Show("文件名不正确");
}
}
else
{
MessageBox.Show("请输入文件名");
}
}
private bool judgeEnter(string value)
{
string pattern = @"[/\\<>*&^%$#@~?]";//正则式子
Match m = Regex.Match(value, pattern); // 匹配正则表达式,把this.textBox1.Text跟pattern正则对比
if (!m.Success) // 判断输入的是不是英文和数字,不是进入
{ return true; }
return false;
}
}
}