Recently, I used the project. Set the Gridcontrol column to the Pictureedit type, and then use this column to display the picture. After trying to find the following two ways to be feasible.
Method One, know the path and name of the picture
For example: The path of the image is stored in the database (including: Local path, server path), then can be implemented by non-binding columns.
1. Create a non-binding column and set its corresponding properties, set the following properties:
FieldName set to Photo (the field name must be unique)
Unboundtype set to Unboundcolumntype.object
Columnedit is set to an instance of the Repositoryitempictureedit class (This action pictureedit the column's built-in editor)
2. Add the Customunboundcolumndata event for the GridView to populate the data for the unbound column.
Now that the setup is complete, how do you write the specific code? The specific code is as follows:
private void Gridview1_customunboundcolumndata (object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) {if (E.column.fieldname = = "Photo" &&am P E.isgetdata) {//refimage is the column that stores the path to the picture string FilePath = (String) ((DataRowView) e.ro W) ["Refimage"]; Image img = null; try {//determine if the picture path is a network path if (Urldiscern (FilePath)) { If the file exists if (remotefileexists (FilePath)) { Read file using (WebClient WC = new WebClient ()) { img = new Bitmap (WC. OpenRead (FilePath)); }}}//Determine if the local file exists else if (localfileexis TS (FilePath)) { Load local image img = Image.FromFile (filePath); }//pictureedit Column binding picture E.value = img; } catch (Exception ex) {MessageBox.Show (ex. ToString ()); }}}///<summary>//To determine if a remote file exists///</summary>//<param Name= "FILEURL" ></param>///<returns></returns> public bool Remotefileexists (string fil Eurl) {HttpWebRequest re = null; HttpWebResponse res = null; try {re = (HttpWebRequest) webrequest.create (FILEURL); res = (HttpWebResponse) re. GetResponse (); if (res. ContentLength! = 0) {//messagebox.show ("file exists"); return true; }} catch (Exception) {//messagebox.show ("No such file"); return false; } finally {if (re! = null) {Re. Abort ();//Destroy Close connection} if (res! = null) {Res. Close ();//Destroy Close response}} return false; }///<summary>//Determine if local file exists///</summary>//<param name= "path" ></pa ram>//<returns></returns> public bool Localfileexists (string filePath) { if (file.exists (FilePath)) {return true; } else {return false; }}///<summary>//Identify if URLSTR is a network path///</summary>//<param name= "ur Lstr "></param>///<returns></returns> public bool Urldiscern (string UrlSTR) {if (Regex.IsMatch (Urlstr, @ "((HTTP|FTP|HTTPS)://) (([a-za-z0-9\._-]+\.[ a-za-z]{2,6}) | ([0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3})) (: [0-9]{1,4}) * (/[a-za-z0-9\&%_\./-~-]*)?)) {return true; } else {return false; } }
If the picture shows a problem in the cell, you can adjust
Method Two, know the path and name of the picture
In addition to method one, we can also use the flow of the way to load the picture, that is, according to the picture path to the picture into a stream, and then directly bound to the repositoryitempictureedit column. You do not need to modify the binding type of the column at this time, only the fieldname of the column is the same as the name of the column in the byte[] stream in the data source .
If this binding is not valid, you can add a new column to the Gridcontrol data source (which is assumed to be a dataset here)
Ds. Tables[0]. Columns.Add ("Photo", System.Type.GetType ("system.byte[]"));
Then, load the picture into the photo column according to the path,
<pre name= "code" class= "HTML" > byte[] bb = pubfunc.getimagebyte (path, webClient); Ds. Tables[0]. rows[i]["Photo"] = BB;
The functions that may be used are as follows:
<summary>////byte[]///</summary>//<param name= "ImagePath" ></pa ram>//<param name= "webClient" ></param>///<returns></returns> public B Yte[] Getimagebyte (string imagePath) {byte[] imgbyte = null; try {if (Urldiscern (ImagePath)) {using (WebClient webclient=new WebClient ()) { Bitmap BT = new Bitmap (Webclient.openread (ImagePath)); Imgbyte = Pubfunc.imgtobyte (BT);} } else {using (FileStream files = new FileStream (ImagePath, Filemode.ope N) {imgbyte = new byte[files. Length]; Files. Read (imgbyte, 0, imgbyte.length); Files. Close (); }}} catch (Exception ee) {MessageBox.Show (EE. ToString ()); } return imgbyte; }
<summary>/// picture converted to byte stream///</summary>// <param name= "img" > Image object to convert </param > ///<returns> byte stream returned after conversion </returns> public byte[] imgtobyte (Image img) { try { using (MemoryStream ms = new MemoryStream ()) { byte[] imagedata = null; Img. Save (MS, System.Drawing.Imaging.ImageFormat.Jpeg); ImageData = Ms. GetBuffer (); return imagedata; } } catch (Exception ee) { MessageBox.Show (EE. ToString ()); return null; } }
Display a picture in a column of DevExpress Gridcontrol