.net - C# code understanding and learning -
i learning c# diving it. , got stopped. have code below has menu item, acquire. runs acquire fine when select it. want run acquire application loaded / started.
where make necessary change?
using system; using system.drawing; using system.collections; using system.componentmodel; using system.windows.forms; using system.data; using system.io; using system.runtime.interopservices; using wialib; // namespace of imported wia scripting com component namespace wia { /// <summary> mainform wia sample </summary> public class mainform : system.windows.forms.form { private system.windows.forms.mainmenu mainmenu; private system.windows.forms.menuitem menutopfile; private system.windows.forms.menuitem menufileacquire; private system.windows.forms.menuitem menufilesaveas; private system.windows.forms.menuitem menufilesep1; private system.windows.forms.menuitem menufileexit; private system.windows.forms.picturebox picturebox; private icontainer components; public mainform() { // // required windows form designer support // initializecomponent(); } /// <summary> /// clean resources being used. /// </summary> protected override void dispose( bool disposing ) { if( disposing ) { disposeimage(); if (components != null) { components.dispose(); } } base.dispose( disposing ); } #region windows form designer generated code /// <summary> /// required method designer support - not modify /// contents of method code editor. /// </summary> private void initializecomponent() { this.components = new system.componentmodel.container(); system.componentmodel.componentresourcemanager resources = new system.componentmodel.componentresourcemanager(typeof(mainform)); this.mainmenu = new system.windows.forms.mainmenu(this.components); this.menutopfile = new system.windows.forms.menuitem(); this.menufileacquire = new system.windows.forms.menuitem(); this.menufilesaveas = new system.windows.forms.menuitem(); this.menufilesep1 = new system.windows.forms.menuitem(); this.menufileexit = new system.windows.forms.menuitem(); this.picturebox = new system.windows.forms.picturebox(); ((system.componentmodel.isupportinitialize)(this.picturebox)).begininit(); this.suspendlayout(); // // mainmenu // this.mainmenu.menuitems.addrange(new system.windows.forms.menuitem[] { this.menutopfile}); // // menutopfile // this.menutopfile.index = 0; this.menutopfile.menuitems.addrange(new system.windows.forms.menuitem[] { this.menufileacquire, this.menufilesaveas, this.menufilesep1, this.menufileexit}); this.menutopfile.text = "&file"; // // menufileacquire // this.menufileacquire.index = 0; this.menufileacquire.text = "acquire..."; this.menufileacquire.click += new system.eventhandler(this.menufileacquire_click); // // menufilesaveas // this.menufilesaveas.enabled = false; this.menufilesaveas.index = 1; this.menufilesaveas.text = "save &as..."; this.menufilesaveas.click += new system.eventhandler(this.menufilesaveas_click); // // menufilesep1 // this.menufilesep1.index = 2; this.menufilesep1.text = "-"; // // menufileexit // this.menufileexit.index = 3; this.menufileexit.text = "e&xit"; this.menufileexit.click += new system.eventhandler(this.menufileexit_click); // // picturebox // this.picturebox.location = new system.drawing.point(8, 8); this.picturebox.name = "picturebox"; this.picturebox.size = new system.drawing.size(488, 357); this.picturebox.sizemode = system.windows.forms.pictureboxsizemode.autosize; this.picturebox.tabindex = 0; this.picturebox.tabstop = false; // // mainform // this.autoscalebasesize = new system.drawing.size(5, 13); this.autoscroll = true; this.backcolor = system.drawing.systemcolors.window; this.clientsize = new system.drawing.size(520, 377); this.controls.add(this.picturebox); this.icon = ((system.drawing.icon)(resources.getobject("$this.icon"))); this.menu = this.mainmenu; this.minimumsize = new system.drawing.size(256, 256); this.name = "mainform"; this.text = "scanning device"; ((system.componentmodel.isupportinitialize)(this.picturebox)).endinit(); this.resumelayout(false); this.performlayout(); } #endregion /// <summary> main entry point application. </summary> [stathread] static void main() { application.run(new mainform()); } private void menufileexit_click( object sender, system.eventargs e ) { close(); } private void menufileacquire_click( object sender, system.eventargs e ) { wiaclass wiamanager = null; // wia manager com object collectionclass wiadevs = null; // wia devices collection com object itemclass wiaroot = null; // wia root device com object collectionclass wiapics = null; // wia collection com object itemclass wiaitem = null; // wia image com object try { wiamanager = new wiaclass(); // create com instance of wia manager wiadevs = wiamanager.devices collectionclass; // call wia.devices devices if( (wiadevs == null) || (wiadevs.count == 0) ) { messagebox.show( this, "no wia devices found!", "wia", messageboxbuttons.ok, messageboxicon.stop ); application.exit(); return; } object selectusingui = system.reflection.missing.value; // = nothing wiaroot = (itemclass) wiamanager.create( ref selectusingui ); // let user select device if( wiaroot == null ) // nothing return; // call shows common wia dialog let user select picture: wiapics = wiaroot.getitemsfromui( wiaflag.singleimage, wiaintent.imagetypecolor ) collectionclass; if( wiapics == null ) return; bool takefirst = true; // sample uses 1 single picture foreach( object wiaobj in wiapics ) // enumerate pictures user selected { if( takefirst ) { disposeimage(); // remove previous picture wiaitem = (itemclass) marshal.createwrapperoftype( wiaobj, typeof(itemclass) ); imagefilename = path.gettempfilename(); // create temporary file image cursor.current = cursors.waitcursor; // take time this.refresh(); wiaitem.transfer( imagefilename, false ); // transfer picture our temporary file picturebox.image = image.fromfile( imagefilename ); // create image instance file menufilesaveas.enabled = true; // enable "save as" menu entry takefirst = false; // first , 1 done. } marshal.releasecomobject( wiaobj ); // release enumerated com object } } catch( exception ee ) { messagebox.show( this, "acquire wia imaging failed\r\n" + ee.message, "wia", messageboxbuttons.ok, messageboxicon.stop ); application.exit(); } { if( wiaitem != null ) marshal.releasecomobject( wiaitem ); // release wia image com object if( wiapics != null ) marshal.releasecomobject( wiapics ); // release wia collection com object if( wiaroot != null ) marshal.releasecomobject( wiaroot ); // release wia root device com object if( wiadevs != null ) marshal.releasecomobject( wiadevs ); // release wia devices collection com object if( wiamanager != null ) marshal.releasecomobject( wiamanager ); // release wia manager com object cursor.current = cursors.default; // restore cursor } } /// <summary> user selected menu entry save image. </summary> private void menufilesaveas_click( object sender, system.eventargs e ) { if( picturebox.image == null ) // no bitmap exists return; savefiledialog sd = new savefiledialog(); sd.title = "save image as..."; sd.filename = "temp.bmp"; sd.filter = "bitmap file (*.bmp)|*.bmp"; // bmp bitmap file format if( sd.showdialog() != dialogresult.ok ) return; picturebox.image.save( sd.filename ); // save file } /// <summary> remove image screen, dispose object , delete temporary file. </summary> private void disposeimage() { menufilesaveas.enabled = false; // disable "save as" menu entry image oldimg = picturebox.image; picturebox.image = null; // empty picture box if( oldimg != null ) oldimg.dispose(); // dispose old image (free memory, unlock file) if( imagefilename != null ) { // try delete temporary image file try { file.delete( imagefilename ); } catch( exception ) { } } } /// <summary> temporary image file. </summary> private string imagefilename; } }
probably best place to wire form_load
event handler, , there.
ok:
move code out of
menufileacquire_click
method definedprivate void acquire
call
acquire
menufileacquire_click
using visual studio, add event handler handle form's loaded event.
within method (most called
mainform_loaded
), callacquire
that way, same functionality called both acquire method, method triggered when form first loaded.
Comments
Post a Comment