指数超出范围。

时间:2014-02-05 01:36:51

标签: windows bitmap microsoft-metro bitmapencoder

我现在想要使用BitmapEncoder / BitmapDecoder捕获一个Bitmap。以下是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();
        };


    }

说错误&#34;索引超出范围。必须是非负的且小于集合的大小。&#34;怎么解决?

0 个答案:

没有答案