Exemple #1
0
        private async void ExportImage(object sender, RoutedEventArgs e)
        {
            if (selection.Width == 0 || selection.Height == 0)
            {
                MessageDialog md = new MessageDialog(Strings.EmptySelectionMessage);
                md.ShowAsync();
                return;
            }
            var picker = new FileSavePicker();

            picker.FileTypeChoices.Add("png", new List <string> {
                ".png"
            });
            picker.DefaultFileExtension = ".png";

            StorageFile file = await picker.PickSaveFileAsync();

            if (file != null)
            {
                var saveStream = await file.OpenStreamForWriteAsync();

                var crop = new C1Bitmap((int)selection.Width, (int)selection.Height);
                crop.BeginUpdate();
                for (int x = 0; x < selection.Width; ++x)
                {
                    for (int y = 0; y < selection.Height; ++y)
                    {
                        crop.SetPixel(x, y, bitmap.GetPixel(x + (int)selection.X, y + (int)selection.Y));
                    }
                }
                crop.EndUpdate();
                var readStream = crop.GetStream(ImageFormat.Png, true);
                var data       = new byte[readStream.Length];
                readStream.Read(data, 0, (int)readStream.Length);
                saveStream.Write(data, 0, data.Length);
                saveStream.Dispose();
            }
        }
        void Warp(C1Bitmap src, C1Bitmap dst, Point start, Point end)
        {
            dst.BeginUpdate();
            dst.Copy(src, false);

            var dist                = Distance(start, end);
            var affectedDist        = dist * 1.5;
            var affectedDistSquared = affectedDist * affectedDist;

            for (int row = 0; row < dst.Height; ++row)
            {
                for (int col = 0; col < dst.Width; ++col)
                {
                    var point = new Point(col, row);
                    if (DistanceSq(start, point) > affectedDistSquared)
                    {
                        continue;
                    }
                    if (DistanceSq(end, point) < 0.25)
                    {
                        dst.SetPixel(col, row, src.GetPixel((int)start.X, (int)start.Y));
                        continue;
                    }
                    var dir = new Point(point.X - end.X, point.Y - end.Y);
                    var t   = IntersectRayCircle(end, dir, start, affectedDist);
                    TryT(-end.X / dir.X, ref t);
                    TryT(-end.Y / dir.Y, ref t);
                    TryT((dst.Width - end.X) / dir.X, ref t);
                    TryT((dst.Height - end.X) / dir.X, ref t);
                    var anchor = new Point(end.X + (point.X - end.X) * t, end.Y + (point.Y - end.Y));
                    var x      = start.X + (anchor.X - start.X) / t;
                    var y      = start.Y + (anchor.Y - start.Y) / t;
                    dst.SetPixel(col, row, src.GetInterpolatedPixel(x, y));
                }
            }
            dst.EndUpdate();
        }