Exception when creating QRCode as image in code

4 posts, 0 answers
  1. Martin
    Martin avatar
    17 posts
    Member since:
    Apr 2009

    Posted 23 Sep 2014 Link to this post

    Im getting the following exception when i try to create a QRCode and render it as an image in c#.

    System.ArgumentOutOfRangeException was unhandled
      _HResult=-2146233086
      _message=The parameter value must be greater than zero.
      HResult=-2146233086
      IsTransient=false
      Message=The parameter value must be greater than zero.
    Parameter name: pixelWidth
      Source=PresentationCore
      ParamName=pixelWidth
      StackTrace:
           at System.Windows.Media.Imaging.RenderTargetBitmap..ctor(Int32 pixelWidth, Int32 pixelHeight, Double dpiX, Double dpiY, PixelFormat pixelFormat)
           at Telerik.Windows.Media.Imaging.ExportHelper.GetBitmapSource(FrameworkElement element, Double dpiX, Double dpiY)
           at Telerik.Windows.Media.Imaging.ExportHelper.GetElementImage(FrameworkElement element)
           at Telerik.Windows.Media.Imaging.ImageExporter.Export(FrameworkElement element, Stream stream, BitmapEncoder encoder)
           at Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(FrameworkElement element, Stream stream, BitmapEncoder encoder)
           at TelerikWpfApp1.MainWindow.Button_Click(Object sender, RoutedEventArgs e) in c:\Users\Martin\Documents\Visual Studio 2013\Projects\TelerikWpfApp1\TelerikWpfApp1\MainWindow.xaml.cs:line 46
           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 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, 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.Threading.Dispatcher.Run()
           at System.Windows.Application.RunDispatcher(Object ignore)
           at System.Windows.Application.RunInternal(Window window)
           at System.Windows.Application.Run(Window window)
           at System.Windows.Application.Run()
           at TelerikWpfApp1.App.Main() in c:\Users\Martin\Documents\Visual Studio 2013\Projects\TelerikWpfApp1\TelerikWpfApp1\obj\x86\Debug\App.g.cs:line 0
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(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 System.Threading.ThreadHelper.ThreadStart()

    Im using 2014.2.729.40. Attached is a new Telerik Wpf app project that shows the same behaviour as my program.

    TelerikWpfApp1.zip

    Am I missing some vital property or is this a known issue?

    Thanks,
    MArtin
  2. Tsvetie
    Admin
    Tsvetie avatar
    1517 posts

    Posted 25 Sep 2014 Link to this post

    Hi Martin,

    You need to set the Width and Height of the control before you can export it. For example:
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var qrCode = new RadBarcodeQR();
     
        qrCode.BeginInit();
        this.SetUpBarCode(qrCode);
        qrCode.EndInit();
        qrCode.Width = 200;
        qrCode.Height = 200;
        qrCode.Measure(new System.Windows.Size(200, 200));
        qrCode.Arrange(new Rect(new System.Windows.Size(200, 200)));
     
        var dialog = new SaveFileDialog() { FileName = "QRBarCode.png" };
        var res = dialog.ShowDialog();
     
        if (res.HasValue && res.Value)
        {
            using (var stream = dialog.OpenFile())
            {
                ExportExtensions.ExportToImage(qrCode, stream, new PngBitmapEncoder());
            }
        }
     
    }
     
    private void SetUpBarCode(RadBarcodeQR qrCode)
    {
     
        qrCode.ErrorCorrectionLevel = QRClassLibrary.Modes.ErrorCorrectionLevel.H;
        qrCode.Mode = QRClassLibrary.Modes.CodeMode.Alphanumeric;
     
        qrCode.Text = "Test Test Test 123456";
    }


    Regards,
    Tsvetie
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Martin
    Martin avatar
    17 posts
    Member since:
    Apr 2009

    Posted 26 Sep 2014 in reply to Tsvetie Link to this post

    Hi Tsvetie,

    That works but isnt really what i want.
    I use this code in various places each with different amounts of data. So the qr code version changes to cope with that and i then need to render it as a different size. Is there a way to cause it to auto size?

    Also is there really no simpler way to render this? It seems a bit silly to have to do all that manual WPF stuff when i just want an image of a qr code.

    Thanks,
    Martin

  5. Tsvetie
    Admin
    Tsvetie avatar
    1517 posts

    Posted 26 Sep 2014 Link to this post

    Hi Martin,

    In order to render an image of a QRcode, you need to specify size for the QRcode. As you do not add this control to the page, it does not have a parent that can stretch it or determine its size. As a result, in case you do not specify size for the control, it will render as 0,0 and you will not be able to see it. 

    Regarding auto-sizing - basically the control can render with size 100, 100 and size 1000, 1000. There is no fixed size based on the type of the barcode. In case you need this behavior, you can implement the code that calculates the size for the barcode based on your specification.

    Regards,
    Tsvetie
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top