商信互联
可以声明图片属性作为字节数组属性,或者作为一个参考属性的的MediaDataObject型(可用在商业类库)。
下面的示例说明了如何在Entity Framework Code-First类中实现图像属性。声明一个字节 数组属性,然后将ImageEditorAttribute应用于它。(可选)您可以使用属性的参数来自定义图像编辑器的行为。
using System.Drawing;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
// ...
[DefaultClassOptions]
public class Employee {
// ...
[ImageEditor(ListViewImageEditorMode = ImageEditorMode.PopupPictureEdit,
DetailViewImageEditorMode = ImageEditorMode.DropDownPictureEdit)]
public byte[] Photo { get; set; }
}
Imports System.Drawing
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations.Schema
' ...
<DefaultClassOptions>
Public Class Employee
' ...
Private _photo As Image
' The hidden property that is mapped to a database column
' and stores image data as a binary array.
Private privatePhotoData As Byte()
<Browsable(False)>
Public Property PhotoData() As Byte()
Get
Return privatePhotoData
End Get
Protected Set(ByVal value As Byte())
privatePhotoData = value
End Set
End Property
' The property that is visible in XAF UI, but is not mapped to a database column.
<NotMapped, _
ImageEditor(ListViewImageEditorMode := ImageEditorMode.PopupPictureEdit, _
DetailViewImageEditorMode := ImageEditorMode.DropDownPictureEdit)>
Public Property Photo() As Image
Get
If (_photo Is Nothing) AndAlso (PhotoData IsNot Nothing) Then
Dim imageConverter As New ImageConverter()
_photo = CType(imageConverter.ConvertFrom(PhotoData), Image)
End If
Return _photo
End Get
Set(ByVal value As Image)
_photo = value
If _photo Is Nothing Then
PhotoData = Nothing
Else
Dim imageConverter As New ImageConverter()
PhotoData = CType(imageConverter.ConvertTo(_photo, GetType(Byte())), Byte())
End If
End Set
End Property
End Class
有关上面代码中传递给ImageEditor属性的参数的详细信息,请参考ImageEditorAttribute主题。您还可以使用“模型编辑器”中的以下属性来指定图像选项:
当应用程序在列表视图中显示很多大图像时,它可能会消耗大量内存。对于具有许多同时连接的客户端的ASP.NET应用程序,这是最正确的。为了提高性能,可以使用延迟加载,如“如何在Entity Framework中启用图像的延迟加载”示例中所示。
下面的示例说明了如何在Entity Framework Code-First类中实现MediaDataObject类型的图像属性(在Business Class Library中提供)。WinForms和ASP.NET图像属性编辑器都自动用于MediaDataObject类型的属性,不需要属性(但是,您仍然可以应用ImageEditorAttribute来自定义编辑器的选项,因为上面已对字节数组进行了演示)。使用此类型可减少流量,因为图像被缓存在浏览器缓存中(与byte []类型的图像相比)。延迟加载始终用于MediaDataObject属性。
using DevExpress.ExpressApp;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.EF;
// ...
[DefaultClassOptions]
public class Contact {
[Browsable(false)]
public int ID { get; private set; }
public string Name { get; set; }
public virtual MediaDataObject Photo { get; set; }
}
Imports DevExpress.ExpressApp
Imports DevExpress.Persistent.Base
Imports DevExpress.Persistent.BaseImpl.EF
' ...
<DefaultClassOptions> _
Public Class Contact
Private privateID As Integer
<Browsable(False)> _
Public Property ID() As Integer
Get
Return privateID
End Get
Private Set(ByVal value As Integer)
privateID = value
End Set
End Property
Public Property Name() As String
Public Overridable Property Photo() As MediaDataObject
End Class