简体   繁体   English

使用C#在Windows Store App中裁剪并保存图像(UIElement)

[英]Crop and save image (UIElement) in Windows Store App using c# with

I already can take a print screen from the actual content of my application. 我已经可以从应用程序的实际内容中获取打印屏幕。 I choose az UIElement (eg a grid), and I render it into a bmp file. 我选择一个z UIElement(例如一个网格),然后将其渲染为bmp文件。

But how can I crop this image as i feel? 但是,我该如何裁剪我的图像呢? The code is below works, just the cropping missing. 下面的代码有效,只是缺少裁剪。 I work for windows 8.1. 我为Windows 8.1工作。

public async void SaveVisualElementToFile(UIElement element, StorageFile file)
    {
        var renderTargetBitmap = new RenderTargetBitmap();
        await renderTargetBitmap.RenderAsync(element);
        var pixels = await renderTargetBitmap.GetPixelsAsync();

        using (IRandomAccessStream stream = await 
               file.OpenAsync(FileAccessMode.ReadWrite))
        {
            var encoder = await BitmapEncoder.CreateAsync(
                                              BitmapEncoder.JpegEncoderId, stream);
            byte[] bytes = pixels.ToArray();
            encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                 BitmapAlphaMode.Ignore,
                                 (uint)renderTargetBitmap.PixelWidth, 
                                 (uint)renderTargetBitmap.PixelHeight,
                                 96, 96, bytes);

            await encoder.FlushAsync();
        }
    } 

There are a couple of ways to do this. 有两种方法可以做到这一点。 There's the traditional way as espoused by MSFT themself. MSFT本身就是拥护的传统方式 You can also use some extensions such as WinRTXamlToolkit and WriteableBitmapEx . 您还可以使用某些扩展程序,例如WinRTXamlToolkitWriteableBitmapEx The latter two make it quite easy. 后两个使它很容易。 Check in their source codes on Codeplex for their sample applications, which will have samples for how to use cropping. 在Codeplex上检查其源代码以获取其示例应用程序,其中将提供有关如何使用裁剪的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM