Add image in cell

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

    • Post Options:
    • Link

    Posted 10 September 2017, 10: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.

     

  • Posted 10 September 2017, 10: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:

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using GrapeCity.Windows.SpreadSheet.UI;
    using GrapeCity.Windows.SpreadSheet.Data;


    namespace SilverlightApplication37
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                this.gcSpreadSheet1.Sheets.Clear();
                this.gcSpreadSheet1.Sheets.Add(new MyWorksheet());
                var sheet = this.gcSpreadSheet1.ActiveSheet;
            }
        }


        public class ControlDrawingObject : CustomDrawingObject
        {
            private Control _rootElement;
            public ControlDrawingObject(int row, int col, Control control) : base(row, col) { _rootElement = control; this.ShowDrawingObjectOnly = true; }
            public override FrameworkElement RootElement
            {
                get { _rootElement.Margin = new Thickness(1); return _rootElement; }
            }
        }


        public class MyWorksheet : Worksheet
        {
            public bool DrawingObjectVisible { get; set; }
            public override DrawingObject[] GetDrawingObject(int row, int column, int rowCount, int columnCount)
            {
                if (row != 1 || column != 1) return base.GetDrawingObject(row, column, rowCount, columnCount);


                DrawingObject dobj;


                dobj = new ControlDrawingObject(row, column, new HyperlinkButton() { Content="This is a link", NavigateUri=new Uri("http://www.google.com") });


                return new DrawingObject[] { dobj };
            }
        }
    }


    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).

  • Posted 10 September 2017, 10: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; 

  • Posted 10 September 2017, 10: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.



  • Posted 10 September 2017, 10: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");

            }

        } 

  • Posted 18 November 2019, 9: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!

  • Posted 19 November 2019, 2:56 am 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