Advertisement
Guest User

Untitled

a guest
May 21st, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. //using System.Windows.Shapes;
  14. using System.IO;
  15. using System.Drawing;
  16. using System.Drawing.Imaging;
  17. using System.ComponentModel;
  18.  
  19. namespace ImageWPFApp
  20. {
  21.     /// <summary>
  22.     /// Логика взаимодействия для MainWindow.xaml
  23.     /// </summary>
  24.     public partial class MainWindow : Window
  25.     {
  26.         public MainWindow()
  27.         {
  28.             InitializeComponent();
  29.         }
  30.  
  31.         public class WBitmap: INotifyPropertyChanged
  32.         {
  33.             private WriteableBitmap wb;
  34.  
  35.             public WriteableBitmap Bitmap
  36.             {
  37.                 get
  38.                 {
  39.                     return this.wb;
  40.                 }
  41.                 set
  42.                 {
  43.                     if (value != this.wb)
  44.                     {
  45.                         this.wb = value;
  46.                         NotifyPropertyChanged("Bitmap");
  47.                     }
  48.                 }
  49.             }
  50.  
  51.             protected void NotifyPropertyChanged(String propertyName)
  52.             {
  53.                 if (PropertyChanged != null)
  54.                     PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  55.             }
  56.  
  57.             public event PropertyChangedEventHandler PropertyChanged;
  58.         }
  59.  
  60.         private string path = System.IO.Path.Combine(Environment.CurrentDirectory, "picture.jpg");
  61.         public WBitmap wb { get; set; }
  62.         private double lastBrightness = 0;
  63.  
  64.         private byte ToByte(int Value)
  65.         {
  66.             if (Value < 0) Value = 0;
  67.             if (Value > 255) Value = 255;
  68.             return (byte)Value;
  69.         }
  70.  
  71.         private void Window_Loaded(object sender, RoutedEventArgs e) // тут биндинг
  72.         {
  73.             wb = new WBitmap { Bitmap = new WriteableBitmap(new BitmapImage(new Uri(path))) };
  74.             Binding binding = new Binding("Bitmap");
  75.             binding.Source = wb;
  76.             //binding.Mode = BindingMode.OneWay;
  77.             image.SetBinding(System.Windows.Controls.Image.SourceProperty, binding);
  78.            
  79.         }
  80.  
  81.         private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
  82.         {
  83.             var transform = new RotateTransform(360.0 - rotateSlider.Value);
  84.             image.RenderTransform = transform;
  85.         }
  86.  
  87.        
  88.         private void brightnessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) // <---
  89.         {
  90.             if (wb == null) return; // в этой функции wb будет меняться
  91.             wb.Bitmap.Lock();
  92.             // в Слайдере у меня Min 0, Max 200, перевожу в шкалу от -100 до 100
  93.             double brightness = brightnessSlider.Value - 100;
  94.             int delta = (int)(brightness - lastBrightness);
  95.             int width = (int)wb.Bitmap.Width;
  96.             int height = (int)wb.Bitmap.Height;
  97.             int stride = wb.Bitmap.BackBufferStride;
  98.             int bytesPerPixel = (wb.Bitmap.Format.BitsPerPixel + 7) / 8;
  99.             unsafe
  100.             {
  101.                 IntPtr pBackBuffer = wb.Bitmap.BackBuffer;
  102.                 byte* pBuff = (byte*)pBackBuffer.ToPointer();
  103.  
  104.                 for (int row = 0; row < height; row++)
  105.                     for (int col = 0; col < width; col++)
  106.                         for (int k = 0; k < bytesPerPixel-1; k++)
  107.                         {
  108.                             int index = row * width * bytesPerPixel + col * bytesPerPixel + k;
  109.                             pBuff[index] = ToByte(pBuff[index] + delta);
  110.                         }
  111.             }
  112.             lastBrightness = brightness;
  113.             Title = lastBrightness.ToString();
  114.             wb.Bitmap.Unlock();
  115.         }
  116.  
  117.         private void CreateThumbnail(string filename, BitmapSource image5)
  118.         {
  119.             if (filename != string.Empty)
  120.             {
  121.                 using (FileStream stream5 = new FileStream(filename, FileMode.Create))
  122.                 {
  123.                     JpegBitmapEncoder encoder5 = new JpegBitmapEncoder();
  124.                     encoder5.Frames.Add(BitmapFrame.Create(image5));
  125.                     encoder5.Save(stream5);
  126.                 }
  127.             }
  128.         }
  129.  
  130.         private void button1_Click(object sender, RoutedEventArgs e)
  131.         {
  132.             CreateThumbnail("changedImage.jpg", wb.Bitmap.Clone());
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement