Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Drawing;
  8. using System.Drawing.Drawing2D;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Navigation;
  16. using System.Windows.Shapes;
  17.  
  18. namespace Paint
  19. {
  20.      
  21.     public partial class MainWindow : Window
  22.     {
  23.         bool paint = false;
  24.  
  25.         bool brushTool = true;
  26.  
  27.         int prevX = 0;
  28.         int prevY = 0;
  29.  
  30.         WriteableBitmap wBitmap = BitmapFactory.New(2048, 2048);
  31.  
  32.         public MainWindow()
  33.         {
  34.             InitializeComponent();
  35.  
  36.             RenderOptions.SetBitmapScalingMode(img, BitmapScalingMode.NearestNeighbor);
  37.             RenderOptions.SetEdgeMode(img, EdgeMode.Aliased);
  38.  
  39.             for(int x = 0; x < wBitmap.Height; x++)
  40.             {
  41.                 for (int y = 0; y < wBitmap.Width; y++)
  42.                 {
  43.                     wBitmap.SetPixel(x, y, 255, 255, 255);
  44.                 }
  45.             }
  46.             img.Source = wBitmap;
  47.         }    
  48.  
  49.         private void clrPicker_SelectedColorChanged(object sender, RoutedPropertyChangedEventArgs<System.Windows.Media.Color?> e)
  50.         {
  51.            
  52.         }
  53.  
  54.         private void image_MouseDown(object sender, MouseButtonEventArgs e)
  55.         {
  56.             prevX = (int)(e.GetPosition(img).X);
  57.             prevY = (int)(e.GetPosition(img).Y);
  58.             paint = true;
  59.         }
  60.  
  61.         private void image_MouseUp(object sender, MouseButtonEventArgs e)
  62.         {
  63.             prevX = 0;
  64.             prevY = 0;
  65.             paint = false;
  66.         }
  67.  
  68.         private void image_MouseMove(object sender, MouseEventArgs e)
  69.         {
  70.             if (paint)
  71.             {
  72.                 if (brushTool)
  73.                 {
  74.                     DrawPixel(e);
  75.                 }
  76.             }
  77.         }
  78.  
  79.         private void DrawPixel(MouseEventArgs e)
  80.         {
  81.             int column = (int)e.GetPosition(img).X;
  82.             int row = (int)e.GetPosition(img).Y;
  83.  
  84.             try
  85.             {
  86.                 // Reserve the back buffer for updates.
  87.                 wBitmap.Lock();
  88.  
  89.                 unsafe
  90.                 {
  91.                     // Get a pointer to the back buffer.
  92.                     IntPtr pBackBuffer = wBitmap.BackBuffer;
  93.  
  94.                     // Find the address of the pixel to draw.
  95.                     pBackBuffer += row * wBitmap.BackBufferStride;
  96.                     pBackBuffer += column * 4;
  97.  
  98.                     // Compute the pixel's color.
  99.                     int color_data = 0 << 0; // R
  100.                     color_data |= 0 << 0;   // G
  101.                     color_data |= 0 << 0;   // B
  102.  
  103.                     // Assign the color data to the pixel.
  104.                     *((int*)pBackBuffer) = color_data;
  105.                 }
  106.  
  107.                 // Specify the area of the bitmap that changed.
  108.                 wBitmap.AddDirtyRect(new Int32Rect(column, row, 1, 1));
  109.             }
  110.             finally
  111.             {
  112.                 // Release the back buffer and make it available for display.
  113.                 wBitmap.Unlock();
  114.             }
  115.         }
  116.        
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement