Use winform to upload images for web projects

Source: Internet
Author: User

There is a requirement in the project: Use winform to maintain the data on the web server. images must be added to the project and the mobile phone must be considered for image uploading, to compress the image and put it in another folder, it is called by the mobile phone to relieve the excessive data volume burden on the mobile phone. The implementation logic is as follows: 1. use webservice as the winform data layer to maintain web server data; 2. the following code is added to webservice and called by winform: # region --- upload an image --- // <summary> /// </summary> // <param name = "content"> </param> /// <param name = "pathandname"> </param> // <returns> </returns> [WebMethod] public int UpLoadFile (byte [] content, string pathandname) {int result =-2; //-2, unknown error,-1 Upload Failed, 1 Web image uploaded successfully, but mobile image uploaded not successfully, 2. Both Web and mobile images are uploaded successfully. Try {string phonePicturePath = ConfigurationManager. appSettings ["phonePicturePath"]; int percentage = Convert. toInt32 (ConfigurationManager. appSettings ["percentage"]); int index = pathandname. lastIndexOf (". "); if (index = 0) {result =-1;} else {string extended = string. empty; if (index + 1 = pathandname. length) {result =-1;} else {extended = pathandname. substring (index + 1); if (extended = "Jpeg" | extended = "gif" | extended = "jpg" | extended = "png") {try {File. writeAllBytes (Server. mapPath (pathandname), content); // successful web upload result = 1; phonePicturePath + = pathandname. substring (pathandname. lastIndexOf ('/'); if (GetPicThumbnail (pathandname, phonePicturePath, percentage) // All uploaded successfully {result = 2 ;}} catch (Exception ex) {result =-1 ;}} else {result =-1 ;}}} catch (Exception ex) {result =-1;} return result ;} /// <summary> /// compress the image /// </summary> /// <param name = "sFile"> file path </param> /// <param name = "outPath"> storage path </param> /// <param name = "flag"> compression ratio </param> /// <returns> Boolean </returns> [WebMethod] public bool GetPicThumbnail (string sFile, string outPath, int flag) {System. drawing. image iSource = System. drawing. image. fromFile (Server. mapPath (sFile); Imag EFormat tFormat = iSource. rawFormat; // The following Code sets the compression quality EncoderParameters ep = new EncoderParameters (); long [] qy = new long [1]; qy [0] = flag; // set the compression ratio to 1-100 EncoderParameter eParam = new EncoderParameter (System. drawing. imaging. encoder. quality, qy); ep. param [0] = eParam; try {ImageCodecInfo [] arrayICI = ImageCodecInfo. getImageEncoders (); ImageCodecInfo effeciciinfo = null; for (int x = 0; x <arra YICI. length; x ++) {if (arrayICI [x]. formatDescription. equals ("JPEG") {policiciinfo = arrayICI [x]; break ;}} if (policiciinfo! = Null) {iSource. save (Server. mapPath (outPath), jpegICIinfo, ep); // dFile is the new path after compression} else {iSource. save (outPath, tFormat);} return true;} catch {return false;} finally {iSource. dispose (); iSource. dispose () ;}# endregion 3. then the winform code is as follows: private void btnCommonFile_Click (object sender, EventArgs e) {OpenFileDialog fileDialog = new OpenFileDialog (); if (fileDialog. showDialog () = DialogResult. O K) {string extension = Path. getExtension (fileDialog. fileName); string [] str = new string [] {". gif ",". jpeg ",". jpg "," png "}; if (! Str. Contains (extension) {MessageBox. Show ("only images in gif, jpeg, jpg, and png formats can be uploaded! ");} Else {FileInfo fileInfo = new FileInfo (fileDialog. fileName); if (fileInfo. length> (FILE_SIZE * 1024) * 1024) {MessageBox. show ("the uploaded image cannot exceed 5 MB");} else {Stream file = fileDialog. openFile (); byte [] bytes = new byte [file. length]; file. read (bytes, 0, bytes. length); DateTime time = DateTime. now; // rename the image name and path www.2cto.com string path_name = "/images/" + time. toString ("yyyyMMddHHmmss") + extensi On; if (tb. UploadFile (bytes, path_name, extension )! = "-1") {MessageBox. Show ("upload function! "); This.txt picture. Text = path_name ;}}} 4. Now all work is done.

Related Article

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.