Happy stress free coding

Sep 27, 2008

WPF visuals on the clipboard

I had an urgent need to copy diagrams from Modlr to word, or anything really. So I was half expecting to find some easy way to go from a visual to a emf-graphic that I could put on the clipboard.

I had to give up on that since the new idea of graphic vector language is XPS and even if the printing logic in vista easily converts between XPS and EMF it does not seem to be exposed in any ease way to us mortals.

The options was then to use a raster format, like bitmap; and this post describes how to do that:

 
 

System.Windows.Controls.Canvas vis = this.CurrentDiagramCanvas();

 

int Height = (int)vis.DesiredSize.Height;

 

int Width = (int)vis.DesiredSize.Width;

 

RenderTargetBitmap bmp = new RenderTargetBitmap(Width, Height, 96, 96, PixelFormats.Pbgra32);

  
 

System.Windows.Shapes.Rectangle vRect = new System.Windows.Shapes.Rectangle();

 

vRect.Width = Width;

 

vRect.Height = Height;

 

vRect.Fill = System.Windows.Media.Brushes.White; // Fill it with white; the new bitmap is black, and all your alpha channels will make the end image more or less black

 

vRect.Arrange(new Rect(0, 0, vRect.Width, vRect.Height));

 

bmp.Render(vRect);

 

bmp.Render(vis);

 

System.Windows.DataObject m_data = new System.Windows.DataObject();

 

m_data.SetData(System.Windows.DataFormats.Bitmap,bmp, true);

 

System.Windows.Clipboard.SetDataObject(m_data, true);

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home