This is a migrated thread and some comments may be shown as answers.

Gridview with Custom Cell Image

0 Answers 20 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Bao
Top achievements
Rank 1
Bao asked on 24 May 2018, 07:24 PM

Hi ,

I have a grid view with base 64 image from api. Now I am binding the base 64 image by the way:

private void Gridview_CreateCell(object sender, GridViewCreateCellEventArgs e)
        {
            if (e.Column.Index == 0 && e.Row.RowInfo is GridViewDataRowInfo)
            {
                e.CellElement = new CustomGridImageCellElement(e.Column, e.Row);
            }
        }

CustomGridImageCellElement.cs

 public class CustomGridImageCellElement : GridDataCellElement
    {
        ImagePrimitive _imagePrimitive;
        public CustomGridImageCellElement(GridViewColumn column, GridRowElement row)
            : base(column, row)
        {
        }


        public override void Initialize(GridViewColumn column, GridRowElement row)
        {
            //if (_imagePrimitive == null)
            //{
            _imagePrimitive = new ImagePrimitive();
            _imagePrimitive.Margin = new Padding(3);
            this.Children.Add(_imagePrimitive);
            this.AutoSizeMode = RadAutoSizeMode.WrapAroundChildren;
            //}         
            base.Initialize(column, row);
        }

 protected override void SetContentCore(object value)
        {

     if (this.Value != null && this.Value.ToString() != string.Empty
                && (this.Value.ToString().StartsWith("http") || this.Value.ToString().StartsWith("data:image/")) && _imagePrimitive.Image == null)
            {
                var imageData = this.Value.ToString();
                if (!imageData.StartsWith("http"))
                {
                    byte[] imageBytes = Convert.FromBase64String(imageData.Replace("data:image/JPEG;base64,", ""));
                    // Convert byte[] to Image
                    using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
                    {
                        _imagePrimitive.Image = System.Drawing.Image.FromStream(ms, true);
                    }
                }

 

It can display image now., but my problem is when scrolling up and down I see it run the custom cell again and it makes SetContentCore run and display error and slowly. How can I avoid it ?

No answers yet. Maybe you can help?

Tags
GridView
Asked by
Bao
Top achievements
Rank 1
Share this question
or