![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
| |
| | #1 (permalink) |
| Guest | Save WPF Control as PNG image I'd like to save a WPF control as a PNG image. I've tried using the following code: http://dvuyka.spaces.live.com/blog/c...E19A!240.entry but the calls the Measure and Arrange screw up the layout of the control which is then drawn incorrectly until the user resizes the window. I don't understate what state they've changed but I am unable to fix it. I've tried adding calls to InvalidateMeasure and InvalidateArrange and UpdateLayout but they do not fix the problem. I just discovered that you can use the backwards compatibility with WinForms stuff in "Forms.Integration" to create an ElementHost that has a DrawToBitmap method for Win32 bitmaps. What is the recommended way to do this? Am I really supposed to resort to calling the backwards compatibility stuff and using WinForms? -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. http://www.ffconsultancy.com/?u |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: Save WPF Control as PNG image There's lots of other examples of using RenderTargetBitmap floating around that don't use Measure/Arrange. Here's just one: http://blogs.msdn.com/swick/archive/...using-wpf.aspx Mark -- Mark Salsbery Microsoft MVP - Visual C++ "Jon Harrop" <jon@xxxxxx> wrote in message news aSdnVgGD6f1e1XVnZ2dnUVZ8jidnZ2d@xxxxxxQuote: > > I'd like to save a WPF control as a PNG image. I've tried using the > following code: > > http://dvuyka.spaces.live.com/blog/c...E19A!240.entry > > but the calls the Measure and Arrange screw up the layout of the control > which is then drawn incorrectly until the user resizes the window. I don't > understate what state they've changed but I am unable to fix it. I've > tried > adding calls to InvalidateMeasure and InvalidateArrange and UpdateLayout > but they do not fix the problem. > > I just discovered that you can use the backwards compatibility with > WinForms > stuff in "Forms.Integration" to create an ElementHost that has a > DrawToBitmap method for Win32 bitmaps. > > What is the recommended way to do this? Am I really supposed to resort to > calling the backwards compatibility stuff and using WinForms? > > -- > Dr Jon D Harrop, Flying Frog Consultancy Ltd. > http://www.ffconsultancy.com/?u |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: Save WPF Control as PNG image "Jon Harrop" <jon@xxxxxx> wrote in message news:La2dnRxn6vCZ9VTVnZ2dnUVZ8uGdnZ2d@xxxxxx Quote: > Mark Salsbery [MVP] wrote: Quote: >> There's lots of other examples of using RenderTargetBitmap floating >> around >> that don't use Measure/Arrange. >> >> Here's just one: >> > > That solution does not work correctly when the control is already in a > window. Specifically, the control appears in the output image at its > offset > in the window. RenderTargetBitmap. Here's another, possibly addressing the offset bitmap... http://www.west-wind.com/Weblog/posts/150676.aspx Mark -- Mark Salsbery Microsoft MVP - Visual C++ Quote: |
My System Specs![]() |
| | #4 (permalink) |
| Member | Re: Save WPF Control as PNG image I'd like to save a WPF control as a PNG image. I've tried using the following code: http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!240.entry but the calls the Measure and Arrange screw up the layout of the control which is then drawn incorrectly until the user resizes the window. I don't understate what state they've changed but I am unable to fix it. I've tried adding calls to InvalidateMeasure and InvalidateArrange and UpdateLayout but they do not fix the problem. I just discovered that you can use the backwards compatibility with WinForms stuff in "Forms.Integration" to create an ElementHost that has a DrawToBitmap method for Win32 bitmaps. What is the recommended way to do this? Am I really supposed to resort to calling the backwards compatibility stuff and using WinForms? -- Dr Jon D Harrop, Flying Frog Consultancy Ltd. Flying Frog Consultancy Ltd. Pixillion Free Image File Converter - Convert jpeg, gif pdf and more. It's free and works great.................................... ART |
My System Specs![]() |
| | #5 (permalink) |
| Newbie | Re: Save WPF Control as PNG image I dont know if you have found your solution or not.. but this is how it works for me.. Code:
public static ImageSource ToImageSource(FrameworkElement obj)
{
// Save current canvas transform
Transform transform = obj.LayoutTransform;
obj.LayoutTransform = null;
// fix margin offset as well
Thickness margin = obj.Margin;
obj.Margin = new Thickness(0, 0,
margin.Right - margin.Left, margin.Bottom - margin.Top);
// Get the size of canvas
Size size = new Size(obj.Width, obj.Height);
// force control to Update
obj.Measure(size);
obj.Arrange(new Rect(size));
RenderTargetBitmap bmp = new RenderTargetBitmap(
(int)obj.Width, (int)obj.Height, 96, 96, PixelFormats.Pbgra32);
bmp.Render(obj);
// return values as they were before
obj.LayoutTransform = transform;
obj.Margin = margin;
return bmp;
}
|
My System Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| save image from picturebox | .NET General | |||
| How to save image as HD Photo? | Vista music pictures video | |||
| Save drive image to network storage device | Vista General | |||
| Save Image as PDF? | Vista print fax & scan | |||