public static void Pixel() { //Undo:撤销 Undo undo = new Undo(); undo.MaskInstantiation(App.Model.MaskRenderTarget, App.Model.MaskAnimatedTarget); App.UndoAdd(undo); //复制 CanvasRenderTarget crt = new CanvasRenderTarget(App.Model.AnimatedControl, App.Model.Width, App.Model.Height); crt.SetPixelBytes(App.Model.Layers[App.Model.Index].CanvasRenderTarget.GetPixelBytes()); App.Mask(App.Model.Layers[App.Model.Index].CanvasRenderTarget, crt); //重新设置描边 App.Model.isReStroke = true; App.Model.isAnimated = true; if (App.Model.isMask == true) { App.Model.isReRender = true; //重新渲染 App.Model.Refresh++; //画布刷新 } }
public async Task <SoftwareBitmap> SaveToBitmap(InkStrokeContainer ink) { ICanvasResourceCreator device = CanvasDevice.GetSharedDevice(); CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, Image.PixelWidth, Image.PixelHeight, 96); renderTarget.SetPixelBytes(new byte[Image.PixelWidth * 4 * Image.PixelHeight]); byte[] imageBytes = new byte[4 * Image.PixelWidth * Image.PixelHeight]; using (var ds = renderTarget.CreateDrawingSession()) { Image.CopyToBuffer(imageBytes.AsBuffer()); var win2dRenderedBitmap = CanvasBitmap.CreateFromBytes( device, imageBytes, Image.PixelWidth, Image.PixelHeight, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized, 96.0f); ds.DrawImage(win2dRenderedBitmap); IReadOnlyList <InkStroke> inklist = ink.GetStrokes(); ds.DrawInk(inklist); } var inkpixel = renderTarget.GetPixelBytes(); WriteableBitmap bmp = new WriteableBitmap((int)Image.PixelWidth, (int)Image.PixelHeight); Stream s = bmp.PixelBuffer.AsStream(); s.Seek(0, SeekOrigin.Begin); s.Write(inkpixel, 0, (int)Image.PixelWidth * 4 * (int)Image.PixelHeight); s.Position = 0; await s.ReadAsync(imageBytes, 0, (int)s.Length); Image.CopyFromBuffer(imageBytes.AsBuffer()); return(Image); }
public async Task <SoftwareBitmap> SaveToBitmap(InkStrokeContainer ink) { ICanvasResourceCreator device = CanvasDevice.GetSharedDevice(); CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, Width, Height, 96); var src = new byte[Width * 4 * Height]; for (int index = 0; index < src.Length; ++index) { src[index] = 255; } renderTarget.SetPixelBytes(src); byte[] imageBytes = new byte[4 * Width * Height]; using (var ds = renderTarget.CreateDrawingSession()) { IReadOnlyList <InkStroke> inklist = ink.GetStrokes(); ds.DrawInk(inklist); } var inkpixel = renderTarget.GetPixelBytes(); WriteableBitmap bmp = new WriteableBitmap(Width, Height); Stream s = bmp.PixelBuffer.AsStream(); s.Seek(0, SeekOrigin.Begin); s.Write(inkpixel, 0, Width * 4 * Height); s.Position = 0; await s.ReadAsync(imageBytes, 0, (int)s.Length); SoftwareBitmap result = new SoftwareBitmap(BitmapPixelFormat.Bgra8, Width, Height, BitmapAlphaMode.Premultiplied); result.CopyFromBuffer(imageBytes.AsBuffer()); return(result); }
public static CanvasRenderTarget GenImage(int width, int height, Color clr, byte alpha) { CanvasDevice device = CanvasDevice.GetSharedDevice(); CanvasRenderTarget offscreen = new CanvasRenderTarget(device, width, height, 96, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized, CanvasAlphaMode.Premultiplied); byte[] pixels = offscreen.GetPixelBytes(); if (pixels == null) { return(null); } uint col = 0; uint stride = offscreen.SizeInPixels.Width; for (uint row = 0; row < offscreen.SizeInPixels.Height; ++row) { uint total_row_len = (uint)(row * stride); for (col = 0; col < offscreen.SizeInPixels.Width; ++col) { uint index = (total_row_len + col) * 4; pixels[index + 3] = alpha; pixels[index + 2] = clr.R; pixels[index + 1] = clr.G; pixels[index] = clr.B; } } offscreen.SetPixelBytes(pixels); return(offscreen); }
private void MainBottom(XDocument xdoc)//Main:Bottom为项目文档 { //格式化 App.Formatting(); //读取数据 var dataInfo = from a in xdoc.Descendants("Layer") select new { LayerName = a.Element("LayerName").Value, LayerVisual = a.Element("LayerVisual"), LayerOpacity = a.Element("LayerOpacity"), LayerBlendIndex = a.Element("LayerBlendIndex"), LayerWidth = a.Element("LayerWidth"), LayerHeight = a.Element("LayerHeight"), CanvasRenderTarget = a.Element("CanvasRenderTarget").Value, }; //新建图层 foreach (var date in dataInfo) { //新建渲染目标 CanvasRenderTarget crt = new CanvasRenderTarget(App.Model.VirtualControl, (int)date.LayerWidth, (int)date.LayerHeight); string Target = date.CanvasRenderTarget; var bytes = Convert.FromBase64String(Target); crt.SetPixelBytes(bytes); //新建层 Layer l = new Layer { Name = date.LayerName, Visual = (bool)date.LayerVisual, Opacity = (int)date.LayerOpacity, BlendIndex = (int)date.LayerBlendIndex, CanvasRenderTarget = crt }; if (App.Model.isLowView) { l.LowView(); } else { l.SquareView(); } l.SetWriteableBitmap(App.Model.VirtualControl);//设置缩略图 App.Model.Layers.Add(l); } //属性 App.Model.Tool = (int)xdoc.Descendants("Tool").Single(); App.Model.Index = (int)xdoc.Descendants("Index").Single(); }
public void RecoverAfterDeviceLost() { if (regionMask != null) { regionMask.Dispose(); } if (currentRegionMask != null) { regionMask = new CanvasRenderTarget(SourceBitmap.Device, Parent.Size.X, Parent.Size.Y, 96); regionMask.SetPixelBytes(currentRegionMask); } cachedRegionMask.RecoverAfterDeviceLost(); }
//CanvasDrawingSession ls; public override void Invalidate(bool init = false) { if (init) { source.SetPixelBytes(b.PixelBuffer); } else { session.Dispose(); source.GetPixelBytes(b.PixelBuffer); b.Invalidate(); } session = source.CreateDrawingSession(); //session.Blend = Blend; }
public async Task <StorageFile> SaveDoodle() { storageFolder = KnownFolders.SavedPictures; //var file = await storageFolder.CreateFileAsync("ink.png", CreationCollisionOption.ReplaceExisting); if (ink_canvas.InkPresenter.StrokeContainer.GetStrokes().Count == 0) { finishfile = sourcefile; } else { CanvasDevice device = CanvasDevice.GetSharedDevice(); CanvasRenderTarget renderTarget = new CanvasRenderTarget(device, (int)ink_canvas.ActualWidth, (int)ink_canvas.ActualHeight, 96); renderTarget.SetPixelBytes(new byte[(int)ink_canvas.ActualWidth * 4 * (int)ink_canvas.ActualHeight]); using (var ds = renderTarget.CreateDrawingSession()) { IReadOnlyList <InkStroke> inklist = ink_canvas.InkPresenter.StrokeContainer.GetStrokes(); Debug.WriteLine("Ink_Strokes Count: " + inklist.Count); //ds.Clear(Colors.White); ds.DrawInk(inklist); } //直接存的ink //using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) //{ // await renderTarget.SaveAsync(fileStream, CanvasBitmapFileFormat.Png, 1f); //} var inkpixel = renderTarget.GetPixelBytes(); //var color = renderTarget.GetPixelColors(); WriteableBitmap bmp = new WriteableBitmap((int)ink_canvas.ActualWidth, (int)ink_canvas.ActualHeight); Stream s = bmp.PixelBuffer.AsStream(); s.Seek(0, SeekOrigin.Begin); s.Write(inkpixel, 0, (int)ink_canvas.ActualWidth * 4 * (int)ink_canvas.ActualHeight); WriteableBitmap ink_wb = await ImageProcessing.ResizeByDecoderAsync(bmp, sourceImage.PixelWidth, sourceImage.PixelHeight, true); //await SaveToFile("ink_scale.png", ink_wb); WriteableBitmap combine_wb = await ImageProcessing.CombineAsync(sourceImage, ink_wb); finishfile = await WriteableBitmapSaveToFile(combine_wb); } Clear(); return(finishfile); }
public void UndoRegionEdit() { if (previousRegionMask != null) { regionMask.SetPixelBytes(previousRegionMask); currentRegionMask = previousRegionMask; previousRegionMask = null; } else { regionMask.Dispose(); regionMask = null; currentRegionMask = null; } cachedRegionMask.Reset(); CanUndo = false; }
private static CanvasBitmap getGraphic(Color color) { if (Graphics.ContainsKey(color)) { return(Graphics[color]); } var g2 = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), (int)Graphics[Colors.White].Size.Width, (int)Graphics[Colors.White].Size.Height, DPI); g2.CopyPixelsFromBitmap(Graphics[Colors.White]); var arr = g2.GetPixelBytes(); for (int i = 0; i < arr.Length; i += 4) { arr[i] = (byte)(((float)arr[i] / 255) * ((float)color.B / 255) * 255); arr[i + 1] = (byte)(((float)arr[i + 1] / 255) * ((float)color.G / 255) * 255); arr[i + 2] = (byte)(((float)arr[i + 2] / 255) * ((float)color.R / 255) * 255); } g2.SetPixelBytes(arr); Graphics[color] = g2; return(g2); }
public static void Apply() { //Undo:撤销 Undo undo = new Undo(); undo.MaskInstantiation(App.Model.MaskRenderTarget, App.Model.MaskAnimatedTarget); App.UndoAdd(undo); //画选区(临时征用底选区渲染目标) using (CanvasDrawingSession ds = App.Model.SecondBottomRenderTarget.CreateDrawingSession()) { ds.Clear(Colors.Transparent); ds.DrawImage(App.Model.SecondCanvasImage); } //复制 CanvasRenderTarget crt = new CanvasRenderTarget(App.Model.AnimatedControl, App.Model.Width, App.Model.Height); crt.SetPixelBytes(App.Model.SecondBottomRenderTarget.GetPixelBytes()); App.Mask(App.Model.SecondBottomRenderTarget, crt); }
/// <summary> /// Loads a <see cref="CanvasBitmap"/> from an XElement. /// </summary> /// <param name="canvasResource"> The canvas-resource. </param> /// <param name="element"> The source XElement. </param> /// <returns> The loaded <see cref="CanvasBitmap"/>. </returns> public static CanvasBitmap LoadBitmap(ICanvasResourceCreatorWithDpi canvasResource, XElement element) { int width = 1024, height = 1024; if (element.Element("PixelWidth") is XElement pixelWidth) { width = (int)pixelWidth; } if (element.Element("PixelHeight") is XElement pixelHeight) { height = (int)pixelHeight; } CanvasRenderTarget bitmap = new CanvasRenderTarget(canvasResource, width, height); if (element.Element("PixelBytes") is XElement pixelBytes) { string strings = pixelBytes.Value; byte[] bytes = Convert.FromBase64String(strings); bitmap.SetPixelBytes(bytes); } return(null); }
public static CanvasRenderTarget GenImage(int width, int height, CanvasLinearGradientBrush brush, byte alpha) { CanvasDevice device = CanvasDevice.GetSharedDevice(); CanvasRenderTarget offscreen = new CanvasRenderTarget(device, width, height, 96, Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized, CanvasAlphaMode.Premultiplied); using (CanvasDrawingSession ds = offscreen.CreateDrawingSession()) { ds.FillRectangle(new Rect(0.0, 0.0, offscreen.SizeInPixels.Width, offscreen.SizeInPixels.Height), brush); } byte[] pixels = offscreen.GetPixelBytes(); if (pixels == null) { return(null); } uint col = 0; uint stride = offscreen.SizeInPixels.Width; for (uint row = 0; row < offscreen.SizeInPixels.Height; ++row) { uint total_row_len = (uint)(row * stride); for (col = 0; col < offscreen.SizeInPixels.Width; ++col) { uint index = (total_row_len + col) * 4; pixels[index + 3] = alpha; } } offscreen.SetPixelBytes(pixels); return(offscreen); }
private void DrawNewton(CanvasDrawingSession ds) { if (_parser?.RootNode == null) { return; } var width = _coords.Width; var height = _coords.Height; var xmin = _coords.XStart; var xmax = _coords.XEnd; var ymin = _coords.YStart; var ymax = _coords.YEnd; var xpixstep = (xmax - xmin) / width; var ypixstep = -(ymax - ymin) / height; var bitmap = new byte[width * height * 4]; // 4 bytes per pixel for (var i = 0; i < bitmap.Length; i++) { bitmap[i] = 0; } var toler = xpixstep * xpixstep * 0.1; var xGridStep = xpixstep * _nStep; var yGridStep = ypixstep * _nStep; for (var x1 = xmin; x1 < xmax; x1 += xGridStep) { for (var y1 = ymin; y1 < ymax; y1 -= yGridStep) { double score; if (_nStep > 1) { var widefact = 1; score = GetNewtonScore( x1 - xpixstep * widefact, y1 + ypixstep * widefact, x1 + xGridStep + xpixstep * widefact, y1 - yGridStep - ypixstep * widefact, x1 + xGridStep / 2, y1 - yGridStep / 2, 10, toler * 10 ); } else { score = 1; } if (score > 0) { for (int i = 0; i < _nStep; i++) { for (var j = 0; j < _nStep; j++) { var x2 = x1 + i * xpixstep; var y2 = y1 - j * ypixstep; score = GetNewtonScore( x2, y2, x2 + xpixstep, y2 - ypixstep, x2 + xpixstep / 2, y2 - ypixstep / 2, 5, toler ); if (score > 0) { var xn = (int)_coords.ToXPix(x2 + xpixstep / 2); var yn = (int)_coords.ToYPix(y2 - ypixstep / 2); SetPix(bitmap, xn, yn, (int)(255 * score)); } } } } } } var target = new CanvasRenderTarget(ds, _coords.Width, _coords.Height); target.SetPixelBytes(bitmap); ds.DrawImage(target); }
public static bool ApplyImageToMask( CanvasRenderTarget image, CanvasRenderTarget mask, CanvasRenderTarget canvas, Color maskColor, bool NoAlphaAtBoundary) { if (image == null || mask == null || canvas == null) { return(false); } byte[] pixelsImage = image.GetPixelBytes(); byte[] pixelsMask = mask.GetPixelBytes(); byte[] pixelsCanvas = canvas.GetPixelBytes(); if (pixelsImage == null || pixelsMask == null || pixelsCanvas == null) { return(false); } uint col = 0; uint image_stride = image.SizeInPixels.Width; uint stride = canvas.SizeInPixels.Width; uint mask_stride = mask.SizeInPixels.Width; for (uint row = 0; row < canvas.SizeInPixels.Height; ++row) { uint total_row_len = (uint)(row * stride); uint total_row_mask_len = (uint)(row * mask_stride); uint total_row_image_len = (uint)(row * image_stride); for (col = 0; col < canvas.SizeInPixels.Width; ++col) { if (row >= image.SizeInPixels.Height || col >= image.SizeInPixels.Width) { continue; } if (row >= mask.SizeInPixels.Height || col >= mask.SizeInPixels.Width) { continue; } uint index = (total_row_len + col) * 4; uint indexMask = (total_row_mask_len + col) * 4; uint indexImage = (total_row_image_len + col) * 4; byte maskByte = 0; if (MaskColor.IsEqual(maskColor, MaskColor.Red)) { maskByte = pixelsMask[indexMask + 2]; } else if (MaskColor.IsEqual(maskColor, MaskColor.Green)) { maskByte = pixelsMask[indexMask + 1]; } else if (MaskColor.IsEqual(maskColor, MaskColor.Blue)) { maskByte = pixelsMask[indexMask]; } if (maskByte > 0) { uint pixels_canvas = (uint)((pixelsCanvas[index + 3] << 24) | (pixelsCanvas[index + 2] << 16) | (pixelsCanvas[index + 1] << 8) | pixelsCanvas[index]); uint pixels_image = (uint)((pixelsImage[index + 3] << 24) | (pixelsImage[index + 2] << 16) | (pixelsImage[index + 1] << 8) | pixelsImage[index]); if (NoAlphaAtBoundary) { uint result = AlphablendNoAlphaAtBoundary(pixels_canvas, pixels_image, pixelsMask[indexMask + 3], pixelsMask[indexMask + 3]); pixelsCanvas[index + 3] = (byte)((result & 0xff000000) >> 24); pixelsCanvas[index + 2] = (byte)((result & 0xff0000) >> 16); pixelsCanvas[index + 1] = (byte)((result & 0xff00) >> 8); pixelsCanvas[index] = (byte)(result & 0xff); } else { uint result = Alphablend(pixels_canvas, pixels_image, pixelsMask[indexMask + 3], pixelsMask[indexMask + 3]); pixelsCanvas[index + 3] = (byte)((result & 0xff000000) >> 24); pixelsCanvas[index + 2] = (byte)((result & 0xff0000) >> 16); pixelsCanvas[index + 1] = (byte)((result & 0xff00) >> 8); pixelsCanvas[index] = (byte)(result & 0xff); } } } } canvas.SetPixelBytes(pixelsCanvas); return(true); }
public static bool ApplyShadowToMask( Color clrShadow, CanvasRenderTarget mask, CanvasRenderTarget canvas, Color maskColor, Point offset) { if (mask == null || canvas == null) { return(false); } byte[] pixelsMask = mask.GetPixelBytes(); byte[] pixelsCanvas = canvas.GetPixelBytes(); if (pixelsMask == null || pixelsCanvas == null) { return(false); } uint col = 0; uint stride = canvas.SizeInPixels.Width; uint mask_stride = mask.SizeInPixels.Width; for (uint row = 0; row < canvas.SizeInPixels.Height; ++row) { uint total_row_len = (uint)(row * stride); uint total_row_mask_len = (uint)((row - offset.Y) * mask_stride); for (col = 0; col < canvas.SizeInPixels.Width; ++col) { if ((row - offset.Y) >= mask.SizeInPixels.Height || (col - offset.X) >= mask.SizeInPixels.Width || (row - offset.Y) < 0 || (col - offset.X) < 0) { continue; } uint index = (total_row_len + col) * 4; uint indexMask = (uint)((total_row_mask_len + (col - offset.X)) * 4); byte maskByte = 0; if (MaskColor.IsEqual(maskColor, MaskColor.Red)) { maskByte = pixelsMask[indexMask + 2]; } else if (MaskColor.IsEqual(maskColor, MaskColor.Green)) { maskByte = pixelsMask[indexMask + 1]; } else if (MaskColor.IsEqual(maskColor, MaskColor.Blue)) { maskByte = pixelsMask[indexMask]; } uint color = (uint)(0xff << 24 | clrShadow.R << 16 | clrShadow.G << 8 | clrShadow.B); if (maskByte > 0) { uint pixels_canvas = (uint)((pixelsCanvas[index + 3] << 24) | (pixelsCanvas[index + 2] << 16) | (pixelsCanvas[index + 1] << 8) | pixelsCanvas[index]); uint result = Alphablend(pixels_canvas, color, pixelsMask[indexMask + 3], (byte)(pixelsMask[indexMask + 3] * clrShadow.A / 255)); pixelsCanvas[index + 3] = (byte)((result & 0xff000000) >> 24); pixelsCanvas[index + 2] = (byte)((result & 0xff0000) >> 16); pixelsCanvas[index + 1] = (byte)((result & 0xff00) >> 8); pixelsCanvas[index] = (byte)(result & 0xff); } } } canvas.SetPixelBytes(pixelsCanvas); return(true); }
public void RecoverAfterDeviceLost() { if (regionMask != null) { regionMask.Dispose(); } if (currentRegionMask != null) { regionMask = new CanvasRenderTarget(SourceBitmap.Device, Parent.Size.X, Parent.Size.Y, 96); regionMask.SetPixelBytes(currentRegionMask); } cachedRegionMask.RecoverAfterDeviceLost(); }