View Code public class PhotoHelper
{
BaseForm _form; //拍照上传窗体
TextBox _txtFileName; //显示照片路径
PictureBox _picPhoto; //显示照片缩略图
public PhotoHelper(BaseForm form,TextBox txtFileName,PictureBox picPhoto)
{
_form = form;
_txtFileName = txtFileName;
_picPhoto = picPhoto;
_picPhoto.Image = new Bitmap(300, 350);
}
#region 拍照逻辑
public void TakePhoto()
{
//拍照-----------------------------
CameraCaptureDialog ccd = new CameraCaptureDialog();
ccd.Mode = CameraCaptureMode.Still;
ccd.Owner = _form;
ccd.Title = "拍照";
ccd.Resolution = new Size(1600, 1200);
ccd.StillQuality = CameraCaptureStillQuality.Normal;
if (ccd.ShowDialog() == DialogResult.OK)
{
//将拍好的以缩略图显示
ImageChangedArgs args = new ImageChangedArgs(ccd.FileName);
using (Graphics g = Graphics.FromImage(_picPhoto.Image))
{
g.DrawImage(args.Image,
new Rectangle(0, 0, _picPhoto.Image.Width, _picPhoto.Image.Height),
new Rectangle(0, 0, args.Image.Width, args.Image.Height),
GraphicsUnit.Pixel);
}
args.Dispose();
args = null;
_txtFileName.Text = ccd.FileName;
}
ccd.Dispose();
ccd = null;
}
#endregion
#region 照片上传逻辑
public void PhotoSave()
{
if (string.IsNullOrEmpty(_txtFileName.Text))
{
Global.ShowMsg("无照片可上传");
return;
}
string prompt = "确认上传照片?";
if (Global.ShowYesNoMsg(prompt) == DialogResult.No)
return;
//将照片转换为byte[]
Byte[] b = FileToBytes(_txtFileName.Text);
#region todo 将Byte[] 通过webservice上传服务器,在此请加入自己代码
// CommandAndTaskIdObj commandObj = _form.CommandAndTaskIdObj;
//WsRmsg msg = Global.GetService().UpFile(commandObj.UserName, commandObj.CommandId ,commandObj.TaskId , b, _txtFileName.Text);
//if (msg.Flag == 0)
//{
// b = null;
// throw new Exception(msg.Emsg);
//}
#endregion
Global.ShowMsg("照片上传成功");
_txtFileName.Text = string.Empty;
b = null;
}
#endregion
static byte[] FileToBytes(string sFilePath)
{
FileStream inFile = null;
try
{
byte[] binaryData;
inFile = new FileStream(sFilePath, FileMode.Open, FileAccess.Read);
binaryData = new Byte[inFile.Length];
long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
return binaryData;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (inFile != null)
{
inFile.Close();
inFile.Dispose();
}
inFile = null;
}
}
internal void PhotoDispose()
{
throw new NotImplementedException();
}
}
public class ImageChangedArgs : EventArgs, IDisposable
{
private Bitmap image;
///
/// used to create the thumbnail
///
private Stream stream;
public ImageChangedArgs(string imageFilePath)
{
try
{
this.stream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
this.image = new Bitmap(this.stream);
}
catch(Exception ex)
{
this.image = null;
if (this.stream != null)
{
this.stream.Close();
this.stream = null;
}
}
}
public Bitmap Image
{
get
{
return this.image;
}
}
public Stream Stream
{
get
{
return this.stream;
}
}
#region IDisposable Members
public void Dispose()
{
if (this.image != null)
this.image.Dispose();
this.image = null;
if (this.stream != null)
{
this.stream.Close();
this.stream.Dispose();
}
this.stream = null;
}
#endregion
}
|