// The DrawPixel method updates the WriteableBitmap by using // unsafe code to write a pixel into the back buffer. static void DrawPixel(MouseEventArgs e) { int column = (int)e.GetPosition(i).X; int row = (int)e.GetPosition(i).Y; // Reserve the back buffer for updates. writeableBitmap.Lock(); unsafe { // Get a pointer to the back buffer. int pBackBuffer = (int)writeableBitmap.BackBuffer; // Find the address of the pixel to draw. pBackBuffer += row * writeableBitmap.BackBufferStride; pBackBuffer += column * 4; // Compute the pixel's color. int color_data = 255 << 16; // R color_data |= 128 << 8; // G color_data |= 255 << 0; // B // Assign the color data to the pixel. *((int*)pBackBuffer) = color_data; } // Specify the area of the bitmap that changed. writeableBitmap.AddDirtyRect(new Int32Rect(column, row, 1, 1)); // Release the back buffer and make it available for display. writeableBitmap.Unlock(); }
private void Control_MouseMove(object sender, MouseEventArgs e) { var draggableControl = sender as UserControl; if (isDragging && draggableControl != null) { Point currentPosition = e.GetPosition(this.Parent as UIElement); var transform = draggableControl.RenderTransform as TranslateTransform; if (transform == null) { transform = new TranslateTransform(); draggableControl.RenderTransform = transform; } transform.X = currentPosition.X - clickPosition.X; transform.Y = currentPosition.Y - clickPosition.Y; } }
static void ErasePixel(MouseEventArgs e) { byte[] ColorData = { 0, 0, 0, 0 }; // B G R Int32Rect rect = new Int32Rect( (int)(e.GetPosition(i).X), (int)(e.GetPosition(i).Y), 1, 1); writeableBitmap.WritePixels(rect, ColorData, 4, 0); }
private void MainPage_MouseMove(object sender, MouseEventArgs e) { if (!_isDragging) return; if (!SearchPopup.IsOpen) SearchPopup.IsOpen = true; Point position = e.GetPosition(null); SearchPopup.HorizontalOffset = position.X; SearchPopup.VerticalOffset = position.Y; }