常用操作
多线程操作
public static string id=string.Empty;
private void button3_Click(object sender, EventArgs e)
{
button4.Enabled = false;
Thread handlerThread = new Thread(HanlderThreadProc); //use delegate ThreadStart to start a new handler thread
handlerThread.Name = "Hanlder";
handlerThread.Start();
}
public void HanlderThreadProc()
{
//button3.Enabled = false;
Action<DataSet> actionDelegate = (x) => { this.button4.Text = x.Tables.Count.ToString();
button4.Enabled = true; };
var ds = new DataSet();
var dt = new DataTable();
ds.Tables.Add(dt);
button4.Invoke(actionDelegate, ds);
System.Threading.Thread.Sleep(5000);
MessageBox.Show("b");
id = string.Empty;
}
Thread异步处理
private void button3_Click(object sender, EventArgs e)
{
Thread handlerThread = new Thread(HanlderThreadProc); //use delegate ThreadStart to start a new handler thread
handlerThread.Name = "Hanlder";
handlerThread.Start();
}
public void HanlderThreadProc()
{
System.Threading.Thread.Sleep(5000);
MessageBox.Show("b");
id = string.Empty;
}
手工签名
一、页面设计
namespace BHG.BarScan
{
partial class FrmSignature
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmSignature));
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.pictureBox5 = new System.Windows.Forms.PictureBox();
this.pictureBox4 = new System.Windows.Forms.PictureBox();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// button2
//
this.button2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
this.button2.Location = new System.Drawing.Point(181, 331);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 19;
this.button2.Text = "保存";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(128)))));
this.button1.Location = new System.Drawing.Point(181, 306);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 18;
this.button1.Text = "清除";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox5
//
this.pictureBox5.BackColor = System.Drawing.Color.Yellow;
this.pictureBox5.Location = new System.Drawing.Point(123, 335);
this.pictureBox5.Name = "pictureBox5";
this.pictureBox5.Size = new System.Drawing.Size(27, 19);
this.pictureBox5.Click += new System.EventHandler(this.pictureBox5_Click);
//
// pictureBox4
//
this.pictureBox4.BackColor = System.Drawing.Color.LightSteelBlue;
this.pictureBox4.Location = new System.Drawing.Point(90, 335);
this.pictureBox4.Name = "pictureBox4";
this.pictureBox4.Size = new System.Drawing.Size(27, 19);
this.pictureBox4.Click += new System.EventHandler(this.pictureBox4_Click);
//
// pictureBox3
//
this.pictureBox3.BackColor = System.Drawing.Color.YellowGreen;
this.pictureBox3.Location = new System.Drawing.Point(123, 310);
this.pictureBox3.Name = "pictureBox3";
this.pictureBox3.Size = new System.Drawing.Size(27, 19);
this.pictureBox3.Click += new System.EventHandler(this.pictureBox3_Click);
//
// pictureBox2
//
this.pictureBox2.BackColor = System.Drawing.Color.Red;
this.pictureBox2.Location = new System.Drawing.Point(90, 310);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(27, 19);
this.pictureBox2.Click += new System.EventHandler(this.pictureBox2_Click);
//
// label2
//
this.label2.Location = new System.Drawing.Point(88, 294);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.Text = "颜色:";
//
// label1
//
this.label1.Location = new System.Drawing.Point(-1, 294);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.Text = "字号:";
//
// comboBox1
//
this.comboBox1.Items.Add("1");
this.comboBox1.Items.Add("2");
this.comboBox1.Items.Add("3");
this.comboBox1.Items.Add("5");
this.comboBox1.Items.Add("15");
this.comboBox1.Location = new System.Drawing.Point(2, 309);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(62, 23);
this.comboBox1.TabIndex = 17;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.White;
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(638, 191);
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove);
this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown);
this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
//
// FrmSignature
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(638, 455);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox5);
this.Controls.Add(this.pictureBox4);
this.Controls.Add(this.pictureBox3);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.pictureBox1);
this.Name = "FrmSignature";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);
}
#endregion
public System.Windows.Forms.Button button2;
public System.Windows.Forms.Button button1;
public System.Windows.Forms.PictureBox pictureBox5;
public System.Windows.Forms.PictureBox pictureBox4;
public System.Windows.Forms.PictureBox pictureBox3;
private System.Windows.Forms.PictureBox pictureBox2;
public System.Windows.Forms.Label label2;
public System.Windows.Forms.Label label1;
public System.Windows.Forms.ComboBox comboBox1;
public System.Windows.Forms.PictureBox pictureBox1;
}
}
二、功能代码
using System;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BHG.BarScan
{
public partial class FrmSignature : Form
{
bool hw;
Graphics g;
Graphics g1;
private Bitmap b;
Pen p = new Pen(Color.Lime);
int x1, y1, x2, y2;
public FrmSignature()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
g = pictureBox1.CreateGraphics();
g1 = System.Drawing.Graphics.FromImage(pictureBox1.Image);
p = new Pen(Color.Lime);
hw = false;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.SelectedIndex)
{
case 0:
p.Width = 1;
break;
case 1:
p.Width = 2;
break;
case 2:
p.Width = 3;
break;
case 3:
p.Width = 5;
break;
case 4:
p.Width = 15;
break;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
hw = false;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
g = pictureBox1.CreateGraphics();
if (hw)
{
x2 = e.X;
y2 = e.Y;
g.DrawLine(p, x1, y1, x2, y2);
g1.DrawLine(p, x1, y1, x2, y2);
x1 = x2;
y1 = y2;
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
hw = true;
x1 = e.X;
y1 = e.Y;
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Refresh();
}
private void button2_Click(object sender, EventArgs e)
{
//pictureBox1.DrawToBitmap(bmp, pictureBox1.Bounds);
//System.Drawing.Graphics g = pictureBox1.CreateGraphics();
//g.DrawImage(bit, pictureBox1.Width, pictureBox1.Height);
System.IO.FileStream fs=new FileStream(@"a.gif",FileMode.CreateNew);
pictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Bmp);
//bit.Save(fs,System.Drawing.Imaging.ImageFormat.Bmp);
fs.Close();
//bmp.Save("test.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
}
private void pictureBox2_Click(object sender, EventArgs e)
{
p.Color = Color.Red;
}
private void pictureBox3_Click(object sender, EventArgs e)
{
p.Color = Color.YellowGreen;
}
private void pictureBox4_Click(object sender, EventArgs e)
{
p.Color = Color.LightSteelBlue;
}
private void pictureBox5_Click(object sender, EventArgs e)
{
p.Color = Color.Yellow;
}
}
}
获取设备的唯一值
private static Int32 METHOD_BUFFERED = 0;
private static Int32 FILE_ANY_ACCESS = 0;
private static Int32 FILE_DEVICE_HAL = 0x00000101;
private const Int32 ERROR_NOT_SUPPORTED = 0x32;
private const Int32 ERROR_INSUFFICIENT_BUFFER = 0x7A;
private static Int32 IOCTL_HAL_GET_DEVICEID = ((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14) | ((21) << 2) | (METHOD_BUFFERED);
[DllImport("coredll.dll", SetLastError = true)]
private static extern bool KernelIoControl(Int32 dwIoControlCode, IntPtr lpInBuf, Int32 nInBufSize, byte[] lpOutBuf, Int32 nOutBufSize, ref Int32 lpBytesReturned);
private static string GetDeviceID()
{
// Initialize the output buffer to the size of a Win32 DEVICE_ID structure
byte[] outbuff = new byte[20];
Int32 dwOutBytes;
bool done = false;
Int32 nBuffSize = outbuff.Length;
// Set DEVICEID.dwSize to size of buffer. Some platforms look at
// this field rather than the nOutBufSize param of KernelIoControl
// when determining if the buffer is large enough.
//
BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0);
dwOutBytes = 0;
// Loop until the device ID is retrieved or an error occurs
while (!done)
{
if (KernelIoControl(IOCTL_HAL_GET_DEVICEID, IntPtr.Zero, 0, outbuff, nBuffSize, ref dwOutBytes))
{
done = true;
}
else
{
int error = Marshal.GetLastWin32Error();
switch (error)
{
case ERROR_NOT_SUPPORTED:
throw new NotSupportedException("IOCTL_HAL_GET_DEVICEID is not supported on this device", new Win32Exception(error));
case ERROR_INSUFFICIENT_BUFFER:
// The buffer wasn't big enough for the data. The
// required size is in the first 4 bytes of the output
// buffer (DEVICE_ID.dwSize).
nBuffSize = BitConverter.ToInt32(outbuff, 0);
outbuff = new byte[nBuffSize];
// Set DEVICEID.dwSize to size of buffer. Some
// platforms look at this field rather than the
// nOutBufSize param of KernelIoControl when
// determining if the buffer is large enough.
//
BitConverter.GetBytes(nBuffSize).CopyTo(outbuff, 0);
break;
default:
throw new Win32Exception(error, "Unexpected error");
}
}
}
Int32 dwPresetIDOffset = BitConverter.ToInt32(outbuff, 0x4); // DEVICE_ID.dwPresetIDOffset
Int32 dwPresetIDSize = BitConverter.ToInt32(outbuff, 0x8); // DEVICE_ID.dwPresetSize
Int32 dwPlatformIDOffset = BitConverter.ToInt32(outbuff, 0xc); // DEVICE_ID.dwPlatformIDOffset
Int32 dwPlatformIDSize = BitConverter.ToInt32(outbuff, 0x10); // DEVICE_ID.dwPlatformIDBytes
StringBuilder sb = new StringBuilder();
for (int i = dwPresetIDOffset; i < dwPresetIDOffset + dwPresetIDSize; i++)
{
sb.Append(String.Format("{0:X2}", outbuff));
}
sb.Append("-");
for (int i = dwPlatformIDOffset; i < dwPlatformIDOffset + dwPlatformIDSize; i++)
{
sb.Append(String.Format("{0:X2}", outbuff));
}
return sb.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
string strDeviceID = GetDeviceID();
MessageBox.Show(strDeviceID);
}
文件转换二进制
C# Stream 和 byte[] 之间的转换
一. 二进制转换成图片
MemoryStream ms = new MemoryStream(bytes);
ms.Position = 0;
Image img = Image.FromStream(ms);
ms.Close();
this.pictureBox1.Image
二. C#中byte[]与string的转换代码
1、System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
byte[] inputBytes =converter.GetBytes(inputString);
string inputString = converter.GetString(inputBytes);
2、string inputString = System.Convert.ToBase64String(inputBytes);
byte[] inputBytes = System.Convert.FromBase64String(inputString);
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
三. C# Stream 和 byte[] 之间的转换
/// 将 Stream 转成 byte[]
public byte[] StreamToBytes(Stream stream)
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
return bytes;
}
/// 将 byte[] 转成 Stream
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
四. Stream 和 文件之间的转换
将 Stream 写入文件
public void StreamToFile(Stream stream,string fileName)
{
// 把 Stream 转换成 byte[]
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(0, SeekOrigin.Begin);
// 把 byte[] 写入文件
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
五. 从文件读取 Stream
public Stream FileToStream(string fileName)
{
// 打开文件
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
// 读取文件的 byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Close();
// 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
}