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 ;}