Insert BarCode inside FlowDocumentEditor

1 Answer 60 Views
BarCode
Richard
Top achievements
Rank 1
Iron
Richard asked on 05 Nov 2023, 02:04 PM

Hi,

I'm trying to add a barcode inside a FlowDocumentEditor.

 


public static ImageInline InsertQRCode(this RadFlowDocumentEditor editor, string code, double width = 96)
{

    // Barcode
    const double imgWidth = 1000;
    var barcode = new RadBarcode
    {
        Width = imgWidth,
        Height = imgWidth,
        Symbology = new QRCode()
        {
            ErrorCorrectionLevel = ErrorCorrectionLevel.H,
            CodeMode = CodeMode.Alphanumeric
        },
        Value = code
    };
    
    barcode.BeginInit();
    barcode.Measure(new Size(imgWidth, imgWidth));
    barcode.Arrange(new Rect(new Size(imgWidth, imgWidth)));
    barcode.UpdateLayout();
    barcode.EndInit();

    using (var stream = new MemoryStream())
    {
        Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(barcode, stream, new PngBitmapEncoder());
        stream.Position = 0;

        return editor.InsertImageInline(new Telerik.Windows.Documents.Media.ImageSource(stream, "png"),
            new Size(96, 96));

    }

At barcode.Measure, I've got an NullException. This didn't happen with old RadBarCodeQR component.

Any ideas?

1 Answer, 1 is accepted

Sort by
0
Accepted
Martin Ivanov
Telerik team
answered on 07 Nov 2023, 11:18 AM

Hello Richard,

With the new barcode, you will need to take the extra step to assign its Style manually before measuring the control.

var resourceDictionary = new ResourceDictionary()  
{  
	Source = new Uri("/Telerik.Windows.Controls.DataVisualization;component/Themes/GenericVisualStudio2019.xaml", UriKind.RelativeOrAbsolute)  
}; 
barcode.Style = (Style)resourceDictionary["BarcodeStyle"]; 

This approach is described in the following KB article.

Regards,
Martin Ivanov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Richard
Top achievements
Rank 1
Iron
commented on 08 Nov 2023, 09:38 AM

Thank you. Works nice.

For those who use themed controls, I changed it to this (I use Fluent design):


var resourceDictionary = new ResourceDictionary()
{
    Source = new Uri("/Telerik.Windows.Themes.Fluent;component/Themes/telerik.windows.controls.datavisualization.xaml", UriKind.RelativeOrAbsolute)
};

barcode.Style =  resourceDictionary["BarcodeStyle"] as System.Windows.Style;

Tags
BarCode
Asked by
Richard
Top achievements
Rank 1
Iron
Answers by
Martin Ivanov
Telerik team
Share this question
or