question

saravananmadhesh-7190 avatar image
0 Votes"
saravananmadhesh-7190 asked DaisyTian-1203 answered

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

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-wpf
customcontrol.txt (2.3 KiB)
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Maybe it was not the appropriate time to call Save. When do you call the function?

0 Votes 0 ·

@saravananmadhesh-7190
Could you show the code of Template? Do you mean the code doesn't work fine when it is <local:CustomControl x:Name="StackingColumnChart2" Template="{StaticResource Template}" Grid.Row="1"/>? And it works fine when code is <local:CustomControl x:Name="StackingColumnChart2" Template="{StaticResource Template}" Grid.Row="0"/>? Could you show me when to call method Save and the detailed steps to reproduce the error?

0 Votes 0 ·

80511-mainpagexml.txt


please check the attached files. saved the content in button click.

80512-mainwindow.txt


0 Votes 0 ·
mainpagexml.txt (2.8 KiB)
mainwindow.txt (1.7 KiB)

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.