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.Windows;
- 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;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.ComponentModel;
- namespace ImageWPFApp
- {
- /// <summary>
- /// Логика взаимодействия для MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- public class WBitmap: INotifyPropertyChanged
- {
- private WriteableBitmap wb;
- public WriteableBitmap Bitmap
- {
- get
- {
- return this.wb;
- }
- set
- {
- if (value != this.wb)
- {
- this.wb = value;
- NotifyPropertyChanged("Bitmap");
- }
- }
- }
- protected void NotifyPropertyChanged(String propertyName)
- {
- if (PropertyChanged != null)
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- public event PropertyChangedEventHandler PropertyChanged;
- }
- private string path = System.IO.Path.Combine(Environment.CurrentDirectory, "picture.jpg");
- public WBitmap wb { get; set; }
- private double lastBrightness = 0;
- private byte ToByte(int Value)
- {
- if (Value < 0) Value = 0;
- if (Value > 255) Value = 255;
- return (byte)Value;
- }
- private void Window_Loaded(object sender, RoutedEventArgs e) // тут биндинг
- {
- wb = new WBitmap { Bitmap = new WriteableBitmap(new BitmapImage(new Uri(path))) };
- Binding binding = new Binding("Bitmap");
- binding.Source = wb;
- //binding.Mode = BindingMode.OneWay;
- image.SetBinding(System.Windows.Controls.Image.SourceProperty, binding);
- }
- private void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- var transform = new RotateTransform(360.0 - rotateSlider.Value);
- image.RenderTransform = transform;
- }
- private void brightnessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) // <---
- {
- if (wb == null) return; // в этой функции wb будет меняться
- wb.Bitmap.Lock();
- // в Слайдере у меня Min 0, Max 200, перевожу в шкалу от -100 до 100
- double brightness = brightnessSlider.Value - 100;
- int delta = (int)(brightness - lastBrightness);
- int width = (int)wb.Bitmap.Width;
- int height = (int)wb.Bitmap.Height;
- int stride = wb.Bitmap.BackBufferStride;
- int bytesPerPixel = (wb.Bitmap.Format.BitsPerPixel + 7) / 8;
- unsafe
- {
- IntPtr pBackBuffer = wb.Bitmap.BackBuffer;
- byte* pBuff = (byte*)pBackBuffer.ToPointer();
- for (int row = 0; row < height; row++)
- for (int col = 0; col < width; col++)
- for (int k = 0; k < bytesPerPixel-1; k++)
- {
- int index = row * width * bytesPerPixel + col * bytesPerPixel + k;
- pBuff[index] = ToByte(pBuff[index] + delta);
- }
- }
- lastBrightness = brightness;
- Title = lastBrightness.ToString();
- wb.Bitmap.Unlock();
- }
- private void CreateThumbnail(string filename, BitmapSource image5)
- {
- if (filename != string.Empty)
- {
- using (FileStream stream5 = new FileStream(filename, FileMode.Create))
- {
- JpegBitmapEncoder encoder5 = new JpegBitmapEncoder();
- encoder5.Frames.Add(BitmapFrame.Create(image5));
- encoder5.Save(stream5);
- }
- }
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- CreateThumbnail("changedImage.jpg", wb.Bitmap.Clone());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement