Search code examples
.netvb.netwinformsinfragistics

Image.FromFile is very very slow


I am trying to load image icon in infragistics ultrawingrid. For this i am trying to intialize row and based on condition i am loading respective image.

But I have very slow performance when I am trying to use Image.FromFile()

Please suggest how to fix for faster loading of grid

    Private Sub ugMain_InitializeRow(sender As Object, e As InitializeRowEventArgs) Handles ugMain.InitializeRow
    If e.Row.Cells("Delays").Value = 0 Then
        e.Row.Cells("Indicator").Value = Image.FromFile("Images\\RoundedGreen.png")
    Else
        e.Row.Cells("Indicator").Value = Image.FromFile("Images\\RoundedRed.png")

    End If
End Sub

Solution

  • Image.FromFile is not slow, but the way you are calling it for each record, may cause some performance penalty for you. You are calling that method which interact with filesystem for each row.

    Instead of loading those images from file system for each row in your grid, you can use either of these options:

    • You can load those images once and put them in a List<Image> and then each time you need them, get the image from list by index, Images[0] for example. If you want to access those images by key, you can use a Dictionary<string , Image> as mentioned by Plutonix and then you can get the image using Images["RoundedGreen"] for example.

    • Instead of relying on images on file system, You can use Resources.Resx file of your application to store those images at compile time. Then each time you need to use those images at run-time, you can find them using My.Resources.RoundedGreen.

    Example

    For example you can define Images member at form level:

    Dim Images As Dictionary(Of String, Image) = New Dictionary(Of String, Image)()
    

    Then somewhere, for example in Load event of form, load images:

    Images.Add("RoundedGreen", Image.FromFile("Path to RoundedGreen image"))
    Images.Add("RoundedRed", Image.FromFile("Path to RoundedRed image"))
    

    Then wherever you need to use those images, you can get them from Images member this way: Images("RoundedGreen")