Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace Paint
- {
- public partial class MainWindow : Window
- {
- bool paint = false;
- bool brushTool = true;
- int prevX = 0;
- int prevY = 0;
- WriteableBitmap wBitmap = BitmapFactory.New(2048, 2048);
- public MainWindow()
- {
- InitializeComponent();
- RenderOptions.SetBitmapScalingMode(img, BitmapScalingMode.NearestNeighbor);
- RenderOptions.SetEdgeMode(img, EdgeMode.Aliased);
- for(int x = 0; x < wBitmap.Height; x++)
- {
- for (int y = 0; y < wBitmap.Width; y++)
- {
- wBitmap.SetPixel(x, y, 255, 255, 255);
- }
- }
- img.Source = wBitmap;
- }
- private void clrPicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<System.Windows.Media.Color?> e)
- {
- }
- private void image_MouseDown(object sender, MouseButtonEventArgs e)
- {
- prevX = (int)(e.GetPosition(img).X);
- prevY = (int)(e.GetPosition(img).Y);
- paint = true;
- }
- private void image_MouseUp(object sender, MouseButtonEventArgs e)
- {
- prevX = 0;
- prevY = 0;
- paint = false;
- }
- private void image_MouseMove(object sender, MouseEventArgs e)
- {
- if (paint)
- {
- if (brushTool)
- {
- DrawPixel(e);
- }
- }
- }
- private void DrawPixel(MouseEventArgs e)
- {
- int column = (int)e.GetPosition(img).X;
- int row = (int)e.GetPosition(img).Y;
- try
- {
- // Reserve the back buffer for updates.
- wBitmap.Lock();
- unsafe
- {
- // Get a pointer to the back buffer.
- IntPtr pBackBuffer = wBitmap.BackBuffer;
- // Find the address of the pixel to draw.
- pBackBuffer += row * wBitmap.BackBufferStride;
- pBackBuffer += column * 4;
- // Compute the pixel's color.
- int color_data = 0 << 0; // R
- color_data |= 0 << 0; // G
- color_data |= 0 << 0; // B
- // Assign the color data to the pixel.
- *((int*)pBackBuffer) = color_data;
- }
- // Specify the area of the bitmap that changed.
- wBitmap.AddDirtyRect(new Int32Rect(column, row, 1, 1));
- }
- finally
- {
- // Release the back buffer and make it available for display.
- wBitmap.Unlock();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement