Back to Devexpress

RepositoryItemPictureEdit.ImageLoading Event

windowsforms-devexpress-dot-xtraeditors-dot-repository-dot-repositoryitempictureedit-66e727ae.md

latest6.6 KB
Original Source

RepositoryItemPictureEdit.ImageLoading Event

Allows you to execute actions when a user loads a file via the control’s context menu. You can handle this event to perform image preprocessing or to load images stored in unsupported formats.

Namespace : DevExpress.XtraEditors.Repository

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Events")]
public event ImageLoadEventHandler ImageLoading
vb
<DXCategory("Events")>
Public Event ImageLoading As ImageLoadEventHandler

Event Data

The ImageLoading event's data class is DevExpress.XtraEditors.Repository.SaveLoadImageEventArgs.

Remarks

A user can load and save images at runtime via the context menu’s Load and Save commands (see ShowMenu). These commands invoke the Open File and Save File dialogs.

After a user selects a file in these dialogs, the PictureEdit control fires the ImageLoading event (to load an image) and ImageSaving event (to save an image).

The ImageLoading and ImageSaving events allow you to pre-process images. You can also handle these events to load and save image formats not supported in .NET Framework.

The ImageLoading event fires after the user selects and confirms a file in the Open File dialog. This event has the following arguments:

  • FileName - Returns the full file name selected in the dialog.
  • Image - The PictureEdit tries to convert a selected image to a System.Drawing.Image object. The e.Image parameter initially contains the result of this conversion. If the automatic conversion fails, the parameter returns null. In this case, you can convert the selected image to a System.Drawing.Image object and assign the converted image to the e.Image parameter and set the e.Handled parameter to true.
  • Handled - Set this parameter to true to load your converted image ( e.Image ) to the PictureEdit control. Otherwise, the value of the e.Image parameter is ignored.

The ImageSaving event fires after the user confirms a file name in the Save File dialog. This event allows you to pre-process the image, convert it to a custom image format, and save it to the specified file.

Tip

Use the OpenFileDialogFilter and SaveFileDialogFilter properties to customize filters for the Open File and Save File dialogs.

The following example shows how to use the ImageLoading and ImageSaving events to convert images when they are loaded and saved.

csharp
private void Form1_Load(object sender, EventArgs e) {
    pictureEdit1.Properties.OpenFileDialogFilter = "WebP Files |*.webp|" + "Portable Network Graphics Format (*.png)|*.png";
    pictureEdit1.Properties.SaveFileDialogFilter = pictureEdit1.Properties.OpenFileDialogFilter;
}

private void pictureEdit1_Properties_ImageLoading(object sender, DevExpress.XtraEditors.Repository.SaveLoadImageEventArgs e) {
    FileInfo fi = new FileInfo(e.FileName);
    string fileExtension = fi.Extension.ToLower();
    if (fileExtension == ".webp") {
        e.Image = CustomConvertFromWebP(e.FileName);
        e.Handled = true;
    }
}

private void pictureEdit1_Properties_ImageSaving(object sender, DevExpress.XtraEditors.Repository.SaveLoadImageEventArgs e) {
    FileInfo fi = new FileInfo(e.FileName);
    string fileExtension = fi.Extension.ToLower();
    if (fileExtension == ".webp") {
        CustomSaveToWebP(e.Image, e.FileName);
        e.Handled = true;
    }
}

private Image CustomConvertFromWebP(string fileName) {
    //...
}

private void CustomSaveToWebP(Image image, string fileName) {
    //...
}
vb
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    PictureEdit1.Properties.OpenFileDialogFilter = "WebP Files |*.webp|" & "Portable Network Graphics Format (*.png)|*.png"
    PictureEdit1.Properties.SaveFileDialogFilter = PictureEdit1.Properties.OpenFileDialogFilter
End Sub

Private Sub PictureEdit1_Properties_ImageLoading(sender As Object, e As DevExpress.XtraEditors.Repository.SaveLoadImageEventArgs) Handles PictureEdit1.Properties.ImageLoading
    Dim fi As FileInfo = New FileInfo(e.FileName)
    Dim fileExtension As String = fi.Extension.ToLower()
    If fileExtension = ".webp" Then
        e.Image = CustomConvertFromWebP(e.FileName)
        e.Handled = True
    End If
End Sub

Private Sub PictureEdit1_Properties_ImageSaving(sender As Object, e As DevExpress.XtraEditors.Repository.SaveLoadImageEventArgs) Handles PictureEdit1.Properties.ImageSaving
    Dim fi As FileInfo = New FileInfo(e.FileName)
    Dim fileExtension As String = fi.Extension.ToLower()
    If fileExtension = ".webp" Then
        CustomSaveToWebP(e.Image, e.FileName)
        e.Handled = True
    End If
End Sub

Private Function CustomConvertFromWebP(ByVal fileName As String) As Image
    '...
End Function

Private Sub CustomSaveToWebP(ByVal image As Image, ByVal fileName As String)
    '...
End Sub

See Also

ImageSaving

OpenFileDialogFilter

SaveFileDialogFilter

RepositoryItemPictureEdit Class

RepositoryItemPictureEdit Members

DevExpress.XtraEditors.Repository Namespace