/// <summary> /// Palette color from CanvasBitmap. /// </summary> /// <param name="bimap"> Win2D CanvasBitmap. </param> /// <returns> Color </returns> private static Color GetPaletteFormBitmap(CanvasBitmap bimap) { try { //scale double scaleX = width / bimap.Size.Width; double scaleY = height / bimap.Size.Height; Vector2 v = new Vector2((float)scaleX, (float)scaleY); Matrix3x2 m = Matrix3x2.CreateScale(v); //draw using (CanvasRenderTarget target = new CanvasRenderTarget(device, width, height, bimap.Dpi)) { using (CanvasDrawingSession ds = target.CreateDrawingSession()) { Transform2DEffect effect = new Transform2DEffect { Source = bimap, TransformMatrix = m }; ds.DrawImage(effect); } //Palette Color[] colors = target.GetPixelColors(); return GetPaletteFromColors(colors); } } catch (Exception) { return FallBackColor; } }
//由魔棒得到区域 public static ICanvasImage GetMagicWandr(CanvasRenderTarget SourceBitmap, int x, int y, float dragDistance) { Color clickColor = SourceBitmap.GetPixelColors(x, y, 1, 1).Single(); return(new ColorMatrixEffect { //ChromaKeyEffect:浓度帧作用 //(替换指定的颜色和透明度) Source = new ChromaKeyEffect { Source = SourceBitmap, Color = clickColor, //指定的颜色 Tolerance = dragDistance, //容差 InvertAlpha = true //反转α }, ColorMatrix = new Matrix5x4 { // 保持alpha M44 = 1, // 设置RGB =白色。 M51 = 1, M52 = 1, M53 = 1, } }); }
private void FillCanvas(CanvasRenderTarget canvas, object drawLock, Vector2 position) { // This seems pretty hacky... maybe dpi should be handled in the layer above the tools position *= (GraphicsInformation.Dpi / 96.0f); lock (drawLock) { var canvasSize = canvas.SizeInPixels.ToVector2(); var colors = canvas.GetPixelColors(); var rect = new Rect(0, 0, canvas.SizeInPixels.Width, canvas.SizeInPixels.Height); if ((position.X >= rect.Left && position.X <= rect.Right) || (position.Y >= rect.Top && position.Y <= rect.Bottom)) { var targetColor = colors[(int)(position.X + position.Y * canvasSize.X)]; if (targetColor != _color) { FloodPixel((int)position.X, (int)position.Y, colors, targetColor, canvasSize, rect); using (var bitmap = CanvasBitmap.CreateFromColors(_device, colors, (int)canvas.SizeInPixels.Width, (int)canvas.SizeInPixels.Height, GraphicsInformation.Dpi)) using (var drawingSession = canvas.CreateDrawingSession()) using (var layer = drawingSession.CreateLayer(1.0f, rect)) { drawingSession.DrawImage(bitmap); } } } } }
private static Color GetAverageColor(ICanvasResourceCreatorWithDpi resourceCreator, CanvasBitmap image) { var miniImage = new CanvasRenderTarget(resourceCreator, 1, 1, 96); using (var g = miniImage.CreateDrawingSession()) g.DrawImage(image, new Rect(0, 0, 1, 1)); return(miniImage.GetPixelColors(0, 0, 1, 1)[0]); }
public void SetPixelColorsReadHazards() { var device = new CanvasDevice(); var bitmap = CanvasBitmap.CreateFromColors(device, new Color[1], 1, 1); var renderTarget = new CanvasRenderTarget(device, 2, 1, 96); using (var ds = renderTarget.CreateDrawingSession()) { bitmap.SetPixelColors(new Color[] { Colors.Blue }); ds.DrawImage(bitmap, 0, 0); bitmap.SetPixelColors(new Color[] { Colors.Red }); ds.DrawImage(bitmap, 1, 0); } CollectionAssert.AreEqual(new Color[] { Colors.Blue, Colors.Red }, renderTarget.GetPixelColors()); }
public void SetPixelBytesReadHazards() { var device = new CanvasDevice(); var bitmap = CanvasBitmap.CreateFromBytes(device, new byte[4], 1, 1, DirectXPixelFormat.B8G8R8A8UIntNormalized); var renderTarget = new CanvasRenderTarget(device, 2, 1, 96); using (var ds = renderTarget.CreateDrawingSession()) { bitmap.SetPixelBytes(new byte[] { 255, 0, 0, 255 }); ds.DrawImage(bitmap, 0, 0); bitmap.SetPixelBytes(new byte[] { 0, 0, 255, 255 }); ds.DrawImage(bitmap, 1, 0); } CollectionAssert.AreEqual(new Color[] { Colors.Blue, Colors.Red }, renderTarget.GetPixelColors()); }
// Toggles the color of cells when they are clicked on. private void ProcessPointerInput(PointerRoutedEventArgs e) { if (!isPointerDown) { return; } // Invert the display transform, to convert pointer positions into simulation rendertarget space. Matrix3x2 transform; Matrix3x2.Invert(Utils.GetDisplayTransform(canvas.Size, canvas, simulationW, simulationH), out transform); foreach (var point in e.GetIntermediatePoints(canvas)) { if (!point.IsInContact) { continue; } var pos = Vector2.Transform(point.Position.ToVector2(), transform); var x = canvas.ConvertDipsToPixels(pos.X); var y = canvas.ConvertDipsToPixels(pos.Y); // If the point is within the bounds of the rendertarget, and not the same as the last point... if (x >= 0 && y >= 0 && x < simulationW && y < simulationH && ((x != lastPointerX || y != lastPointerY))) { // Read the current color. var cellColor = currentSurface.GetPixelColors(x, y, 1, 1); // Toggle the value. cellColor[0] = cellColor[0].R > 0 ? Colors.Black : Colors.White; // Set the new color. currentSurface.SetPixelColors(cellColor, x, y, 1, 1); lastPointerX = x; lastPointerY = y; } } }
public void Flush(int x, int y, int wx, int wy) { Session.Flush(); Color[] lc = target.GetPixelColors(); for (int i = 0; i < lc.Length; i++) { int posx = i % Width; int posy = i / Width; if (posx >= x && posx <= x + wx) { if (posy >= y && posy <= y + wy) { drawPixel(posx, posy, lc[i]); } } } }
void ApplyHitPoints() { lock (hitPoints) { foreach (var point in hitPoints) { var x = point.X; var y = point.Y; // Read the current color. var cellColor = currentSurface.GetPixelColors(x, y, 1, 1); // Toggle the value. cellColor[0] = cellColor[0].R > 0 ? Colors.Black : Colors.White; // Set the new color. currentSurface.SetPixelColors(cellColor, x, y, 1, 1); } hitPoints.Clear(); } }
public static Color GetPixelColor(this CanvasRenderTarget target, int left, int top) { var scale = 96.0 / target.Dpi; return(target.GetPixelColors((int)(left / scale), (int)(top / scale), 1, 1)[0]); }