[UWP]如何在UWP的irandomaccessstream中添加文本?

人气:285 发布:2022-09-22 其他开发语言 wpdevelop

问题描述

这是我的功能记录ip camera。如何在我拥有的每个图像(_frameBuffer)中插入时间。当我完成记录后,我会收到一个视频,其中包含我添加到每张图片中的图片和时间。

public async void Record(byte[] _frameBuffer)
{
using (IRandomAccessStream stream = new InMemoryRandomAccessStream())
{
   await stream.WriteAsync(_frameBuffer.AsBuffer());
   stream.Seek(0);

   var decoder = await BitmapDecoder.CreateAsync(stream);
   PixelDataProvider dataProvider = await decoder.GetPixelDataAsync(
          BitmapPixelFormat.Bgra8
          BitmapAlphaMode.Straight
          new BitmapTransform { ScaledWidth = this.PixelWidth ScaledHeight = this.PixelHeight }
          ExifOrientationMode.RespectExifOrientation
          ColorManagementMode.ColorManageToSRgb);
    if (this.PictureWriter != null)
          PictureWriter?.Addframe(dataProvider.DetachPixelData() (int)this.PixelWidth (int)this.PixelHeight);
    stream.Dispose();
}
}

我非常感谢所有人的帮助 我尝试过: 我找到了一个可以将文本插入图像的解决方案。但在我的情况下,我还没有找到解决方法。

private async void Button_Click(object sender RoutedEventArgs e)
{
    var picker = new FileOpenPicker();
    picker.FileTypeFilter.Add(".png");
    picker.FileTypeFilter.Add(".jpg");
    picker.FileTypeFilter.Add(".jpeg");
    var sourceFile = await picker.PickSingleFileAsync();

    if (sourceFile == null) { return; }

    var device = CanvasDevice.GetSharedDevice();
    var image = default(CanvasBitmap);
    using (var s = await sourceFile.OpenReadAsync())
    {
        image = await CanvasBitmap.LoadAsync(device s);
    }
    var offscreen = new CanvasRenderTarget(
        device (float)image.Bounds.Width (float)image.Bounds.Height 96);

    using (var ds = offscreen.CreateDrawingSession())
    {
        ds.DrawImage(image 0 0);
        ds.DrawText("Hello world" 10 10 Colors.Blue);
    }
    var displayInformation = DisplayInformation.GetForCurrentView();
    var savepicker = new FileSavePicker();
    savepicker.FileTypeChoices.Add("png" new List<string> { ".png" });
    var destFile = await savepicker.PickSaveFileAsync();

    using (var s = await destFile.OpenAsync(FileAccessMode.ReadWrite))
    {
        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId s);
        encoder.SetPixelData(
            BitmapPixelFormat.Bgra8
            BitmapAlphaMode.Ignore
            (uint)offscreen.Size.Width
            (uint)offscreen.Size.Height
            displayInformation.LogicalDpi
            displayInformation.LogicalDpi
            offscreen.GetPixelBytes());
        await encoder.FlushAsync();
    }
}

推荐答案

这是我的功能记录ip camera。如何在我拥有的每个图像(_frameBuffer)中插入时间。当我完成记录时,我得到一个包含图像的视频和我添加到每个图像的时间。

public async void Record(byte[] _frameBuffer)
{
using (IRandomAccessStream stream = new InMemoryRandomAccessStream())
{
   await stream.WriteAsync(_frameBuffer.AsBuffer());
   stream.Seek(0);

   var decoder = await BitmapDecoder.CreateAsync(stream);
   PixelDataProvider dataProvider = await decoder.GetPixelDataAsync(
          BitmapPixelFormat.Bgra8
          BitmapAlphaMode.Straight
          new BitmapTransform { ScaledWidth = this.PixelWidth ScaledHeight = this.PixelHeight }
          ExifOrientationMode.RespectExifOrientation
          ColorManagementMode.ColorManageToSRgb);
    if (this.PictureWriter != null)
          PictureWriter?.Addframe(dataProvider.DetachPixelData() (int)this.PixelWidth (int)this.PixelHeight);
    stream.Dispose();
}
}

我非常感谢所有人的帮助 yle ="border:0px; font-family:'Segoe UI','Lucida Grande',Verdana,Arial,Helvetica,sans-serif; margin:0px; outline:0px; padding: 0px;颜色:#454545; font-size:14px">我试过的: 我找到了一个可以解决的问题将文本插入图像。但就我而言,我还没有办法解决它。

private async void Button_Click(object sender RoutedEventArgs e)
{
    var picker = new FileOpenPicker();
    picker.FileTypeFilter.Add(".png");
    picker.FileTypeFilter.Add(".jpg");
    picker.FileTypeFilter.Add(".jpeg");
    var sourceFile = await picker.PickSingleFileAsync();

    if (sourceFile == null) { return; }

    var device = CanvasDevice.GetSharedDevice();
    var image = default(CanvasBitmap);
    using (var s = await sourceFile.OpenReadAsync())
    {
        image = await CanvasBitmap.LoadAsync(device s);
    }
    var offscreen = new CanvasRenderTarget(
        device (float)image.Bounds.Width (float)image.Bounds.Height 96);

    using (var ds = offscreen.CreateDrawingSession())
    {
        ds.DrawImage(image 0 0);
        ds.DrawText("Hello world" 10 10 Colors.Blue);
    }
    var displayInformation = DisplayInformation.GetForCurrentView();
    var savepicker = new FileSavePicker();
    savepicker.FileTypeChoices.Add("png" new List<string> { ".png" });
    var destFile = await savepicker.PickSaveFileAsync();

    using (var s = await destFile.OpenAsync(FileAccessMode.ReadWrite))
    {
        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId s);
        encoder.SetPixelData(
            BitmapPixelFormat.Bgra8
            BitmapAlphaMode.Ignore
            (uint)offscreen.Size.Width
            (uint)offscreen.Size.Height
            displayInformation.LogicalDpi
            displayInformation.LogicalDpi
            offscreen.GetPixelBytes());
        await encoder.FlushAsync();
    }
}

586