Display pictures in the datagridview control and precautions [Z]

Source: Internet
Author: User

Windows Forms programming has a datagridview control, which not only displays data, but also displays buttons, check boxes, and even images. These images can be stored in a database (in binary format) or a file system. The following is an example.

Suppose you have a WindowsProgramAnd there is a datagridview control on the form. This control has only one column (named image) and its type is datagridviewimagecolumn]

 

  Public   form1 () {initializecomponent (); getdata () ;}  private   void   getdata () {  foreach  ( string  S  in  directory. getfiles ( @ "  E: \ My Documents \ image favorites \ emoticons   ", "   *. GIF   "  )) {  This . demogrid. rows. add (s); ///   note, although the field type is image, we only pass it an image path, the real image reading is done by the following event handler  }} 

 

 ///   <Summary>  ///  This event is triggered when you try to format cells. Set the real image here  ///   </Summary>  ///   <Param name = "sender"> </param>  ///   <Param name = "E"> </param>  Private   Void Demogrid_cellformatting ( Object Sender, datagridviewcellformattingeventargs e ){  If (Demogrid. Columns [E. columnindex]. Name. Equals ( "  Image  "  )){  String Path = E. value. tostring (); E. Value = Getimage (PATH );}}  Public System. Drawing. Image getimage ( String  Path ){  Return System. Drawing. image. fromfile (PATH );} 

 

The aboveCodeThe purpose is to read all GIF images from a directory and display them on the datagridview. As shown in

However, loading images using the image. fromfile syntax has a major problem:The file is locked by our current process.. Not only can other programs use it, but if the current program needs to delete it (for example, if we want to delete a row of data while deleting the image), an error will be reported.

To solve this problem, you can modify the getimage method. As shown below

PublicSystem. Drawing. Image getimage (StringPath) {system. Io. filestream FS=NewSystem. Io. filestream (path, system. Io. filemode. Open); system. Drawing. Image result=System. Drawing. image. fromstream (FS); FS. Close ();ReturnResult ;}

 

Windows Forms programming has a datagridview control, which not only displays data, but also displays buttons, check boxes, and even images. These images can be stored in a database (in binary format) or a file system. The following is an example.

[Assume that you have a Windows program and the form has a dview control. This control has only one column (named image) and its type is datagridviewimagecolumn]

 

  Public   form1 () {initializecomponent (); getdata () ;}  private   void   getdata () {  foreach  ( string  S  in  directory. getfiles ( @ "  E: \ My Documents \ image favorites \ emoticons   ", "   *. GIF   "  )) {  This . demogrid. rows. add (s); ///   note, although the field type is image, we only pass it an image path, the real image reading is done by the following event handler  }} 

 

 ///   <Summary>  ///  This event is triggered when you try to format cells. Set the real image here  ///   </Summary>  ///   <Param name = "sender"> </param>  ///   <Param name = "E"> </param>  Private   Void Demogrid_cellformatting ( Object Sender, datagridviewcellformattingeventargs e ){  If (Demogrid. Columns [E. columnindex]. Name. Equals ( "  Image  "  )){  String Path = E. value. tostring (); E. Value = Getimage (PATH );}}  Public System. Drawing. Image getimage ( String  Path ){  Return System. Drawing. image. fromfile (PATH );} 

 

The above code can indeed achieve our goal, that is, to read all GIF images from a directory and display them on the datagridview. As shown in

However, loading images using the image. fromfile syntax has a major problem:The file is locked by our current process.. Not only can other programs use it, but if the current program needs to delete it (for example, if we want to delete a row of data while deleting the image), an error will be reported.

To solve this problem, you can modify the getimage method. As shown below

 
PublicSystem. Drawing. Image getimage (StringPath) {system. Io. filestream FS=NewSystem. Io. filestream (path, system. Io. filemode. Open); system. Drawing. Image result=System. Drawing. image. fromstream (FS); FS. Close ();ReturnResult ;}

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.