Share via

Save Stackpanel's content as PNG image

Pratham Jain 351 Reputation points
2026-02-21T17:38:00.1066667+00:00

Hi All,

I have a stackpanel control which displays four textblocks in vertical orientation.

User's image

The second textbox displays the barcode generated using code39 font. I want to print all these four textblocks on barcode stickers for scanning. For this I am converting the stackpanel to PNG image with specific height and width and saving the image on disk like below:

private void SaveStackPanelAsPng(StackPanel panel, string filename, int pixelWidth = 500, int pixelHeight = 150)
{
    // Ensure the panel has its layout calculated correctly
    // 1. Measure and Arrange the panel (important if it's not fully rendered)
    // 1. Measure and Arrange the element to the desired size
    panel.Measure(new Size(pixelWidth, pixelHeight));
    panel.Arrange(new Rect(0, 0, pixelWidth, pixelHeight));
    // 2. Render the element to a RenderTargetBitmap
    RenderTargetBitmap renderBitmap = new RenderTargetBitmap(
        pixelWidth, pixelHeight, 96, 96, PixelFormats.Pbgra32); // Use 96 DPI (standard WPF DPI)
    renderBitmap.Render(panel);
    // 3. Encode the bitmap as PNG
    PngBitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(renderBitmap));
    // 4. Save to a file stream
    using (FileStream stream = File.Create(filename))
    {
        encoder.Save(stream);
    }
}

But after saving it as PNG it is not displaying any barcode information (or displaying 0 barcodes) in image. Please advise how can I fix the same.

Regards,

Pratham

Developer technologies | Windows Presentation Foundation

Answer accepted by question author
  1. Danny Nguyen (WICLOUD CORPORATION) 6,615 Reputation points Microsoft External Staff Moderator
    2026-02-23T09:26:01.0133333+00:00

    Hi @Pratham Jain,

    Your export code is OK. The issue is typically that the “barcode” is generated by a Code39 font, and when WPF renders to a RenderTargetBitmap it will only draw the bars if that exact font is available and actually used. If WPF can’t resolve the Code39 font, it silently falls back to a normal font and the barcode bars won’t appear in the saved PNG.

    Please check

    1. Is the Code39 font installed on the same machine where you generate the PNG (dev PC / server / VM)?
    2. Are you explicitly setting the barcode TextBlock’s FontFamily to the Code39 font (with the correct family name)?

    Example (works if the font is installed and the family name matches):

    
    <TextBlock Text="*RI000001*" FontFamily="Free 3 of 9" FontSize="48" />
    
    

    To avoid relying on the font being installed on every machine, you can try embed the Code39 font to your WPF app

    1. Add the .ttf (Code39) to your project, e.g. Fonts/Code39.ttf
    2. Select the TTF in Solution Explorer and set Build Action = Resource
    3. Reference it via pack URI (important: the part after # must match the font’s internal family name, not necessarily the file name):
    
    <TextBlock
    
        Text="*RI000001*"
    
        FontSize="48"
    
        FontFamily="pack://application:,,,/YourAssemblyName;component/Fonts/#Your Code39 Font Family Name" />
    
    

    You can read more on that approach here Packaging Fonts with Applications.

    Let me know if this works for you.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.