Đọc thêm:
Trước tiên, muốn lưu ảnh ta cần thêm Package.appxmanifest | Capabilities | Pictures Libary
a.) Phương thức quan trọng nhất là chụp màn hình, lưu ảnh, chia sẻ nó
private async Task<bool> GetShareContent(DataRequest request)
{
//BEGIN
bool succeeded = false;
DataRequestDeferral deferral = request.GetDeferral();
// 0. Cature Screen
var bitmap = new RenderTargetBitmap();
await bitmap.RenderAsync(MyBorder); //Your UIElement Here <================================
// 1. Create folder and file
var folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("MySample", CreationCollisionOption.OpenIfExists);
StorageFile file = await folder.CreateFileAsync("ScreenShot.png", CreationCollisionOption.GenerateUniqueName);
// 2. Get the pixels & Write the pixels to a InMemoryRandomAccessStream
var displayInformation = DisplayInformation.GetForCurrentView();
IBuffer pixelBuffer = await bitmap.GetPixelsAsync();
byte[] pixels = pixelBuffer.ToArray();
using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)bitmap.PixelWidth,
(uint)bitmap.PixelHeight,
displayInformation.LogicalDpi,
displayInformation.LogicalDpi,
pixelBuffer.ToArray());
await encoder.FlushAsync();
// 3. Share it for all
DataPackage requestData = request.Data;
// It's recommended to use both SetBitmap and SetStorageItems for sharing a single image
// since the target app may only support one or the other.
List<IStorageItem> imageItems = new List<IStorageItem>();
imageItems.Add(file);
requestData.SetStorageItems(imageItems);
//requestData.SetBitmap(RandomAccessStreamReference.CreateFromStream(stream));
requestData.Properties.Description = "ShareContent for Windows #BravoHex";
requestData.Properties.Title = "Wow, this my screenshot :-)";
//requestData.Properties.ApplicationName = "DemoApp";
//requestData.SetApplicationLink(new Uri("http://www.bravohex.com"));
}
//END
deferral.Complete();
return succeeded;
}
b.) Đây là phương thức sự kiện Share Content. Nó được gọi khi đăng ký
- Bạn có thể thông báo lỗi qua đoạn mã sau:
e.Request.FailWithDisplayText("Show Error...!");
private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs e)
{
// Call the scenario specific function to populate the datapackage with the data to be shared.
if (await GetShareContent(e.Request))
{
// Out of the datapackage properties, the title is required. If the scenario completed successfully, we need
// to make sure the title is valid since the sample scenario gets the title from the user.
if (String.IsNullOrEmpty(e.Request.Data.Properties.Title))
{
e.Request.FailWithDisplayText("Error...!");
}
}
}
c.) Bạn phải đăng ký sự kiện Share Content trước khi thực hiện hoặc hủy
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// Register the current page as a share source.
this.dataTransferManager = DataTransferManager.GetForCurrentView();
this.dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
}
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// Unregister the current page as a share source.
this.dataTransferManager.DataRequested -= new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
}
d.) Thực hiện Share Content
private void Button_Click(object sender, RoutedEventArgs e)
{
DataTransferManager.ShowShareUI();
}
Đây là app demo:
Ta sẽ chỉ chụp Border ở giữa. Kết quả sau khi chụp và chia sẻ:
Craeted: 29/03/2016
Source code: TakenScreenShotAndShareContent
[UWP] Take Screenshot With Custom Size and Share It
Related Tags :Screenshot ShareContent UWP
No comments:
Post a Comment
Commets Download Photoshop Actions, Lightroom Presets, PSD Template, Mockups, Stocks, Vectors, Fonts. Download free