Ticket T258546
Visible to All Users

SingleChoiceAction - Set Image to value from database

created 10 years ago

I have a SingleChoiceAction which I'd like to use to set the status of the selected objects in a view.
The possible status values come from a lookup table, which also contains a SmallIcon property which I'd like to use for the image of the items but the ChoiceActionItem only has an ImageName property to use an Embedded Resource.
How do I use the images from the database instead? Putting the images as Embedded Resources defeats the object of storing them in the database - any new values would require the addition of a new Embedded Resource, a programming change.

Dim objectspace As IObjectSpace = objView.ObjectSpace

SetDataAction.Items.Clear()

setStatusItem = New ChoiceActionItem("Status", "Status", Nothing)
SetDataAction.Items.Add(setStatusItem)

For Each ps As ProspectStatus In objectspace.GetObjects(Of ProspectStatus)(New BinaryOperator("Withdrawn", False)).OrderBy(Function(ps1) ps1.DisplayOrder)
Dim item As New ChoiceActionItem(ps.Description, ps)
   'item.ImageName = ImageLoader.Instance…
  setStatusItem.Items.Add(item)
Next

Answers approved by DevExpress Support

created 10 years ago (modified 9 years ago)

Hello Simon,

To accomplish this task, handle the ImageLoader.CustomGetImageInfo Event and provide a custom ImageInfo object based on the specified name.
For instance, you can write the following code in the Program.xx file of YourSolutionName.Win project (this approach is for WinForms apps):

C#
namespace MainDemo.Win { public class Program { static void ImageLoader_CustomGetImageInfo(object sender, CustomGetImageInfoEventArgs e) { if (e.ImageName.Contains("Save")) { e.ImageInfo = new ImageInfo(e.ImageName, System.Drawing.Image.FromFile("D:\\screenshot.png"), ""); e.Handled = true; } } [STAThread] public static void Main(string[] arguments) { ImageLoader.CustomGetImageInfo += ImageLoader_CustomGetImageInfo; } } }
Visual Basic
Namespace MainDemo.Win Public Class Program Private Shared Sub ImageLoader_CustomGetImageInfo(ByVal sender As Object, ByVal e As CustomGetImageInfoEventArgs) If e.ImageName.Contains("Save") Then e.ImageInfo = New ImageInfo(e.ImageName, System.Drawing.Image.FromFile("D:\screenshot.png"), "") e.Handled = True End If End Sub <STAThread> Public Shared Sub Main(ByVal arguments() As String) AddHandler ImageLoader.CustomGetImageInfo, AddressOf ImageLoader_CustomGetImageInfo End Sub End Class End Namespace

I hope you find this information helpful.

    Show previous comments (1)
    Dennis Garavsky (DevExpress) 10 years ago

      I am happy to hear of your result. One correction, though: use the XafApplication.CreateObjectSpace method to create an IObjectSpace object and use it for data manipulations instead of Session you created, which is wrong. Refer to the eXpressApp Framework > Concepts > Data Manipulation and Business Logic article for more details.

        Thanks again.
        I amended my code as you suggested and it works fine.
           Private Shared Sub ImageLoader_CustomGetImageInfo(sender As Object, e As CustomGetImageInfoEventArgs)
               If e.ImageName.StartsWith("ProspectStatus") Then
                   Dim objObjectSpace As IObjectSpace = m_application.CreateObjectSpace()
                   Dim intID As Integer
                   If e.ImageName.Contains("_") Then
                       intID = e.ImageName.Substring(15, e.ImageName.IndexOf("_") - 15)
                   Else
                       intID = e.ImageName.Substring(15)
                   End If
                   Dim objProspectStatus As ProspectStatus = objObjectSpace.FindObject(Of ProspectStatus)(CriteriaOperator.Parse("ProspectStatusID=" & intID))
                   Dim objImage As Image = Prospera2.Module.CommonFunctions.ByteToImage(objProspectStatus.SmallIcon)
                   e.ImageInfo = New ImageInfo(e.ImageName, objImage, "")
                   e.Handled = True
               End If

        Dennis Garavsky (DevExpress) 10 years ago

          Well done, Simon!

          Other Answers

          created 9 years ago (modified 9 years ago)

          Hi guys,

          I tried this solution in XAF Web application and of course it doesn't work directly.
          Actually, You should do a little bit more to work this solution for the XAF Web part.

          Workaround Solution:
          You must save image to somewhere (preferring under web site folder) and you must assign URL part in new ImageInfo(…).
          I want to share my web part workaround solution:

          C#
          // Loading Image To Memory Stream from Image of Business Object Item using (System.IO.MemoryStream imagestream = new System.IO.MemoryStream(tt.Marker.Image)) { // Create image from Memory Stream Image marker = System.Drawing.Image.FromStream(imagestream); // Set current direct to somewhere, I choose Root Directory of Web Application System.Reflection.Module mod = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]; System.IO.Directory.SetCurrentDirectory(System.IO.Directory.GetParent(mod.Assembly.CodeBase.Substring(8)).Parent.FullName); // Check&Create a Special Folder for the images if (!System.IO.Directory.Exists("images/Markers")) System.IO.Directory.CreateDirectory("images/Markers"); // Check&Create Image if (!System.IO.File.Exists("images/Markers/" + e.ImageName + ".jpg")) marker.Save("images/Markers/" + e.ImageName + ".jpg"); // Important: Handle new ImageInfo with URL parameter, not with empty string e.ImageInfo = new ImageInfo(e.ImageName, marker, "/images/Markers/" + e.ImageName + ".jpg"); e.Handled = true; // Set Current Directory to /bin folder my other special operations, // You may consider this for your other file system operations in Application System.IO.Directory.SetCurrentDirectory(System.IO.Directory.GetParent(mod.Assembly.CodeBase.Substring(8)).FullName); }

          Best Regards
          Mert Essiz

            Show previous comments (1)
            DevExpress Support Team 9 years ago

              I have investigated this scenario and found that the ImageLoader.CustomGetImageInfo event was not designed to work in ASP.NET applications and currently it requires additional code like your code. We will see how to improve this situation. Thank you for your feedback. I greatly appreciate your cooperation.

                You are always welcome :)

                DevExpress Support Team 9 years ago

                  I have changed our code to avoid additional code snippets when a custom image is provided using the ImageLoader.CustomGetImageInfo event.
                  This change will be included into the next update. Please see Avoid additional code when a custom image is provided through the ImageLoader.CustomGetImageInfo event in ASP.NET applications.

                  Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

                  Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.