Have Image file path - Display column with image

23 posts, 0 answers
  1. Ahmuser
    Ahmuser avatar
    11 posts
    Member since:
    Jun 2010

    Posted 21 Jun 2010 Link to this post

    Hi,

    I have a database with the field that contains the filename of an image that I would like to display in am Rad GridViewImage Column.  

    - The databse itself does not contain the image just the filename.
    - The field only contains the filename - not the full path (all images are stored in a single directory)
    - The width of all the images are the same, however the height will vary.

    I need to load the image at runtime for every record that is displayed.

    I have searched everywhere, but cannot find how to achieve this.

    I am using VB.NET 2010

    Kindly assist.
  2. Geert
    Geert avatar
    14 posts
    Member since:
    Feb 2009

    Posted 22 Jun 2010 Link to this post

    I also need an answer tothis question.

    I have my gridview binded to a datasource. in my datasource there is a field which indicates if it is a folder or a file. Then I have tot show the correct icon in front of the row.

    Anyone knows which event I can use for this??

    Where is the RowDataBound event??
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Geert
    Geert avatar
    14 posts
    Member since:
    Feb 2009

    Posted 24 Jun 2010 Link to this post

    Anyone who knows the answer?
  5. Martin Vasilev
    Admin
    Martin Vasilev avatar
    1061 posts

    Posted 24 Jun 2010 Link to this post

    Hello folks,

    You can load an image to a RadGridView cell element by using a custom cell element and adding an ImagePrimitive inside. Please consider the following code snippet. It demonstrates how you can create a custom cell which loads an image from the path specified in the value of the cell:
    void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e)
    {
        if (e.Row is GridDataRowElement && e.Column is GridViewDataColumn &&
            ((GridViewDataColumn)e.Column).UniqueName == "pic")
        {
            e.CellType = typeof(CustomGridImageCellElement);
        }
    }    
    public class CustomGridImageCellElement : GridDataCellElement
    {
        ImagePrimitive _imagePrimitive;
        public CustomGridImageCellElement(GridViewDataColumn column, GridRowElement row)
            : base(column, row)
        {
        }
        public override void Initialize()
        {
            base.Initialize();
            _imagePrimitive = new ImagePrimitive();
            _imagePrimitive.Margin = new Padding(3);            
            this.Children.Add(_imagePrimitive);
            this.AutoSizeMode = RadAutoSizeMode.WrapAroundChildren;
        }
        protected override void SetContentCore(object value)
        {
            if (this.Value != null && this.Value != DBNull.Value)
            {
                _imagePrimitive.Image = Bitmap.FromFile(this.Value.ToString());
            }
            else
            {
                _imagePrimitive.Image = null;
            }
        }
    }

    I hope this helps. Let me know if you have any additional questions.

    Regards,
    Martin Vasilev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Carisch
    Carisch avatar
    25 posts
    Member since:
    Aug 2007

    Posted 08 Dec 2010 Link to this post

    I have a fully qualified url, instead of a file path.  Any recommendations?

    Thanks
  7. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 08 Dec 2010 Link to this post

    Hello Brian,

    You should be able to use the system above, but just get the image from a stream instead. E.g.

    Dim request As Net.HttpWebRequest = DirectCast(Net.HttpWebRequest.Create("http://www.server.com/image.jpg"), Net.HttpWebRequest)
    Dim response As Net.HttpWebResponse = DirectCast(request.GetResponse, Net.HttpWebResponse)
    Dim img As Image = Image.FromStream(response.GetResponseStream())

    hope that helps
    Richard
  8. Carisch
    Carisch avatar
    25 posts
    Member since:
    Aug 2007

    Posted 08 Dec 2010 Link to this post

    Thanks Richard I tried this in the mean time, and it worked, but I'm having difficulty getting the rows to size correctly

    WebClient wc = new WebClient();
    byte[] data = wc.DownloadData(value.ToString());
    MemoryStream ms = new MemoryStream(data);
    Bitmap bitmap = new Bitmap(ms);
    _imagePrimitive.Image = bitmap;

    protected override void SetContentCore(object value)
    {
        if (this.Value != null && this.Value != DBNull.Value && value.ToString().StartsWith("http://"))
        {
            WebClient wc = new WebClient();
            byte[] data = wc.DownloadData(value.ToString());
            MemoryStream ms = new MemoryStream(data);
            Bitmap bitmap = new Bitmap(ms);
            _imagePrimitive.Image = bitmap;
        }
        else
        {
            _imagePrimitive.Image = null;
        }
    }
  9. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 08 Dec 2010 Link to this post

    Hi Brian,

    Have you tried the following:

    this.radGridView1.AutoSizeRows = True;

    richard
  10. Carisch
    Carisch avatar
    25 posts
    Member since:
    Aug 2007

    Posted 08 Dec 2010 Link to this post

    I did, and there was no change to the appearance.    The last two lines did solve the height problem, but the width is still funky.  I think it has to do with the fact that I have set this on form load:
    radGridView1.MasterTemplate.BestFitColumns();

    WebClient webclient = new WebClient();
    byte[] data = webclient.DownloadData(value.ToString());
    MemoryStream stream = new MemoryStream(data);
    Bitmap bitmap = new Bitmap(stream);
    _imagePrimitive.Image = bitmap;
    RowInfo.Height = bitmap.Height;
    ColumnInfo.Width = bitmap.Width;
  11. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 08 Dec 2010 Link to this post

    Hi Brian,

    This will set the width of the column to the width of the last image. You need to set the width to the width of your widest image.
    Hope that helps
    Richard
  12. bob
    bob avatar
    1 posts
    Member since:
    Nov 2010

    Posted 09 Dec 2010 Link to this post

    I have inserted the provided code and applied what I believe to be the appropriate references. I am receiving an error on the base.Initialize(); line which says "void GridVirtualizationCellElement.Initialize(GridViewColumn column, GridRowElement Row)  Error: No overload for method 'Iniatalize' takes 0 arguments".

    The only change to the code was the ((GridViewDataColumn)e.Column).UniqueName  line to ((GridViewDataColumn)e.Column).Name

    What am I missing?
  13. Carisch
    Carisch avatar
    25 posts
    Member since:
    Aug 2007

    Posted 09 Dec 2010 Link to this post

    I tweaked to work with the latest release that I have.

    protected override void InitializeFields()
    {
        base.InitializeFields();
        _imagePrimitive = new ImagePrimitive();
        this.Children.Add(_imagePrimitive);
    }

    I also want to set a specific size, and this is what I came up with.  Would love to hear feedback if there is an easier way.

    protected override void SetContentCore(object value)
    {
        if (value != null && value != DBNull.Value)
        {
            HttpWebRequest request = HttpWebRequest.Create(value.ToString()) as HttpWebRequest;
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            Image img = Image.FromStream(response.GetResponseStream());
     
            Bitmap sized = new Bitmap(80, 115);
            Graphics g = Graphics.FromImage(sized);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(img, 0, 0, 80, 115);
            g.Dispose();
     
            _imagePrimitive.Image = sized;
            RowInfo.Height = sized.Height;
            ColumnInfo.Width = sized.Width;
        }
        else
        {
            _imagePrimitive.Image = null;
        }
    }
  14. David
    David avatar
    6 posts
    Member since:
    Jul 2011

    Posted 23 Dec 2013 Link to this post

    Martin,

    This is exactly what we are needing to do in our project, but it is in VB.net.  We have converted your sample using the code converter, but get errors on the Public Overrides Sub Initialize that say "sub Initialize cannot be declared Overrides because it does not override a sub in a base class".

    Is it possible to provide a code sample in VB to display an image to a RadGridView cell element by using a custom cell element and adding an ImagePrimitive inside?

    Thanks in advance.
  15. David
    David avatar
    6 posts
    Member since:
    Jul 2011

    Posted 23 Dec 2013 Link to this post

    Martin,

    This is exactly what we are needing to do in our project, but it is in VB.net.  We have converted your sample using the code converter, but get errors on the Public Overrides Sub Initialize that say "sub Initialize cannot be declared Overrides because it does not override a sub in a base class".

    Is it possible to provide a code sample in VB to display an image to a RadGridView cell element by using a custom cell element and adding an ImagePrimitive inside?

    Thanks in advance.
  16. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 24 Dec 2013 Link to this post

    Hi David,

    Here is the custom cell class in VB updated for the latest version of the suite:
    Public Class CustomGridImageCellElement
        Inherits GridDataCellElement
        Private _imagePrimitive As ImagePrimitive
        Public Sub New(column As GridViewDataColumn, row As GridRowElement)
            MyBase.New(column, row)
        End Sub
        Protected Overrides Sub CreateChildElements()
            MyBase.CreateChildElements()
            _imagePrimitive = New ImagePrimitive()
            _imagePrimitive.Margin = New Padding(3)
            Me.Children.Add(_imagePrimitive)
            Me.AutoSizeMode = RadAutoSizeMode.WrapAroundChildren
        End Sub
        Protected Overrides Sub SetContentCore(value As Object)
            If Me.Value IsNot Nothing AndAlso Not Me.Value.Equals(DBNull.Value) AndAlso File.Exists(Me.Value.ToString()) Then
                _imagePrimitive.Image = Bitmap.FromFile(Me.Value.ToString())
            Else
                _imagePrimitive.Image = Nothing
            End If
        End Sub
    End Class

    I hope this helps.

    Regards,
    Stefan
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  17. Vlad
    Vlad avatar
    12 posts
    Member since:
    Jun 2010

    Posted 06 Mar 2014 in reply to Richard Slade Link to this post

    Very long. Cell already contains URl of xml. Why go again to the server to download pictures? And if the path http :/ / server.com / bhbvhdhbvvbf.jpg and ordered picture' need? Again, view the entire xml file swing? And then how all compare in the table? Use Fildname does not work?
  18. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 07 Mar 2014 Link to this post

    Hi,

    FieldName will not work as in the data source there is just link to the image, not the image itself. I agree that there can be improvement in this approach to use the already downloaded images instead of getting them again. Feel free to post your suggestion.

    Regards,
    Stefan
    Telerik

    DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

  19. Vlad
    Vlad avatar
    12 posts
    Member since:
    Jun 2010

    Posted 07 Mar 2014 in reply to Stefan Link to this post

    I do not know. I'm thinking about it. Maybe try something RowFormating apply BitmapImage (URI) -> Imag From existing Cell?

    Offtop - 2014 vs 2013 scrteenshots WinForms.
  20. Vlad
    Vlad avatar
    12 posts
    Member since:
    Jun 2010

    Posted 07 Mar 2014 in reply to Stefan Link to this post

    I do not know. I'm thinking about it. Maybe try something RowFormating apply BitmapImage (URI) -> Imag From existing Cell?
  21. Vlad
    Vlad avatar
    12 posts
    Member since:
    Jun 2010

    Posted 07 Mar 2014 in reply to Stefan Link to this post

    In windows there Pitsture Pitsturboks boxing. "PicBox.Load (Response.Elements (" user "). Descendants (" photo_medium "). Value.ToString)" Loading precisely RLC. It's so impossible?
  22. Vlad
    Vlad avatar
    12 posts
    Member since:
    Jun 2010

    Posted 07 Mar 2014 in reply to Vlad Link to this post

    sorry *URL
  23. Vlad
    Vlad avatar
    12 posts
    Member since:
    Jun 2010

    Posted 07 Mar 2014 in reply to Stefan Link to this post

    Dim Response As XElement
                Response = XElement.Load("https://server.ru/method/users.get.xml?uids=" & strUsers & "&fields=status,counter,online,wall_comments,last_seen,activity,can_write_private_message,can_see_all_posts,can_post,contacts,screen_name,domain,sex,photo_medium,photo") '&access_token=" & VK_Online.VKOnline.TextBox1.Text)
                xmlUsers = XDocument.Parse(Response.ToString)
     
     
                Dim items = From item In xmlUsers.Descendants("user") Select New Users() With { _
                .No = n, _
                .UID = item.Element("uid"), _
                .Name = item.Element("first_name").Value & " " & item.Element("last_name").Value & vbCrLf & item.Element("uid").Value, _
                .Sex = item.Element("sex"), _
                .Data = Format(New DateTime(1970, 1, 1, 4, 0, 0).AddSeconds(item.Elements("last_seen").Descendants("time").Value), "dd/MM/yyyy") & vbCrLf & Format(New DateTime(1970, 1, 1, 4, 0, 0).AddSeconds(item.Elements("last_seen").Descendants("time").Value), "HH:mm:ss") & vbCrLf & "(OS/" & item.Elements("last_seen").Descendants("platform").Value & ")", _
                .Time = Format(New DateTime(1970, 1, 1, 4, 0, 0).AddSeconds(item.Elements("last_seen").Descendants("time").Value), "HH:mm:ss"), _
                .ET = "(" & DateDiff(DateInterval.Minute, Now, New DateTime(1970, 1, 1, 4, 0, 0).AddSeconds(item.Elements("last_seen").Descendants("time").Value)) + 15 & ")", _
                .Status = item.Element("online"), _
                .OS = item.Elements("last_seen").Descendants("platform").Value, _
                .App_ID = item.Element("online_app"), _
                .Mob = item.Element("online_mobile"), _
                .Del = item.Element("deactivated"), _
                .Photo = item.Element("photo").Value, _
                .Activity = item.Element("activity")}
                Dim contacts As New ObservableCollection(Of Users)()
                For Each contact As Users In items
                                PictureBox1.Load(contact.Photo)
                    contacts.Add(contact)
                Next contact
    Stupid way, but it is edtgstvenny works.
  24. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 11 Mar 2014 Link to this post

    Thank you for sharing this solution with the community. I have updated your Telerik Points for your time and efforts. 

    Regards,
    Stefan
    Telerik

    DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

Back to Top
UI for WinForms is Visual Studio 2017 Ready