We have the RadImageGallery implemented on page. The code behind loads up one or more images by reading in an array of bytes from each file. Occasionally, an exception is thrown when one of those images is invalid (corrupt or some other issue). OK, that's fine, but we would like to trap the exception and handle the error in a particular way. But... we don't seem to be able to trap it other than in our last-chance global exception handler.
The binary data is created in the NeedDataSource event, and that returns successfully. I've added other events and set breakpoints in each of them, and they all return ok. So, is there an event we can handle that would be appropriate for a simple try/catch block?
Pretty simple markup for the RadImageGallery control (note, all the events after the OnNeedDataSource were added simply to test out if there was a place we could try/catch the exception, but no exception was thrown in those events):
<telerik:RadImageGallery ID="galleryImages" runat="server" RenderMode="Lightweight"
DisplayAreaMode="Image" LoopItems="true" DataImageField="ImageBytes" DataDescriptionField="Description" DataTitleField="Title"
OnNeedDataSource="galleryImages_NeedDataSource"
OnDataBinding="galleryImages_DataBinding"
OnImageRequested="galleryImages_ImageRequested"
OnItemCreated="galleryImages_ItemCreated"
OnPreRender="galleryImages_PreRender">
</telerik:RadImageGallery>
Code behind for NeedDataSource (which returns successfully):
protected void galleryImages_NeedDataSource(object sender, Telerik.Web.UI.ImageGalleryNeedDataSourceEventArgs e)
{
galleryImages.Visible = false;
FileManager fileManager = new FileManager ();
DataTable dt = fileManager.GetFileList();
var imageFiles = fileManager.GetGalleryImages(dt);
if (imageFiles.Count() == 0)
{
txtNotes.Text = "There are no image files to display.";
return;
}
try
{
galleryImages.DataSource = imageFiles;
galleryImages.Visible = true;
}
catch (Exception ex)
{
LogException(ex);
ErrorMessage("An unexpected occurred while reading one or more the image files");
}
if (fileManager.HasErrors)
{
txtErrors.Text = "The following errors were encountered:<br/><ul>" + String.Join("<li>", fileManager.ErrorList) + "</ul>";
}
}
Library code that reads image files into byte arrays (also return successfully):
public List<GalleryImage> GetGalleryImages(DataTable srcDataTable)
{
//...
foreach (DataRow dr in query)
{
CustomImageFile f = new CustomImageFile(dr);
try
{
byte[] byteArray = File.ReadAllBytes(f.FullFileName);
GalleryImage img = new GalleryImage
{
ImageBytes = byteArray,
Description = $"{f.Description}", // [{f.Name}]
};
imageList.Add(img);
}
catch (Exception ex)
{
Errors.Add($"{f.Name} could not be read");
LogException(ex);
}
}
return imageList;
}
Yet when there is an invalid file encountered, we get this:
[ArgumentException: Parameter is not valid.]
System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) +1162080
Telerik.Web.UI.BinaryImageFormatHelper.CreateImgFromBytes(Byte[] data) +69
[ArgumentException: The provided binary data may not be valid image or may contains unknown header]
Telerik.Web.UI.BinaryImageFormatHelper.CreateImgFromBytes(Byte[] data) +120
Telerik.Web.UI.BinaryImageTransformationFilter.ProcessImageInternal(Byte[] image) +44
Telerik.Web.UI.BinaryImageTransformationFilter.ProcessImage(Byte[] image) +20
Telerik.Web.UI.BinaryImageFilterProcessor.ProcessFilters(Byte[] imageData) +112
Telerik.Web.UI.RadBinaryImage.ProcessImageData() +57
Telerik.Web.UI.RadBinaryImage.OnPreRender(EventArgs e) +20
System.Web.UI.Control.PreRenderRecursiveInternal() +90
System.Web.UI.Control.PreRenderRecursiveInternal() +163