源碼下載:http://yunpan.cn/c3iNuHFFAcr8h 访问密码 8e48
還是先來看下截圖:
實現了幾個效果:放大、縮小、旋轉、左右翻轉、上下翻轉,亮度變化、灰度圖、對比度、高斯模糊。
放大、縮小代碼:
private void scale(float x) { scale_ *= x; using (var ds = board_.CreateDrawingSession()) { ds.Transform = Matrix3x2.CreateScale(scale_); ds.Clear(Colors.Black); ds.DrawImage(oriImage_); } transform.Invalidate(); } private void zoomIn_Click(object sender, RoutedEventArgs e) { scale(1.1f); } private void zoomOut_Click(object sender, RoutedEventArgs e) { scale(1 / 1.1f); }
旋轉代碼(注意,默認轉動方向是鐘錶方向——順時針):
private void rotate_Click(object sender, RoutedEventArgs e) { rotate_radians_ += 90; using (var ds = board_.CreateDrawingSession()) { ds.Transform = Matrix3x2.CreateRotation(rotate_radians_ * (float)Math.PI / 180, new Vector2((float)oriImageRc_.Width / 2.0f, (float)oriImageRc_.Height / 2.0f)); ds.Clear(Colors.Black); ds.DrawImage(oriImage_); } transform.Invalidate(); }
左右、上下翻轉代碼:
private void left_right_Click(object sender, RoutedEventArgs e) { left_right_direction_ = -left_right_direction_; using (var ds = board_.CreateDrawingSession()) { ds.Transform = Matrix3x2.CreateScale(new Vector2(left_right_direction_, top_bottom_direction_), new Vector2((float)oriImageRc_.Width / 2.0f, (float)oriImageRc_.Height / 2.0f)); ds.Clear(Colors.Black); ds.DrawImage(oriImage_); } transform.Invalidate(); } private void top_bottom_Click(object sender, RoutedEventArgs e) { top_bottom_direction_ = -top_bottom_direction_; using (var ds = board_.CreateDrawingSession()) { ds.Transform = Matrix3x2.CreateScale(new Vector2(left_right_direction_, top_bottom_direction_), new Vector2((float)oriImageRc_.Width / 2.0f, (float)oriImageRc_.Height / 2.0f)); ds.Clear(Colors.Black); ds.DrawImage(oriImage_); } transform.Invalidate(); }
亮度、灰度、對比度、高斯,均使用Effect效果,其效果放在名字空間:
Microsoft.Graphics.Canvas.Effects
具體代碼:
亮度,注意,我使用了WhitePoint,還有BlackPoint,請讀者自己試驗效果。而且亮度的範圍為:[0-1),注意閉包關係。
private void lightSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { if (board_ == null) return; using (var ds = board_.CreateDrawingSession()) { Microsoft.Graphics.Canvas.Effects.BrightnessEffect brightness = new Microsoft.Graphics.Canvas.Effects.BrightnessEffect(); if (e.NewValue >= 0.000001) { brightness.WhitePoint = new Vector2(0, (float)e.NewValue); } else { brightness.WhitePoint = new Vector2(-(float)e.NewValue, 0); } brightness.Source = oriImage_; ds.Clear(Colors.Black); ds.DrawImage(brightness); } transform.Invalidate(); }
灰度:
private void gray_Click(object sender, RoutedEventArgs e) { Microsoft.Graphics.Canvas.Effects.GrayscaleEffect grayEffect = new Microsoft.Graphics.Canvas.Effects.GrayscaleEffect(); grayEffect.Source = oriImage_; using (var ds = board_.CreateDrawingSession()) { ds.Clear(Colors.Black); ds.DrawImage(grayEffect); } transform.Invalidate(); }
對比度:
private void constrastSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { if (board_ == null) return; Microsoft.Graphics.Canvas.Effects.ContrastEffect constrastEffect = new Microsoft.Graphics.Canvas.Effects.ContrastEffect(); constrastEffect.Contrast = (float)e.NewValue; constrastEffect.Source = oriImage_; using (var ds = board_.CreateDrawingSession()) { ds.Clear(Colors.Black); ds.DrawImage(constrastEffect); } transform.Invalidate(); }
高斯:
private void gaussianSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) { if (board_ == null) return; Microsoft.Graphics.Canvas.Effects.GaussianBlurEffect gaussianEffect = new Microsoft.Graphics.Canvas.Effects.GaussianBlurEffect(); gaussianEffect.BlurAmount = (float)e.NewValue; gaussianEffect.Source = oriImage_; using (var ds = board_.CreateDrawingSession()) { ds.Clear(Colors.Black); ds.DrawImage(gaussianEffect); } transform.Invalidate(); }
时间: 2024-10-06 17:07:20