public override async void OnDrawCommit(IModel sender, PointerPoint args) { base.OnDrawCommit(sender, args); var area = sender.ElemArea as Border; if (area.Child == null) { return; } VModel.vm.Loading = true; var rb = await(area.Child as FrameworkElement).Render(); do { if (rb == null) { break; } sender.CurrentLayer.getRect(out Rect or, out WriteableBitmap ob); var layer = sender.CurrentLayer; var pos = (area.Child as FrameworkElement).RenderTransform as TranslateTransform; var rect = new Rect(pos.X, pos.Y, rb.PixelWidth, rb.PixelHeight); var nr = RectHelper.Intersect(or.IsEmpty ? rect : RectHelper.Union(rect, or), DrawRect); if (nr.IsEmpty) { break; } var i = sender.Layers.IndexOf(layer); var b = sender.CurrentLayer.Bitmap.Clone(); var nb = new WriteableBitmap((int)Math.Ceiling(nr.Width), (int)Math.Ceiling(nr.Height)); IGrap.addImg(b, nb, -(int)Math.Floor(nr.X - or.Left), -(int)Math.Floor(nr.Y - or.Top)); IGrap.addImg(rb, nb, -(int)Math.Floor(nr.X - rect.X), -(int)Math.Floor(nr.Y - rect.Y)); Exec.Do(new Exec() { exec = () => { sender.Layers[i].setRect(nr, nb); }, undo = () => { sender.Layers[i].setRect(or, ob); sender.CurrentLayer = sender.Layers[i]; } }); } while (false); VModel.vm.Loading = false; sender.ElemArea.Child = null; }
async Task Render(IModel sender, FrameworkElement child) { if (child == null) { return; } var rb = await child.Render(); if (rb == null) { return; } var layer = sender.CurrentLayer; var pos = child.RenderTransform as TranslateTransform; var rect = new Rect(pos.X, pos.Y, rb.PixelWidth, rb.PixelHeight); var or = orect; var nr = RectHelper.Intersect(rect, DrawRect); if (nr.IsEmpty) { return; } var i = sender.Layers.IndexOf(layer); var b = sender.CurrentLayer.Bitmap.Clone(); var nt = (child as TextBlock).Text + "\t" + Size + "\t" + FontName; var ot = otxt; var nb = new WriteableBitmap((int)Math.Ceiling(nr.Width), (int)Math.Ceiling(nr.Height)); var ob = obmp; // IGrap.addImg(b, nb, -(int)Math.Floor(nr.X - or.Left), -(int)Math.Floor(nr.Y - or.Top)); IGrap.addImg(rb, nb, -(int)Math.Floor(nr.X - rect.X), -(int)Math.Floor(nr.Y - rect.Y)); sender.ElemArea.Child = null; Exec.Do(new Exec() { exec = () => { sender.Layers[i].setRect(nr, nb); sender.Layers[i].Name = nt; sender.CurrentLayer = sender.Layers[i]; }, undo = () => { sender.Layers[i].setRect(or, ob); sender.Layers[i].Name = ot; sender.CurrentLayer = sender.Layers[i]; } }); }
private void OnExp(object sender, RoutedEventArgs e) { var i = Items.IndexOf(CurrentLayer); if (i + 1 >= Items.Count) { return; } var _u = Items[i]; var _d = Items[i + 1]; _u.getRect(out Rect ur, out WriteableBitmap ub); _d.getRect(out Rect dr, out WriteableBitmap db); var exm = RectHelper.Union(ur, dr); if (exm.IsEmpty) { return;//感谢反馈@microsoft } var ex = new WriteableBitmap((int)exm.Width, (int)exm.Height); IGrap.addImg(db, ex, (int)(_d.X - exm.X), (int)(_d.Y - exm.Y), _d.Opacity); IGrap.addImg(ub, ex, (int)(_u.X - exm.X), (int)(_u.Y - exm.Y), _u.Opacity); Exec.Do(new Exec() { exec = () => { Items.RemoveAt(i + 1); Items[i].setRect(exm, ex); }, undo = () => { Items[i].setRect(ur, ub); Items.Insert(i + 1, new LayerModel { Bitmap = db, X = dr.X, Y = dr.Y, }); }, }); }
public override void OnToolState(IModel sender, bool args) { Debug.WriteLine("picker"); if (PickAll) { bmp = new WriteableBitmap((int)DrawRect.Width, (int)DrawRect.Height); foreach (var item in sender.Layers.Reverse()) { if (!item.IsShow) { continue; } IGrap.addImg(item.Bitmap, bmp, (int)item.X, (int)item.Y, item.Opacity); } } else { bmp = new WriteableBitmap((int)DrawRect.Width, (int)DrawRect.Height); var item = sender.CurrentLayer; IGrap.addImg(item.Bitmap, bmp, (int)item.X, (int)item.Y, item.Opacity); } }
public override async void OnDrawCommit(IModel sender, PointerPoint args) { VModel.vm.Loading = true; var gdi = new Graphics(); gdi.Color = Color; var layer = sender.CurrentLayer; layer.getRect(out Rect orec, out WriteableBitmap obmp); var nbmp = new WriteableBitmap((int)DrawRect.Width, (int)DrawRect.Height); var mask = new WriteableBitmap((int)DrawRect.Width, (int)DrawRect.Height); var nrec = new Rect(0, 0, nbmp.PixelWidth, nbmp.PixelHeight); IGrap.copyImg(obmp, nbmp, (int)orec.X, (int)orec.Y); if (PickAll) { foreach (var item in sender.Layers.Reverse()) { if (!item.IsShow) { continue; } IGrap.addImg(item.Bitmap, mask, (int)item.X, (int)item.Y, item.Opacity); } } else { IGrap.copyImg(obmp, mask, (int)orec.X, (int)orec.Y); } var p = new Point(args.Position.X, args.Position.Y); if (Clipper.IsCliping) { var Bitmap = new WriteableBitmap((int)DrawRect.Width, (int)DrawRect.Height); gdi.SetBitmap(Bitmap); layer.setRect(nrec, nbmp);//bug bug bug await gdi.DrawBucker((int)p.X, (int)p.Y, mask.PixelBuffer.ToArray()); //await Task.Delay(1000); layer.Child = Clipper.createPolygon(Bitmap); var bmp = await layer.Child.Render(); var pos = (Point)layer.Child.Tag; IGrap.addImg(bmp, nbmp, (int)Math.Round(pos.X), (int)Math.Round(pos.Y)); layer.Child = null; nbmp.Invalidate();//bug bug bug } else { gdi.SetBitmap(nbmp); await gdi.DrawBucker((int)p.X, (int)p.Y, mask.PixelBuffer.ToArray()); } var i = sender.Layers.IndexOf(layer); Exec.Do(new Exec() { exec = () => { sender.Layers[i].setRect(nrec, nbmp); }, undo = () => { sender.Layers[i].setRect(orec, obmp); } }); VModel.vm.Loading = false; }
public override async void OnDrawCommit(IModel sender, PointerPoint args) { if (rect.IsEmpty) { OnDrawRollback(sender, args); return; } var layer = sender.CurrentLayer; if (Clipper.IsCliping) { //sender.X.Source = bmp; VModel.vm.Loading = true; var bmp = await layer.Child.Render(); VModel.vm.Loading = false; var pos = (Point)layer.Child.Tag; IGrap.addImg(bmp, layer.Bitmap, (int)Math.Round(pos.X), (int)Math.Round(pos.Y)); layer.Child = null; } if (merge) { var or = orec; var ob = obmp; var nr = RectHelper.Intersect(RectHelper.Union(rect, orec), DrawRect); var i = sender.Layers.IndexOf(layer); var nb = IGrap.clipImg(layer.Bitmap, nr); Exec.Do(new Exec() { exec = () => { sender.Layers[i].setRect(nr, nb); }, undo = () => { sender.Layers[i].setRect(or, ob); sender.CurrentLayer = sender.Layers[i]; } }); } else { var or = orec; var nr = rect; var nb = IGrap.clipImg(layer.Bitmap, nr); layer.setRect(orec, obmp); nr.X -= or.X; nr.Y -= or.Y; var ob = IGrap.clipImg(obmp, nr); var i = sender.Layers.IndexOf(layer); Exec.Do(new Exec() { exec = () => { layer = sender.Layers[i]; IGrap.copyImg(nb, layer.Bitmap, (int)Math.Floor(nr.X), (int)Math.Floor(nr.Y)); layer.Bitmap.Invalidate(); }, undo = () => { layer = sender.Layers[i]; IGrap.copyImg(ob, layer.Bitmap, (int)Math.Floor(nr.X), (int)Math.Floor(nr.Y)); layer.Bitmap.Invalidate(); sender.CurrentLayer = sender.Layers[i]; } }); } }
public async Task SaveFile(StorageFile file, IList <LayerModel> ls, int x, int y) { await Dispatcher.RunIdleAsync(_ => { }); Loading = true; StorageFolder f = null; try { f = await StorageFolder.GetFolderFromPathAsync(file.Path.Replace(file.Name, "")); } catch (Exception) { } try { WriteableBitmap ot = new WriteableBitmap(x, y); foreach (var item in ls.Reverse()) { if (!item.IsShow) { continue; } IGrap.addImg(item.Bitmap, ot, (int)item.X, (int)item.Y, item.Opacity); } switch (file.FileType.ToLower()) { case ".jpg": await SaveIMG(ot, file, BitmapEncoder.JpegEncoderId); break; case ".gif": await SaveIMG(ot, file, BitmapEncoder.GifEncoderId); return; //xuy case ".png": await SaveIMG(ot, file, BitmapEncoder.PngEncoderId); if (f != null) { file = await f.CreateFileAsync(file.Name.Replace(".png", ".psd"), CreationCollisionOption.OpenIfExists); using (var s = await file.OpenStreamForWriteAsync()) SavePSD(s, ls, x, y); } break; case ".psd": using (var s = await file.OpenStreamForWriteAsync()) SavePSD(s, ls, x, y); if (f != null) { file = await f.CreateFileAsync(file.Name.Replace(".psd", ".png"), CreationCollisionOption.OpenIfExists); await SaveIMG(ot, file, BitmapEncoder.PngEncoderId); } break; default: throw new Exception("ntype"); } } catch (Exception e) { new MessageDialog(e.ToString()).ShowMux(); } Loading = false; }
public override async void OnToolState(IModel sender, bool state) { var layer = sender.CurrentLayer; if (!layer.IsEdit || !layer.IsShow) { return; } if (layer.Bitmap == null) { return; } Debug.WriteLine("state:" + state); if (state) { layer.getRect(out orec, out obb); if (Clipper.IsCliping) { VModel.vm.Loading = true; //拷贝选区 var Bitmap = new WriteableBitmap((int)DrawRect.Width, (int)DrawRect.Height); IGrap.copyImg(obb, Bitmap, (int)orec.X, (int)orec.Y); layer.Child = Clipper.createPolygon(Bitmap); var p = (Point)layer.Child.Tag; var xb = await(layer.Child.Render()); Clipper.Points.Clear(); layer.Child = null; if (xb != null) { var i = sender.Layers.IndexOf(layer); var nb = obb; var ob = obb; if (layer.Bitmap != null) { nb = layer.Bitmap.Clone(); IGrap.delImg(xb, nb, (int)(p.X - orec.X), (int)(p.Y - orec.Y)); nb.Invalidate(); } obb = xb; orec = new Rect(p.X, p.Y, xb.PixelWidth, xb.PixelHeight); Exec.Do(new Exec() { exec = () => { sender.Layers[i].Bitmap = nb; sender.Layers.Insert(i, new LayerModel() { Bitmap = xb, X = p.X, Y = p.Y }); sender.CurrentLayer = sender.Layers[i]; }, undo = () => { sender.Layers.RemoveAt(i); sender.CurrentLayer = sender.Layers[i]; sender.CurrentLayer.Bitmap = ob; } }); } VModel.vm.Loading = false; } sender.ElemArea.Child = CreateRect(sender); ch = false; } else { sender.ElemArea.Child = null; if (!ch) { return; } var vc = new CompositeTransform() { Rotation = com.Rotation, ScaleX = com.ScaleX, ScaleY = com.ScaleY, CenterX = com.TranslateX + layer.W / 2, CenterY = com.TranslateY + layer.H / 2 }; var or = orec; var ob = obb; layer.getRect(out Rect nr, out WriteableBitmap nb); if (com.ScaleX != 1 || com.ScaleY != 1 || com.Rotation != 0) { var elem = (FrameworkElement)((DrawPanel)sender).ITEMS.ContainerFromItem(layer); VModel.vm.Loading = true; var b = await(elem).Render(); VModel.vm.Loading = false; var vr = vc.TransformBounds(nr); nr = RectHelper.Intersect(DrawRect, vr); if (nr.IsEmpty) { nb = null; layer.setRect(nr, nb); } else { nb = new WriteableBitmap((int)nr.Width, (int)nr.Height); IGrap.addImg(b, nb, (int)(vr.X - nr.X), (int)(vr.Y - nr.Y)); layer.setRect(nr, nb); } com.Rotation = 0; com.ScaleX = 1; com.ScaleY = 1; } var i = sender.Layers.IndexOf(sender.CurrentLayer); Exec.Save(new Exec() { exec = () => { sender.Layers[i].setRect(nr, nb); }, undo = () => { sender.Layers[i].setRect(or, ob); } }); } }