C # small tool for converting batch color images to black and white images (with source code)

Source: Internet
Author: User

Because the work needs to convert a large number of color images into black and white images, it takes some time to write one. For the convenience of the image, C # is used for development. The code is very simple, the Source Path, target path, overwrite Source Path, and progress bar are displayed. I have never studied C #, so the code is not very well written .. Hope everyone understands ---

(Note: because it is developed with vs2010, you need to install. Net framework4.0 to run it ..)

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; using system. io; namespace windowsformsapplication1 {public partial class form1: FORM {static ilist <string> Path = new list <string> (); // save your image name bitmap curbitmap = NULL; bitmap resbitmap = NULL; int COUNT = 0; Public form1 () {initializecomponent ();} private void sourcebtn_click (Object sender, eventargs e) {folderbrowserdialog dialog = new folderbrowserdialog (alog (); dialog. description = "select the file path"; if (dialog. showdialog () = dialogresult. OK) {sourcestr. TEXT = dialog. selectedpath ;}} private void resultbtn_click (Object sender, eventargs e) {folderbrowserdialog dialog = new folderbrowserdialog (); dialog. description = "select the file path"; if (dialog. showdialog () = dialogresult. OK) {resultstr. TEXT = dialog. selectedpath;} private void startbtn_click (Object sender, eventargs e) {directoryinfo di = new directoryinfo (sourcestr. text); // obtain the list of all files in this path. fileinfo [] fileinfo = di. getfiles (); Path. clear (); // start to get the image name foreach (fileinfo subinfo in fileinfo) {// determine whether the extension is the same if (subinfo. extension = ". jpg "| subinfo. extension = ". jpg "| subinfo. extension = ". JPEG "| subinfo. extension = ". JPEG ") {string strname = subinfo. name; // obtain the file name path. add (strname); // save the file name in the info. TEXT = "find" + path. count + "Images" ;}} if (path. count> 0) {progressbar1.value = 0; progressbar1.step = (INT) 100/path. count; new system. threading. thread (new system. threading. threadstart (starttransform )). start ();} else {MessageBox. show ("jpg image file not found") ;}} delegate void mydelegate (int param); void delegatemethod (int param) {total. TEXT = "processing" + (count + 1) + "/" + path. count + "Images"; if (count + 1) = path. count) {progressbar1.value = 100; total. TEXT = path. count + "image processing completed";} else {progressbar1.20.mstep () ;}} public void starttransform () {int IH, IW; string filename; For (COUNT = 0; count <path. count; count ++) {try {filename = path [count]; picturebox1.refresh (); image = image. fromfile (sourcestr. text + "\" + filename); curbitmap = new Bitmap (image); image. dispose (); picturebox1.image = curbitmap; IW = picturebox1.image. width; ih = picturebox1.image. height; resbitmap = togray (curbitmap, IW, IH); picturebox2.refresh (); picturebox2.image = resbitmap; If (iscover. checked) {picturebox2.image. save (sourcestr. text + "\" + filename); // overwrite the source file} else {picturebox2.image. save (resultstr. text + "\" + filename); // sets the Save path} // updates the UI interface this. invoke (New mydelegate (delegatemethod), count);} catch (system. exception ex) {MessageBox. show (ex. tostring () ;}} public bitmap togray (Bitmap BM, int IW, int IH) {color C = new color (); int T; // convert to grayscale image matrix for (Int J = 0; j <IH; j ++) {for (INT I = 0; I <IW; I ++) {c = BM. getpixel (I, j); t = (INT) (C. R + C. G + C. b)/3.0); BM. setpixel (I, j, color. fromargb (t, t, t) ;}} return bm;} private void iscover_checkedchanged (Object sender, eventargs e) {If (iscover. checked) {resultstr. enabled = false; resultbtn. enabled = false;} else {resultstr. enabled = true; resultbtn. enabled = true ;}}}}

Tools:

Http://download.csdn.net/detail/toss156/4300035If there is a problem with compression, You can decompress the file again ..

Http://download.csdn.net/detail/toss156/4303127

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.