protected override void OnProcess(PixelRegion sourcePixelRegion, PixelRegion targetPixelRegion)
        {
            if(Mask == null)
            {
                //copy all pixel
                Array.Copy(sourcePixelRegion.ImagePixels,targetPixelRegion.ImagePixels,sourcePixelRegion.ImagePixels.Length);
                return;
            }

            //create grayscale buffer
            var buffer = new Byte[(int)(sourcePixelRegion.Bounds.Width*sourcePixelRegion.Bounds.Height)];

            //interface grayscale buffer
            var bitmapMask = new Bitmap(
                new Windows.Foundation.Size(sourcePixelRegion.Bounds.Width, sourcePixelRegion.Bounds.Height),
                ColorMode.Gray8,
                (uint)sourcePixelRegion.Bounds.Width,
                buffer.AsBuffer());
            //load grayscale buffer
            Mask.GetBitmapAsync(bitmapMask,OutputOption.Stretch).AsTask().Wait();

            sourcePixelRegion.ForEachRow((index, width, pos) =>
            {
                for (int x = 0; x < width; ++x, ++index)
                {
                    uint color = sourcePixelRegion.ImagePixels[index];

                    // copy grayscale buffer to alpha channel
                    var a = buffer[index];
                    uint rgb = (color & 0x00FFFFFF);
                    targetPixelRegion.ImagePixels[index] = rgb | (uint)(a << 24);
                }
            });
        }
Пример #2
0
        public static async Task<BitmapImage> SaveImageToCacheAndGetImage2(Byte[] imageArray, string fileName)
        {
            try
            {
                var folder = ApplicationData.Current.LocalFolder;

                var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

                var bitmapImage = new BitmapImage();

                using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    await stream.WriteAsync(imageArray.AsBuffer());

                    stream.Seek(0);
                    await bitmapImage.SetSourceAsync(stream);
                    stream.Dispose();
                }


                return bitmapImage;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return null;
            }
        }
Пример #3
0
 private void PostSocketRead(Int32 length)
 {
     try
     {
         var buffer = new Byte[length];
         var asyncOperationWithProgress = _socket.InputStream.ReadAsync(buffer.AsBuffer(), (UInt32)length, InputStreamOptions.Partial);
         asyncOperationWithProgress.Completed += (info, status) =>
         {
               switch (status)
             {
                 case AsyncStatus.Completed:
                 case AsyncStatus.Error:
                 {
                     try
                     {
                         IBuffer results = info.GetResults();
                         OnDataReadCompletion(results.Length, DataReader.FromBuffer(results));
                     }
                     catch (Exception exception)
                     {
                         Debug.WriteLine(String.Concat("Read operation failed:  ", exception.Message));
                     }
                     break;
                 }
             }
         };
     }
     catch (Exception exception1)
     {
         Debug.WriteLine(String.Concat("failed to post a read failed with error:  ", exception1.Message));
     }
 }