1

我现在想通过使用 BitmapEncoder/BitmapDecoder 来捕获位图。下面是 saveImageAsync 代码

 private async void SaveBitmapAsync()
    {
        List<byte> _pixelData = null;
        //convert the source bitmap to a pixel array
        var srcfile = await StorageFile.GetFileFromApplicationUriAsync(
          new Uri("ms-appx:///assets/ncs.jpg"));

        BitmapDecoder decoder = null;
        using (IRandomAccessStream stm =
          await srcfile.OpenAsync(FileAccessMode.Read))
        {
            decoder = await BitmapDecoder.CreateAsync(
              BitmapDecoder.JpegDecoderId, stm);
            _pixelData = (await decoder.GetPixelDataAsync()).
                DetachPixelData().ToList();
        };

        //do our pixel manipulation here
        List<Point> all = _allPoints.Select(stroke =>
        {
            var interpolationresults = stroke.SelectMany((pt, idx) =>
            {

                List<Point> result = new List<Point>();
                if (idx + 1 == stroke.Count) return result;
                Interpolate(stroke[idx], stroke[idx + 1], ref result);
                return result;
            }).ToList();
            return stroke.Concat(interpolationresults).ToList();
        }).SelectMany(list => list).ToList();



        foreach (Point p in all)
        {
            int idx = (int)(600 * ((int)p.Y - 1) + p.X) * 1;
            _pixelData[idx] = Colors.Red.B;
            _pixelData[idx + 1] = Colors.Red.G;
            _pixelData[idx + 2] = Colors.Red.R;
            _pixelData[idx + 3] = Colors.Red.A;
        }
        //write it back
        var destfile = await KnownFolders.PicturesLibrary.CreateFileAsync(
          string.Format("{0}_signature.jpg",
          DateTimeOffset.Now.ToString("MM_dd_yy_hh_mm_ss")),
          CreationCollisionOption.ReplaceExisting);
        using (IRandomAccessStream stm =
          await destfile.OpenAsync(FileAccessMode.ReadWrite))
        {
            BitmapEncoder encoder = await BitmapEncoder.CreateAsync(
              BitmapEncoder.JpegEncoderId, stm);
            encoder.SetPixelData(
              decoder.BitmapPixelFormat,
              BitmapAlphaMode.Straight,
              800, 535, decoder.DpiX, decoder.DpiY,
              _pixelData.ToArray());

            await encoder.FlushAsync();
        };


    }

错误提示“索引超出范围。必须为非负数且小于集合的大小。” 怎么解决?

4

0 回答 0