Back to Devexpress

ImageValueConverter Class

xpo-devexpress-dot-xpo-dot-metadata.md

latest3.6 KB
Original Source

ImageValueConverter Class

A Value Converter which can be used to convert Image objects to an array of bytes.

Namespace : DevExpress.Xpo.Metadata

Assembly : DevExpress.Xpo.v25.2.dll

NuGet Package : DevExpress.Xpo

Declaration

csharp
public class ImageValueConverter :
    ValueConverter
vb
Public Class ImageValueConverter
    Inherits ValueConverter

Remarks

The following example shows how to convert the value of the Image property to an array of bytes when saving it in a data store.

csharp
using DevExpress.Xpo;

public class SampleImageClass : XPObject {
    private System.Drawing.Image image;
    public SampleImageClass(Session session) : base(session) {}

    [ValueConverter(typeof(DevExpress.Xpo.Metadata.ImageValueConverter))]
    public System.Drawing.Image Image {
        get { return image; }
        set {
            SetPropertyValue(nameof(Image) , ref image, value);
        }
    }
}
vb
Imports DevExpress.Xpo

Public Class SampleImageClass
    Inherits XPObject
    Private image_Renamed As System.Drawing.Image
    Public Sub New(ByVal session As Session)
        MyBase.New(session)
    End Sub

    <ValueConverter(GetType(DevExpress.Xpo.Metadata.ImageValueConverter))> _
    Public Property Image() As System.Drawing.Image
        Get
            Return image_Renamed
        End Get
        Set(ByVal value As System.Drawing.Image)
            SetPropertyValue(NameOf(Image), image_Renamed, value)
        End Set
    End Property
End Class

ImageValueConverter requires System.Drawing.Common 5.0+. In Xamarin/Mono projects, change your image property type from System.Drawing.Image to byte[].

csharp
using DevExpress.Xpo;

[Delayed]
public byte[] Image {
    get { return GetDelayedPropertyValue(nameof(Image)); }
    set { SetDelayedPropertyValue<byte[]>(nameof(Image), value); }
}
vb
Imports DevExpress.Xpo

<Delayed>
Public Property Image() As Byte()
    Get
        Return GetDelayedPropertyValue(NameOf(Image))
    End Get
    Set(ByVal value As Byte())
        SetDelayedPropertyValue(Of Byte())(NameOf(Image), value)
    End Set
End Property

Note

To run your application on Linux and macOS, install libgdiplus.

Ubuntu: sudo apt-get update -y && sudo apt-get install -y libgdiplus.

macOS: brew install mono-libgdiplus.

Inheritance

Object ValueConverter ImageValueConverter

See Also

ImageValueConverter Members

ValueConverterAttribute

Delayed Loading

How do I declare an image property in the ORM Data Model Designer

DevExpress.Xpo.Metadata Namespace