Add image in cell

Posted by: pragati-sd-fp on 10 September 2017, 1:56 am EST

  • Posted 10 September 2017, 1:56 am EST

    Hello,
    I'm trying to add the image in cell, with code - 
                Image objImg = new Image();
                BitmapImage imgSource = null;
                Uri uri = null;
                uri = new Uri("../images/bluearr.png", UriKind.RelativeOrAbsolute);
                imgSource = new BitmapImage(uri);
                objImg.Source = imgSource;
                sheet.SetValue(11, 6, objImg);
    But, its not showing the image. Is this the right way, to show image in cell?

    Please, help.

    Thanks,
    Pragati.


     

  • Replied 10 September 2017, 1:56 am EST

    Hi,

    Spread does not support setting the image as a value like that.  It does support adding a drawing object like this:

    <div style="font-family:arial, sans-serif;"><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">using System;</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">using System.Windows;</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">using System.Windows.Controls;</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">using GrapeCity.Windows.SpreadSheet.UI;</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">using GrapeCity.Windows.SpreadSheet.Data;</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">
    </div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">namespace SilverlightApplication37</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">{</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">    public partial class MainPage : UserControl</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">    {</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">        public MainPage()</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">        {</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">            InitializeComponent();</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">            this.gcSpreadSheet1.Sheets.Clear();</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">            this.gcSpreadSheet1.Sheets.Add(new MyWorksheet());</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">            var sheet = this.gcSpreadSheet1.ActiveSheet;</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">        }</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">    }</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">
    </div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">    public class ControlDrawingObject : CustomDrawingObject</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">    {</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">        private Control _rootElement;</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">        public ControlDrawingObject(int row, int col, Control control) : base(row, col) { _rootElement = control; this.ShowDrawingObjectOnly = true; }</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">        public override FrameworkElement RootElement</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">        {</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">            get { _rootElement.Margin = new Thickness(1); return _rootElement; }</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">        }</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">    }</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">
    </div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">    public class MyWorksheet : Worksheet</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">    {</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">        public bool DrawingObjectVisible { get; set; }</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;"><div>        public override <span class="il" style="background-position:initial initial;background-repeat:initial initial;">DrawingObject</span>[] GetDrawingObject(int row, int column, int rowCount, int columnCount)</div><div>        {</div></div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">            if (row != 1 || column != 1) return base.GetDrawingObject(row, column, rowCount, columnCount);</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">
    </div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">            <span class="il" style="background-position:initial initial;background-repeat:initial initial;">DrawingObject</span> dobj;</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">
    </div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">            dobj = new ControlDrawingObject(row, column, new HyperlinkButton() { Content="This is a link", NavigateUri=new Uri("http://www.google.com") });</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">
    </div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">            return new <span class="il" style="background-position:initial initial;background-repeat:initial initial;">DrawingObject</span>[] { dobj };</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">        }</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">    }</div><div style="font-family:Helvetica, 'Helvetica Nueue';font-size:16px;">}</div></div>

    So you should be able to replace the code for creating the ControlDrawingObject to use your image instead of the HyperlinkButton.

    The next release of Spread WPF-Silverlight will have a new feature that will make this possible without the inherited MyWorksheet class (i.e. a new API for setting the drawing object).

  • Replied 10 September 2017, 1:56 am EST

    Or you can use ImageBrush to show an image as background on a cell.

                ImageBrush ib = new ImageBrush(

                    new BitmapImage(

                        //new Uri(@"images\Chrysanthemum.jpg", UriKind.Relative)

                        new Uri("images/Chrysanthemum.jpg", UriKind.RelativeOrAbsolute)

                    )

                );

                this.gcSpreadSheet1.ActiveSheet[1, 1].Background = ib; 

  • Replied 10 September 2017, 1:56 am EST

    Hello seanl,
    Thanks for your reply.
    I tried the code snippet. But, its not working for me. Will you plz send the sample aplication on my email ID - pragati.dukale@revalanalytics.com

    Hello ericpeng,
    Thanks for your reply. The code is working, but how can I set that image as a hyperlink?

    Please, help.

    Thanks,
    Pragati.

  • Replied 10 September 2017, 1:56 am EST

    use follow code to simulate a hyperlink image

            public MainWindow()

            {

                InitializeComponent();


                ImageBrush ib = new ImageBrush(

                    new BitmapImage(

                        //new Uri(@"images\Chrysanthemum.jpg", UriKind.Relative)

                        new Uri("images/Chrysanthemum.jpg", UriKind.RelativeOrAbsolute)

                    )

                );

                this.gcSpreadSheet1.ActiveSheet[1, 1].Background = ib;

                this.gcSpreadSheet1.CellClick += gcSpreadSheet1_CellClick;

                this.gcSpreadSheet1.MouseMove += gcSpreadSheet1_MouseMove;


            }


            void gcSpreadSheet1_MouseMove(object sender, MouseEventArgs e)

            {

                Point p = e.GetPosition(this.gcSpreadSheet1);

                var vi = this.gcSpreadSheet1.HitTest(p.X, p.Y).ViewportInfo;

                if (vi != null)

                {

                    if (vi.Row == 1 && vi.Column == 1)

                        this.gcSpreadSheet1.Cursor = Cursors.Hand;

                    else

                        this.gcSpreadSheet1.Cursor = Cursors.Arrow;

                }

            }


            void gcSpreadSheet1_CellClick(object sender, GrapeCity.Windows.SpreadSheet.UI.CellClickEventArgs e)

            {

                if(e.Row==1 && e.Column==1)

                    Process.Start("http://www.google.com");

            }

        } 

  • Replied 18 November 2019, 12:16 am EST

    Hello Grapecity,
    you said "The next release of Spread WPF-Silverlight will have a new feature that will make this possible without the inherited MyWorksheet class (i.e. a new API for setting the drawing object)".
    Now in November 2019 is it already supported? And how?
    Thanks!
  • Marked as Answer

    Replied 18 November 2019, 5:56 pm EST

    Hello,

    Please use the following "AddPicture" method to add a picture in sheet/cell:

    https://www.grapecity.com/spreadnet/docs/v13/online-wpf/GrapeCity.WPF.SpreadSheet.Data~GrapeCity.Windows.SpreadSheet.Data.Worksheet~AddPicture.html

    Thanks,
    Mohit
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels