This is a migrated thread and some comments may be shown as answers.

BitMap Exception for RadDiagram.ExportToImage() method

9 Answers 152 Views
Diagram
This is a migrated thread and some comments may be shown as answers.
Valentin
Top achievements
Rank 1
Iron
Iron
Valentin asked on 18 Jun 2018, 02:49 PM

Hi Telerik,

 

I'm export my RadDiagram to Image with the appropriate functionnality, but I have an error (not systematically).

My code :

Private Function CreateStreamFromDiagram(diag As RadDiagram, rect As Windows.Rect) As MemoryStream
    If diag Is Nothing Then Return New MemoryStream({})
 
    Dim mem_stream As New MemoryStream()
 
    diag.ExportToImage(
        stream:=mem_stream,
        encoder:=New System.Windows.Media.Imaging.JpegBitmapEncoder(),
        enclosingBounds:=rect,
        returnImageSize:=rect.Size,
        backgroundBrush:=Windows.Media.Brushes.White,
        margin:=New Windows.Thickness(1),
        dpi:=DIAGRAM_IMG_DPI
    )
 
    Return mem_stream
End Function

 

When the method is called, sometimes, I have this error :

à System.Windows.Media.Imaging.WriteableBitmap.InitFromBitmapSource(BitmapSource source)
   à System.Windows.Media.Imaging.WriteableBitmap..ctor(BitmapSource source)
   à Telerik.Windows.Controls.Diagrams.BitmapUtils.CreateWriteableBitmap(UIElement element, Rect enclosingBounds, Size returnImageSize, Brush backgroundBrush, Thickness margin, Double dpi)
   à Telerik.Windows.Controls.RadDiagram.CreateDiagramImage(Rect enclosingBounds, Size returnImageSize, Brush backgroundBrush, Thickness margin, Double dpi)
   à Telerik.Windows.Controls.RadDiagram.ExportToImage(Stream stream, BitmapEncoder encoder, Nullable`1 enclosingBounds, Size returnImageSize, Brush backgroundBrush, Thickness margin, Double dpi)
   à Portal_ViewModel.MirBuilder.PdfAReportItext7.CreateStreamFromDiagram(RadDiagram diag, Rect rect) dans C:\Users\ng707d2\Desktop\DEV\MAA_MIR_Builder_v2\Sources\WPFPortal\Portal_ViewModel\Mir_Builder\PDFGeneration\PdfAReportItext7.vb:ligne 1292
   à Portal_ViewModel.MirBuilder.PdfAReportItext7.ExportInspectionDiagToImage(Single p_widthRedRectangle) dans C:\Users\ng707d2\Desktop\DEV\MAA_MIR_Builder_v2\Sources\WPFPortal\Portal_ViewModel\Mir_Builder\PDFGeneration\PdfAReportItext7.vb:ligne 1397
   à Portal_ViewModel.MirBuilder.PdfAReportItext7.ExportPage() dans C:\Users\ng707d2\Desktop\DEV\MAA_MIR_Builder_v2\Sources\WPFPortal\Portal_ViewModel\Mir_Builder\PDFGeneration\PdfAReportItext7.vb:ligne 1360
   à Portal_ViewModel.MirBuilder.PdfAReportItext7.ExportCurrentOnglet() dans C:\Users\ng707d2\Desktop\DEV\MAA_MIR_Builder_v2\Sources\WPFPortal\Portal_ViewModel\Mir_Builder\PDFGeneration\PdfAReportItext7.vb:ligne 446

 

Sometimes, the error is an OutOfMemoryException type, but sometimes, the error is "Unable to evaluate expression".

And others times, the error is not occurred...

 

Do you have any idea about this problem ?

Thank you.

 

Valentin.

9 Answers, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 21 Jun 2018, 11:34 AM
Hi Valentin,

Thank you for the provided stack trace.

The ExportToImage method of the RadDiagram internally uses WriteableBitmap which has its limitation. That is why when you want to export a really big diagram OutOfMemoryException could be thrown. In this case, you can create a custom code which splits the diagram into multiple images. We logged Feature Request to improve the export functionality in our feedback portal where you can vote for it and track its progress. You can try the suggestions explained there.

Regards,
Dinko
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Valentin
Top achievements
Rank 1
Iron
Iron
answered on 21 Jun 2018, 12:01 PM

Hi Dinko,

Thank you for your answer.

 

I think the problem is not come from a "big diagram" because sometimes, I have this exception when there is only one little circle (for example) in my diagram.

And if I repeat my test case, the exception is not always occurred.

 

Do you know what I meant ?

0
Dinko | Tech Support Engineer
Telerik team
answered on 26 Jun 2018, 10:35 AM
Hello Valentin,

We are not aware of getting an exception when exporting only one shape. Is it possible to isolate this behavior in a standalone project? Also, when exporting "one little circle" which type of error you got: OutOfMemoryException exception or "Unable to evaluate expression"?

Regards,
Dinko
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Valentin
Top achievements
Rank 1
Iron
Iron
answered on 27 Jun 2018, 07:23 AM

Hi Dinko,

 

I can't export it in a little project, there is a lot of Business classes, and linked classes etc.. to can export it.

Like I said, this behavior does not happen all the time, but when this is the case, I have theses 2 problems :

  • Sometimes OutOfMemoryException exception
  • Sometines "Unable to evaluate expression" (from the debugger)

 

It's a complex problem I know...

0
Dinko | Tech Support Engineer
Telerik team
answered on 29 Jun 2018, 01:58 PM
Hi Valentine,

Without reproducing it on our side I can only guess what could be the reason. As you mentioned in your previous post you get this exception even if there is one shape. What comes up to my mind is the size of the diagram. Can you double check what is the Width/Height of the diagram before calling the Export functionality? Setting really big size could lead to this exception. Is it possible to send me a snippet of the method/class which exports the diagram?

Regards,
Dinko
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Valentin
Top achievements
Rank 1
Iron
Iron
answered on 04 Jul 2018, 03:23 PM

Hi Dinko,

 

The diagram has this size : 684 (h) & 1717.2 (w).

The Rectangle used in the ExportToImage method has this size : 670 (h) & 915 (w) & 20 (x & y).

If I add a little circle, it size is 100 (for h & w).

DIAGRAM_IMG_DPI variable = 300 (it's a double)

 

Today, I reproduced this bug 3 times, but from the fourth time, there is no problem. It's not all the time the same number of tries...

I don't understand what is the trigger of this bug, and what is the resolver. It's always the same used data.

 

Thank you.

0
Dinko | Tech Support Engineer
Telerik team
answered on 09 Jul 2018, 10:10 AM
Hello Valentine,

Thank you for the provided additional information.

Still, I wasn't able to reproduce this exception. Attached to this reply you can find the sample project which I used to test your scenario. Can you take a look at this example and let me know what I need to modify in order to reproduce this exception.

I looking forward to your reply.

Regards,
Dinko
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
0
Valentin
Top achievements
Rank 1
Iron
Iron
answered on 02 Aug 2018, 08:32 AM

Hi Dinko,

 

I can't reproduce my problem with your project.

But now, I can't reproduce the first problem, but I have a similar issue : Sometimes the following error is occured :

System.OutOfMemoryException: Insufficient memory to continue the execution of the program.
   at System.Windows.Media.Imaging.WriteableBitmap.InitFromBitmapSource(BitmapSource source)
   at System.Windows.Media.Imaging.WriteableBitmap..ctor(BitmapSource source)
   at Telerik.Windows.Controls.Diagrams.BitmapUtils.CreateWriteableBitmap(UIElement element, Rect enclosingBounds, Size returnImageSize, Brush backgroundBrush, Thickness margin, Double dpi)
   at Telerik.Windows.Controls.RadDiagram.CreateDiagramImage(Rect enclosingBounds, Size returnImageSize, Brush backgroundBrush, Thickness margin, Double dpi)
   at Telerik.Windows.Controls.RadDiagram.ExportToImage(Stream stream, BitmapEncoder encoder, Nullable`1 enclosingBounds, Size returnImageSize, Brush backgroundBrush, Thickness margin, Double dpi)

   at Portal_ViewModel.MirBuilder.PdfAReportItext7.CreateStreamFromDiagram(RadDiagram diag, Rect rect)
   at Portal_ViewModel.MirBuilder.PdfAReportItext7.ExportSummaryDiagramToImage(Rectangle pdfRect)
   at Portal_ViewModel.MirBuilder.PdfAReportItext7.ExportSummary()
   at Portal_ViewModel.MirBuilder.PdfAReportItext7.ExportCurrentOnglet()
   at Portal_ViewModel.MirBuilder.OngletDiagram.PersisteDiagram(When when, Nullable`1 delay_database)
   at Portal_View.MirBuilder.OngletSummary.UserControl_Unloaded(Object sender, RoutedEventArgs e)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
   at System.Windows.BroadcastEventHelper.BroadcastEvent(DependencyObject root, RoutedEvent routedEvent)
   at System.Windows.BroadcastEventHelper.BroadcastUnloadedEvent(Object root)
   at MS.Internal.LoadedOrUnloadedOperation.DoWork()
   at System.Windows.Media.MediaContext.FireLoadedPendingCallbacks()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Window.ShowHelper(Object booleanBox)
   at System.Windows.Window.Show()
   at System.Windows.Window.ShowDialog()
   at Portal_View.MirBuilderMain.StartMirBuilder()
   at Portal_View.Commands.StartMIRBuilder()
   at Portal_View.CommandsPage.MIRBuilderButton_Click(Object sender, RoutedEventArgs e)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
   at System.Windows.Controls.Primitives.ButtonBase.OnClick()
   at System.Windows.Controls.Button.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run(Window window)
   at Portal_View.Application.Main()

I want to know if you know if it's a maximum size for the image. For example, if there are some big size image, the export can fail ?

 

Thank you for your investigaiton.

0
Dinko | Tech Support Engineer
Telerik team
answered on 06 Aug 2018, 12:44 PM
Hello Valentin,

As the ExportToImage method of the RadDiagram internally uses WriteableBitmap class, the max size is probably ~4 GB. You can take a look at this MSDN thread where a similar question was discussed.

Regards,
Dinko
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Diagram
Asked by
Valentin
Top achievements
Rank 1
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Valentin
Top achievements
Rank 1
Iron
Iron
Share this question
or