Solved

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

Posted on 2015-01-28
5
70 Views
Last Modified: 2015-02-04
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?
0
Comment
Question by:Navigator101
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:Snarf0001
ID: 40577096
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

0
 

Author Comment

by:Navigator101
ID: 40578663
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...
0
 
LVL 23

Expert Comment

by:Snarf0001
ID: 40578668
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
0
 

Accepted Solution

by:
Navigator101 earned 0 total points
ID: 40579407
I know all about insane weeks, all too familiar! Solved it after some logical hacking around, its all quite sensitive. -- anyway here it is... if you have an alternative solution I would love to see it... always willing to learn.
// render the image content and obtain the pixel data
RenderTargetBitmap rtb = new RenderTargetBitmap();
Size imageSize = new Size(panelImageManipulation.ActualWidth, panelImageManipulation.ActualHeight);
await rtb.RenderAsync(scrollImageManipulation, (int)imageSize.Width, (int)imageSize.Height);
IBuffer buffer = await rtb.GetPixelsAsync();

// write the image to file
StorageFile transformedFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("TransformedImage.jpg", CreationCollisionOption.ReplaceExisting);

using (var transformedStream = await transformedFile.OpenAsync(FileAccessMode.ReadWrite))
{
   BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, transformedStream);
   encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint)imageSize.Width, (uint)imageSize.Height, 96, 96, buffer.ToArray());
   await encoder.FlushAsync();
}

Open in new window

0
 

Author Closing Comment

by:Navigator101
ID: 40588235
Solved after documentation research and sitting on sofa solving problems.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question