View Code public class Photohelper { BaseForm _form; Photo Upload Form TextBox _txtfilename; Show Photo Path PictureBox _picphoto; Show photo thumbnails
Public Photohelper (BaseForm form,textbox txtfilename,picturebox Picphoto) { _form = form; _txtfilename = Txtfilename; _picphoto = Picphoto; _picphoto.image = new Bitmap (300, 350); } #region Camera logic public void Takephoto () { //Photo----------------------------- cameracapturedialog CCD = new CameraCaptureDialog (); CCD. Mode = Cameracapturemode.still; CCD. Owner = _form; CCD. Title = "Photograph"; CCD. Resolution = new Size (1600, 1200); CCD. stillquality = Cameracapturestillquality.normal; if (CCD. ShowDialog () = = DialogResult.OK) { //will be photographed in thumbnail display 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 Photo Upload logic public void Photosave () { if (string. IsNullOrEmpty (_txtfilename.text)) { Global.showmsg ("No photos to upload"); Return } string prompt = "Confirm upload photo?" "; if (global.showyesnomsg (prompt) = = dialogresult.no) Return Convert photo to byte[] Byte[] B = filetobytes (_txtfilename.text); #region Todo will byte[] upload the server via WebService, please add your own code here 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 ("Photo upload success");
_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; <summary> Used to create the thumbnail </summary> 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 } |