private void MainCanvas_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { currentAction = Actions.rotating; if (SelectedImage == null) { var image = e.Source as Image; if (image == baseImage) { return; } SelectedImage = image; } if (MainCanvas.CaptureMouse()) { initialClickPosition.X = Canvas.GetLeft(SelectedImage) + SelectedImage.ActualWidth / 2;; initialClickPosition.Y = Canvas.GetTop(SelectedImage) + SelectedImage.ActualHeight / 2;; rotateLine.X1 = initialClickPosition.X; rotateLine.Y1 = initialClickPosition.Y; rotateLine.X2 = e.GetPosition(MainCanvas).X; rotateLine.Y2 = e.GetPosition(MainCanvas).Y; oldAngle = Math2D.AngleBetween(new Vector(1, 0), new Vector(rotateLine.X2 - rotateLine.X1, rotateLine.Y2 - rotateLine.Y1)); Panel.SetZIndex(SelectedImage, 2); } }
private void CompressedImage_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { WriteableBitmap final = new WriteableBitmap(Fixtures.compressedBMimage); int width = (int)final.Width; int height = (int)final.Height; int stride = final.PixelWidth * 4; int size = final.PixelHeight * stride; byte[] pixelsSourceToRead = new byte[size]; final.CopyPixels(pixelsSourceToRead, stride, 0); byte[] pixelsSourceToWrite = new byte[size]; final.CopyPixels(pixelsSourceToWrite, stride, 0); var clickedPoint = e.GetPosition(CompressedImage); List <int> lenghts = new List <int>(); for (int i = 0; i < final.PixelWidth; ++i) { lenghts.Add(DrawVisibleLine(clickedPoint, new Point(i, 0), pixelsSourceToRead, pixelsSourceToWrite, stride)); } for (int i = 0; i < final.PixelHeight; ++i) { lenghts.Add(DrawVisibleLine(clickedPoint, new Point(final.PixelHeight, i), pixelsSourceToRead, pixelsSourceToWrite, stride)); } for (int i = 0; i < final.PixelHeight; ++i) { lenghts.Add(DrawVisibleLine(clickedPoint, new Point(0, i), pixelsSourceToRead, pixelsSourceToWrite, stride)); } for (int i = 0; i < final.PixelWidth; ++i) { lenghts.Add(DrawVisibleLine(clickedPoint, new Point(i, final.PixelHeight), pixelsSourceToRead, pixelsSourceToWrite, stride)); } Math2D.DrawSquareRGB(pixelsSourceToWrite, clickedPoint, 0, 255, 100); final.WritePixels(new Int32Rect(0, 0, width, height), pixelsSourceToWrite, width * 4, 0); CompressedImage.Source = final; MessageBox.Show(lenghts.Max().ToString()); }
private int DrawVisibleLine(Point start, Point end, byte[] pixelsSource, byte[] pixelsSourceToWrite, int stride) { var points = Math2D.line(start, end); int pixel = 0; //if we start in a fixture we can see all the fixture, move on till we get out of it. while (pixel < points.Count && Math2D.GetKindOfTerrain(pixelsSource, points[pixel]) == groundColor.fixture) { Math2D.SetPixeDarkRed(pixelsSourceToWrite, points[pixel]); pixel++; } //now we are for sure in the open. Keep on till the next fixture while (pixel < points.Count && Math2D.GetKindOfTerrain(pixelsSource, points[pixel]) != groundColor.fixture) { var obj = Math2D.GetKindOfTerrain(pixelsSource, points[pixel]); if (obj == groundColor.open) { Math2D.SetPixelRed(pixelsSourceToWrite, points[pixel]); } else { Math2D.SetPixelRGB(pixelsSourceToWrite, points[pixel], 220, 160, 100); } pixel++; } //we are in the last fixture we will see. Keep on untill we reach it's limit while (pixel < points.Count && Math2D.GetKindOfTerrain(pixelsSource, points[pixel]) == groundColor.fixture) { Math2D.SetPixeDarkRed(pixelsSourceToWrite, points[pixel]); pixel++; } return(pixel); }
private void MainCanvas_MouseMove(object sender, MouseEventArgs e) { if (currentAction == Actions.dragging) { var position = e.GetPosition(MainCanvas); var offset = position - initialClickPosition; initialClickPosition = position; Canvas.SetLeft(SelectedImage, Canvas.GetLeft(SelectedImage) + offset.X); Canvas.SetTop(SelectedImage, Canvas.GetTop(SelectedImage) + offset.Y); } else if (currentAction == Actions.rotating) { var position = e.GetPosition(MainCanvas); var angle = Math2D.AngleBetween(new Vector(1, 0), new Vector(rotateLine.X2 - rotateLine.X1, rotateLine.Y2 - rotateLine.Y1)); if (angle != 0) { var offset = angle - oldAngle; oldAngle = angle; rotateLine.X2 = position.X; rotateLine.Y2 = position.Y; Fixtures.FindByImage(SelectedImage).Rotate(offset); } } }
private void CompresImageToColor(byte r, byte g, byte b, bool BackGroundWhite = false) { RenderTargetBitmap rtb = new RenderTargetBitmap((int)MainCanvas.RenderSize.Width, (int)MainCanvas.RenderSize.Height, 96d, 96d, System.Windows.Media.PixelFormats.Default); rtb.Render(MainCanvas); var bitmapEncoder = new PngBitmapEncoder(); bitmapEncoder.Frames.Add(BitmapFrame.Create(rtb)); var temporalBitmap = new BitmapImage(); using (var stream = new MemoryStream()) { bitmapEncoder.Save(stream); stream.Seek(0, SeekOrigin.Begin); temporalBitmap.BeginInit(); temporalBitmap.CacheOption = BitmapCacheOption.OnLoad; temporalBitmap.StreamSource = stream; temporalBitmap.EndInit(); } WriteableBitmap temporalSource; if (Fixtures.compressedBMimage == null) { Fixtures.compressedBMimage = new WriteableBitmap(temporalBitmap); temporalSource = Fixtures.compressedBMimage; } else { temporalSource = new WriteableBitmap(temporalBitmap); } int width = (int)temporalSource.Width; int height = (int)temporalSource.Height; Math2D.stride = temporalSource.PixelWidth * 4; int size = temporalSource.PixelHeight * Math2D.stride; byte[] pixelsSource = new byte[size]; temporalSource.CopyPixels(pixelsSource, Math2D.stride, 0); byte[] pixelsToWrite = new byte[size]; if (Fixtures.compressedBMimage != null) { Fixtures.compressedBMimage.CopyPixels(pixelsToWrite, Math2D.stride, 0); } for (int y = 0; y < height; y++) { for (int x = 0; x < width; ++x) { if (Math2D.IsPixelDifferntThanWhie(pixelsSource, new Point(x, y))) { Math2D.SetPixelRGB(pixelsToWrite, new Point(x, y), r, g, b); } else { if (BackGroundWhite) { Math2D.SetPixelRGB(pixelsToWrite, new Point(x, y), 255, 255, 255); } } } } Fixtures.compressedBMimage.WritePixels(new Int32Rect(0, 0, width, height), pixelsToWrite, width * 4, 0); CompressedImage.Source = Fixtures.compressedBMimage; }