Showing posts with label WPF Control to Image etc. Show all posts
Showing posts with label WPF Control to Image etc. Show all posts

Tuesday, August 10, 2010

Converting WPF Control to Image

In one of my application I need to convert my WPF Form to Image. I Googled around and find RenderTargetBitmap is solution to my Problem. The Code is given below.

public static PngBitmapEncoder ImageFromControl(Control myControl)
{
Transform transform =
myControl.LayoutTransform;

System.Windows.Size sizeOfControl = new System.Windows.Size(
myControl.ActualWidth, controlToConvert.ActualHeight);
myControl.Measure(sizeOfControl);
myControl.Arrange(new Rect(sizeOfControl));

RenderTargetBitmap renderBitmap = new RenderTargetBitmap((Int32)sizeOfControl.Width, (Int32)sizeOfControl.Height, 96d, 96d, PixelFormats.Pbgra32);
renderBitmap.Render(
myControl);

PngBitmapEncoder pngEncoder = new PngBitmapEncoder();
pngEncoder.Frames.Add(BitmapFrame.Create(renderBitmap));

return pngEncoder;
}
and in my Main function is use it like,,,
stream = File.Create("Control.jpg");
ImageFromControl(myControl).Save(stream);
stream.Close();
I hope this will Help..... Happy Coding :)