0

I'm using Xceed WPF Toolkit (Community Edition) DataGridControl, and I would like to create a bitmap from the control (either to put on the clipboard or save to a png).

I have tried using a RenderBitmapTarget, but it will only copy the control as it is rendered on the screen (my grid is bigger than the screen).

My RenderBitmapTarget code looks like this:

RenderTargetBitmap rtb = new RenderTargetBitmap((int)control.ActualWidth, (int)control.ActualHeight, 96, 96, PixelFormats.Pbgra32);
rtb.Render(control);
PngBitmapEncoder png = new PngBitmapEncoder();
png.Frames.Add(BitmapFrame.Create(rtb));
MemoryStream stream = new MemoryStream();
png.Save(stream);
Image image = Image.FromStream(stream);

I've tried specifying a larger size (both in the RenderTargetBitmap constructor, and specifying new width/height for the control, but both just yielded the same image on a larger canvas.

Any thoughts?

Claudio P
  • 2,133
  • 3
  • 25
  • 45
David Hope
  • 2,216
  • 16
  • 32
  • By setting the grids width and height to Double.NaN and calling UpdateLayout prior to the Render, I now get the control rendered at the correct size, but only the visible data is rendered (I guess due to data virtualization) – David Hope Jun 05 '15 at 15:41

1 Answers1

0

Ok, I figured it out....

The final key was to disable delayed loading on the DataGridControl.View...here is my final code:

XAML:

<xcdg:DataGridControl x:Name="CEGrid">
     <xcdg:DataGridControl.View>
         <xcdg:TableflowView IsDeferredLoadingEnabled="False"/>
     </xcdg:DataGridControl.View>
 </xcdg:DataGridControl>

C# code-behind: double tempWidth = CEGrid.ActualWidth; double tempHeight = CEGrid.ActualHeight;

CEGrid.Width = double.NaN;
CEGrid.Height = double.NaN;

CEGrid.UpdateLayout();

RenderTargetBitmap rtb = new RenderTargetBitmap((int)CEGrid.ActualWidth, (int)CEGrid.ActualHeight, 96, 96, PixelFormats.Pbgra32);

rtb.Render(CEGrid);

PngBitmapEncoder pbe = new PngBitmapEncoder();
pbe.Frames.Add(BitmapFrame.Create(rtb));
MemoryStream stream = new MemoryStream();
pbe.Save(stream);
System.Drawing.Bitmap image = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(stream);

CEGrid.Width = tempWidth;
CEGrid.Height = tempHeight;
David Hope
  • 2,216
  • 16
  • 32