Link to home
Start Free TrialLog in
Avatar of Navigator101
Navigator101

asked on

How do up save the visible area of an image that has been clipped?

Considering an image of 1096 wide and 800 high displayed in a UIFrameworkElement of 200x100 which has the ClipToBound set true.  If the image is scaled / rotated etc. by the user they will only see a partial of the image, every thing outside of the UIFrameworkElement will be clipped,
Can the visible portion of the image saved to a new bitmap/file?

A WriteableBitmap can be created and transformed but is there a way to determine the visible bounds so that the image can then be cropped to mirror that of the UIFrameworkElement?  Or can the image control itself be used to determine what is visible?
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Have a look at the "RenderTargetBitmap" class.  What it's designed for, to be fed a Visual object and render that to a BitmapSource.

FrameworkElement target = null;     //whatever your item is actually going to be here 
var rtb = new RenderTargetBitmap((int)target.ActualWidth, (int)target.ActualHeight, 96, 96, PixelFormats.Pbgra32);
rtb.Render(target);
            
//save it
var enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(rtb));
using (var fs = File.Create(@"xxx"))
{
    enc.Save(fs);
}

Open in new window

Avatar of Navigator101
Navigator101

ASKER

Thanks, but in WinRT this is all gone.. Microsoft have change all the rendering methods.. for instance The RenderTargetBitmap is now in Windows.UI.Media.Imaging and doesn't have any constructors instead it uses a RenderAsync method..  o.. and you cant add frames.
The BitmapTransform method on the Encoder only allows rotation of 90, 180 and 270.
Alll I want to do is save the transformed image... its proving more testing...
Aha, my mistake, when I saw xaml I thought WPF.
If no one else answers tonight, I have a bunch of projects on my system at work where I did the same thing on WinRT, I'll post it tomorrow am (don't remember the syntax right now).
Forgive me if I forget, insane week :S
ASKER CERTIFIED SOLUTION
Avatar of Navigator101
Navigator101

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Solved after documentation research and sitting on sofa solving problems.