WCF Development Framework Formation journey--upload and save personal image information

Source: Internet
Author: User

WCF Development Framework Formation journey--upload and save personal image information

Http://www.cnblogs.com/wuhuacong/archive/2011/12/23/2299614.html

Generally in the business system, in addition to storing personal basic information, may also need to store some of the personal picture information, usually such as portraits, business cards, ID cards and other important picture information, and these information is small in order to facilitate management, generally also with personal basic information in the database.

I in the development of the formation of their own WinForm development framework and the WCF development framework process, the optimization of these, is now published and we discuss the study, hope to provide you with a reference, you have a further promotion. This article mainly takes the personal picture information upload and save under the WCF development frame as the topic, introduces some knowledge points and operations involved, and avoids some common problems.

1) First, we need to set up several Image object fields in the database (I'm using SQL Server database).

2) We need to do the database storage of the underlying operation function, the picture information stored in different fields, because this operation is similar, so set an enumeration to select a different field storage, as shown below.

1 /// <summary>2         ///get the corresponding field name based on the picture enumeration type3         /// </summary>4         /// <param name= "ImageType" >Image Enumeration Type</param>5         /// <returns></returns>6         Private stringGetfieldnamebyimagetype (userimagetype imageType)7         {8             stringFieldName ="Portrait";9             Switch(ImageType)Ten             { One                  CaseUserimagetype. Personal portrait: AFieldName ="Portrait"; -                      Break; -                  Caseuserimagetype. ID Card photo 1: theFieldName ="IDPhoto1"; -                      Break; -                  Caseuserimagetype. ID Card Photo 2: -FieldName ="IDPhoto2"; +                      Break; -                  Caseuserimagetype. Business Card 1: +FieldName ="BusinessCard1"; A                      Break; at                  Caseuserimagetype. Business Card 2: -FieldName ="BusinessCard2"; -                      Break; -             } -             returnFieldName; -         } in  -  to         /// <summary> +         ///Update personal image data -         /// </summary> the         /// <param name= "imagetype" >type of picture</param> *         /// <param name= "UserId" >User ID</param> $         /// <param name= "Imagebytes" >Picture byte array</param>Panax Notoginseng         /// <returns></returns> -          Public BOOLUpdatepersonimagebytes (Userimagetype imagetype,stringUseridbyte[] imagebytes) the         { +             stringFieldName =Getfieldnamebyimagetype (imagetype); A  the             stringsql =string. Format ("Update Users set {0}[email protected] where Id = ' {1} '", FieldName, userId); +Database db =databasefactory.createdatabase (); -DbCommand DbCommand =db. Getsqlstringcommand (SQL); $Db. Addinparameter (DbCommand,"Image", Dbtype.binary, imagebytes); $             returnDb. ExecuteNonQuery (DbCommand) >0;
}

3) The above is the operation of saving pictures, also need to do a generic type of picture download operation, the user picture information stored in a byte array, convenient for the client to convert bytes into specific file bytes.

1 ///get picture data based on a personal picture enumeration type2         /// </summary>3         /// <param name= "imagetype" >Image Enumeration Type</param>4         /// <returns></returns>5          Public byte[] Getpersonimagebytes (Userimagetype imagetype,stringuserId)6         {7             stringFieldName =Getfieldnamebyimagetype (imagetype);8 9             stringsql =string. Format ("Select {0} from Users where Id = ' {1} '", FieldName, userId);TenDatabase db =databasefactory.createdatabase (); OneDbCommand DbCommand =db. Getsqlstringcommand (SQL); A  -             byte[] Imagebytes =NULL; -             using(IDataReader reader =db. ExecuteReader (DbCommand)) the             { -                 if(reader. Read ()) -                 { -Imagebytes = (reader. IsDBNull (reader. GetOrdinal (fieldName))?NULL: (byte[]) reader[0]; +                 } -             } +  A             returnimagebytes; at}

4) Then design a picture to upload the displayed form, where the form of the picture control shows an alternative picture by default, a beautiful, two also user-friendly quick set picture, as shown below.

The final effect as shown below, in addition to the picture can be displayed, double-click to preview the contents of the picture, so that users zoom in and zoom out to see the picture.

5) The WCF client code that implements the image upload is shown below.

1 Private voidBtnsaveportrait_click (Objectsender, EventArgs e)2         {3             if(Picportrait.image! =NULL)4             {5                 NewUserserviceclient (). Using (client =6                 {7                     Try8                     {9                         byte[] imagebytes = Imagehelper.imagetobytes ( This. picportrait.image);Ten                         BOOLsucess =client. Updatepersonimagebytes (userimagetype. Personal portrait, One Portal.gc.LoginInfo.Id, imagebytes); AMessagedxutil.showtips (sucess?"Personal portrait picture saved successfully! ":"Save failed! "); -                     } -                     Catch(Exception ex) the                     { - Messagedxutil.showerror (ex. Message); - Logtexthelper.error (ex); -                     } +                 }); -             } +}

The code to reset the picture is shown below.

1 Private voidresetdefaultimage (userimagetype imageType)2         {3System.ComponentModel.ComponentResourceManager resources =NewSystem.ComponentModel.ComponentResourceManager (typeof(Frmpersonalinfo));4             Switch(ImageType)5             {6                  CaseUserimagetype. Personal portrait:7                      This. Picportrait.editvalue = ((Object) (Resources. GetObject ("Picportrait.editvalue")));8                      Break;9                  Caseuserimagetype. ID Card photo 1:Ten                      This. Picidcard1.editvalue = ((Object) (Resources. GetObject ("Picidcard1.editvalue"))); One                      Break; A                  Caseuserimagetype. ID Card Photo 2: -                      This. Picidcard2.editvalue = ((Object) (Resources. GetObject ("Picidcard2.editvalue"))); -                      Break; the                  Caseuserimagetype. Business Card 1: -                      This. Piccard1.editvalue = ((Object) (Resources. GetObject ("Piccard1.editvalue"))); -                      Break; -                  Caseuserimagetype. Business Card 2: +                      This. Piccard2.editvalue = ((Object) (Resources. GetObject ("Piccard2.editvalue"))); -                      Break; +             } A}

Because the enumeration type Userimagetype is used to distinguish different picture information, the uploading, displaying and resetting of multiple images are basically the same, and the reuse of code is better realized. Also noteworthy is that WCF does not support a larger picture upload by default, generally need to set up a configuration file to achieve image data upload (general picture is still a bit large), so you need to set the server and client configuration files, as shown below.

The Web. config configuration file for the server is shown below

The client configuration is shown below.

WCF Development Framework Formation journey--upload and save personal image information

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.