如何分享截图?

时间:2013-07-28 07:58:09

标签: c# windows-phone-7 xaml windows-phone-8

我想在Twitter,Facebook等上分享应用程序的屏幕截图。这是我的代码:它保存图片,但不打开共享媒体任务。我知道问题出在路径上:{

var wb = new WriteableBitmap(LayoutRoot, new TranslateTransform());
using (var mediaLibrary = new MediaLibrary()) {
  using (var stream = new MemoryStream()) {
    var fileName = string.Format("{0}.jpg", DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss"));
    wb.SaveJpeg(stream, wb.PixelWidth, wb.PixelHeight, 0, 100);
    stream.Seek(0, SeekOrigin.Begin);
    mediaLibrary.SavePicture(fileName, stream);
    shareMediaTask = new ShareMediaTask();
    shareMediaTask.FilePath = fileName;
    shareMediaTask.Show();
  }
}

如何获取已保存图片的路径?

是不是可以简单地截取屏幕截图并分享而不将其保存在手机上?

2 个答案:

答案 0 :(得分:0)

要获取MediaLibrary文件的真实路径,您需要使用GetPath()扩展方法,例如;

using Microsoft.Xna.Framework.Media.PhoneExtensions;

...

var picture = mediaLibrary.SavePicture(fileName, stream);
shareMediaTask = new ShareMediaTask();
shareMediaTask.FilePath = picture.GetPath();
shareMediaTask.Show();

答案 1 :(得分:0)

为了共享屏幕截图,不需要保存图像,在Windows 8.1中它非常容易。

这是代码,享受!

async void dataTransferMgr_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            DataRequest request = args.Request;
            request.Data.Properties.Title = "Title";
            request.Data.Properties.Description = "brief description";
            request.Data.SetText("detailed information");


            RandomAccessStreamReference imageStreamRef = await ScreenshotToStreamReferenceAsync(yourChartControlName);
            request.Data.Properties.Thumbnail = imageStreamRef;
            request.Data.SetBitmap(imageStreamRef);

        }

private async Task ScreenshotToStreamAsync(FrameworkElement element, IRandomAccessStream stream)
{
var renderTargetBitmap = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(element);

var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();

var dpi = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().LogicalDpi;

var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)renderTargetBitmap.PixelWidth,
(uint)renderTargetBitmap.PixelHeight,
dpi,
dpi,
pixelBuffer.ToArray());

await encoder.FlushAsync();
}

private async Task<RandomAccessStreamReference> ScreenshotToStreamReferenceAsync(FrameworkElement element)
{
var ms = new InMemoryRandomAccessStream();
await ScreenshotToStreamAsync(element, ms);
ms.Seek(0);
return RandomAccessStreamReference.CreateFromStream(ms);
}