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.v24.1.dll
NuGet Package: DevExpress.Win.Navigation
Declaration
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.
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) {
//...
}