RenderTargetBitmap.Render() creates blank image when element present in Grid.Row = "1"

saravanan madhesh 1 Reputation point
2021-03-22T09:05:24.797+00:00

Save element (inherited from System.Windows.Controls.Control) as image using below code.

RenderTargetBitmap bmpSource = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight, 96, 96, PixelFormats.Pbgra32);  
bmpSource.Render(element);  
imgEncoder.Frames.Add(BitmapFrame.Create(bmpSource));  
using (Stream stream = File.Create(fileName))  
{  
    imgEncoder.Save(stream);  
}  
  

Works fine when element present in 0 th row, not works for 1st row inside grid. By as a workaround, added inside the stackpanel and then grid it works.
80054-customcontrol.txt

[Xaml]

<Grid.RowDefinitions>  
                                <RowDefinition Height="auto"/>  
                                <RowDefinition Height="25*"/>  
                                <RowDefinition Height="75*"/>  
                            </Grid.RowDefinitions>  
. . .  
<local:CustomControl x:Name="StackingColumnChart2" Template="{StaticResource Template}" Grid.Row="1"/>   
. . .  
  
  
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,806 questions
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,631 Reputation points
    2021-03-24T06:03:39.06+00:00

    I update your Save method as below shown to implement want you want

     public void Save(string fileName)  
            {  
                FrameworkElement element = this;  
                string imageExtension;  
                imageExtension = new FileInfo(fileName).Extension.ToLower(System.Globalization.CultureInfo.InvariantCulture);  
                BitmapEncoder imgEncoder;  
                switch (imageExtension)  
                {  
                    case ".bmp":  
                        imgEncoder = new BmpBitmapEncoder();  
                        break;  
                    case ".jpg":  
                    case ".jpeg":  
                        imgEncoder = new JpegBitmapEncoder();  
                        break;  
                    case ".png":  
                        imgEncoder = new PngBitmapEncoder();  
                        break;  
                    case ".gif":  
                        imgEncoder = new GifBitmapEncoder();  
                        break;  
                    case ".tif":  
                    case ".tiff":  
                        imgEncoder = new TiffBitmapEncoder();  
                        break;  
                    case ".wdp":  
                        imgEncoder = new WmpBitmapEncoder();  
                        break;  
                    default:  
                        imgEncoder = new BmpBitmapEncoder();  
                        break;  
                }  
                if (element != null)  
                {  
                    DrawingVisual drawingVisual = new DrawingVisual();  
                    using (DrawingContext context = drawingVisual.RenderOpen())  
                    {  
                        VisualBrush brush = new VisualBrush(element) { Stretch = Stretch.Fill };  
                        context.DrawRectangle(brush, null, new Rect(0, 0, element.ActualWidth, element.ActualHeight));  
                        context.Close();  
                    }  
      
                    RenderTargetBitmap bitmap = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight, 96, 96, PixelFormats.Pbgra32);  
                    bitmap.Render(drawingVisual);  
      
                    imgEncoder.Frames.Add(BitmapFrame.Create(bitmap));  
                    using (Stream stream = File.Create(fileName))  
                    {  
                        imgEncoder.Save(stream);  
                    }  
                }  
            }  
       
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

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