这个事比较复杂,不过相对与FYJ.Winfrom中实现的可是简单许多了,这之前有点小问题,就是启动后要点两次才会换色,后来就一次,始终没打找到原因
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 | private void CanvImage_sMouseDown( object sender, MouseButtonEventArgs e) { Point p = e.GetPosition((IInputElement)sender); this .ellipsePixel_s.SetValue(Canvas.LeftProperty, p.X - 4); this .ellipsePixel_s.SetValue(Canvas.TopProperty, p.Y - 4); this .ellipsePixel_s.Tag = (p.X - 4) + "," + (p.Y - 4); ChangeColor(); } private void CanvImage_hlMouseDown( object sender, MouseButtonEventArgs e) { Point p = e.GetPosition((IInputElement)sender); this .ellipsePixel_hl.SetValue(Canvas.LeftProperty, p.X - 4); this .ellipsePixel_hl.SetValue(Canvas.TopProperty, p.Y - 4); this .ellipsePixel_hl.Tag = (p.X - 4) + "," + (p.Y - 4); ChangeColor(); } |
后来把坐标位置保存到tag属性可以了...
来看看XAML代码
1 | < br > |
后台代码
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | using FYJ.Winui.Util; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace FYJ.Winui { /// /// ColorPickerWindow.xaml 的交互逻辑 /// public partial class ColorPickerWindow : Window { private bool isLoaded = false ; public ColorPickerWindow() { InitializeComponent(); this .Loaded += delegate { //ChangePostion(this.CurrentColor); try { Point point_hl = this .ellipsePixel_hl.TranslatePoint( new Point(0, 0), this .CanvImage_hl); this .ellipsePixel_hl.Tag = point_hl.X + "," + point_hl.Y; Point point_s = this .ellipsePixel_s.TranslatePoint( new Point(0, 0), this .CanvImage_s); this .ellipsePixel_s.Tag = point_s.X + "," + point_s.Y; ChangeColor(); InitSkinImage(); } catch (Exception ex) { MessageBox.Show(ex.Message); } isLoaded = true ; }; } /// /// 加载初始图片选择器 /// private void InitSkinImage() { WrapPanel wp = new WrapPanel(); ControlTemplate skinTemplate = (ControlTemplate)App.Current.Resources[ "skinTemplate" ]; Button btn = new Button(); btn.Template = skinTemplate; wp.Children.Add(btn); } public delegate void ColorChangedEventHandler(System.Windows.Media.Color color); public event ColorChangedEventHandler ColorChanged; public delegate void SkinChangedEventHandler(BitmapSource imageSource); public event SkinChangedEventHandler SkinChanged; public delegate void OpacityChangedEventHandler( int value); public event OpacityChangedEventHandler OpacityChanged; [DllImport( "gdi32.dll" , SetLastError = true )] private static extern bool DeleteObject(IntPtr hObject); private BitmapSource ChangeBitmapToImageSource(System.Drawing.Bitmap bitmap) { //IntPtr hBitmap = bitmap.GetHbitmap(); //ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( // hBitmap, // IntPtr.Zero, // Int32Rect.Empty, // BitmapSizeOptions.FromEmptyOptions()); //if (!DeleteObject(hBitmap)) //{ // throw new System.ComponentModel.Win32Exception(); //} //return wpfBitmap; BitmapSource bs = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); return bs; } private void ChangeColor() { //获取ellipsePixel_s相对于CanvImage_s坐标方法一 //GeneralTransform generalTransform1 = this.CanvImage_s.TransformToVisual(this.ellipsePixel_s); // Point point_s = generalTransform1.Transform(new Point(0, 0)); //获取ellipsePixel_s相对于CanvImage_s坐标方法二 //Point point_s = this.ellipsePixel_s.TranslatePoint(new Point(0, 0), this.CanvImage_s); double x_s = Convert.ToDouble( this .ellipsePixel_s.Tag.ToString().Split( ',' )[0]); double y_s = Convert.ToDouble( this .ellipsePixel_s.Tag.ToString().Split( ',' )[1]); Point point_s = new Point(x_s, y_s); System.Drawing.Bitmap bm_hl = new System.Drawing.Bitmap(360, 100); for ( int i = 0; i < 360; i++) { for ( int j = 0; j < 100; j++) { bm_hl.SetPixel(i, j, new HSLColor(255, i, (point_s.X + 4) / 360.0, j / 100.0).Color); } } this .pickerImage_hl.Source = ChangeBitmapToImageSource(bm_hl); double x_hl = Convert.ToDouble( this .ellipsePixel_hl.Tag.ToString().Split( ',' )[0]); double y_hl = Convert.ToDouble( this .ellipsePixel_hl.Tag.ToString().Split( ',' )[1]); Point point_hl = new Point(x_hl, y_hl); System.Drawing.Bitmap bm_s = new System.Drawing.Bitmap(360, ( int ) this .pickerImage_s.Height); for ( int i = 0; i < 360; i++) { for ( int j = 0; j < this .pickerImage_s.Height; j++) { bm_s.SetPixel(i, j, new HSLColor(255, ( int )point_hl.X + 4, (i + 4) / 360.0, (point_hl.Y + 4) / 100.0).Color); } } this .pickerImage_s.Source = ChangeBitmapToImageSource(bm_s); HSLColor hc = new HSLColor(); hc.Hue = ( int )point_hl.X + 4; hc.Saturation = (point_s.X + 4) / 360.0; hc.Lightness = (point_hl.Y + 4) / 100.0; System.Drawing.Color co = hc.Color; this .CurrentColor = hc.Color; System.Windows.Media.Color c = new Color(); c.R = co.R; c.G = co.G; c.B = co.B; c.A = 255; if (ColorChanged != null && isLoaded) { ColorChanged(c); //触发事件 } } private void ChangePostion(System.Drawing.Color color) { try { HSLColor hsl = new HSLColor(color); this .ellipsePixel_hl.SetValue(Canvas.LeftProperty, hsl.Hue > 352 ? 352 : hsl.Hue); this .ellipsePixel_hl.SetValue(Canvas.TopProperty, hsl.Lightness > 0.91 ? 92 : hsl.Lightness * 100); this .ellipsePixel_s.SetValue(Canvas.LeftProperty, hsl.Saturation > 0.96 ? 352 : hsl.Saturation * 360); this .ellipsePixel_s.SetValue(Canvas.TopProperty, 6); } catch (Exception ex) { } } private System.Drawing.Color _currentColor = System.Drawing.Color.FromArgb(21, 160, 253); public System.Drawing.Color CurrentColor { get { return _currentColor; } set { _currentColor = value; } } public HSLColor CurrentHSL { get ; set ; } private void CanvImage_sMouseDown( object sender, MouseButtonEventArgs e) { Point p = e.GetPosition((IInputElement)sender); this .ellipsePixel_s.SetValue(Canvas.LeftProperty, p.X - 4); this .ellipsePixel_s.SetValue(Canvas.TopProperty, p.Y - 4); this .ellipsePixel_s.Tag = (p.X - 4) + "," + (p.Y - 4); ChangeColor(); } private void CanvImage_hlMouseDown( object sender, MouseButtonEventArgs e) { Point p = e.GetPosition((IInputElement)sender); this .ellipsePixel_hl.SetValue(Canvas.LeftProperty, p.X - 4); this .ellipsePixel_hl.SetValue(Canvas.TopProperty, p.Y - 4); this .ellipsePixel_hl.Tag = (p.X - 4) + "," + (p.Y - 4); ChangeColor(); } private void SkinBtnClick( object sender, RoutedEventArgs e) { Button button = sender as Button; object tag = button.Tag; string imagePath = ( string )tag; if (SkinChanged != null && tag != null ) { BitmapSource imageSource = null ; if (Regex.IsMatch(imagePath, "/.*?;" )) { { } imageSource = new BitmapImage( new Uri(imagePath)); } else { imagePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + imagePath; FileStream fs = new FileStream(imagePath, FileMode.Open); System.Drawing.Image img = System.Drawing.Image.FromStream(fs); fs.Close(); var bitmap = new System.Drawing.Bitmap(img); imageSource = ChangeBitmapToImageSource(bitmap); } SkinChanged(imageSource); } } private void Slider_ValueChanged( object sender, RoutedPropertyChangedEventArgs e) { if (OpacityChanged != null ) { OpacityChanged(100 - ( int )e.NewValue); } } private void btnAddPicClick( object sender, RoutedEventArgs e) { //Microsoft.Win32.OpenFileDialog op = new Microsoft.Win32.OpenFileDialog(); //if(op.ShowDialog().Value) //{ // MessageBox.Show("true"); //} //else //{ // MessageBox.Show("false"); //} System.Windows.Forms.OpenFileDialog fb = new System.Windows.Forms.OpenFileDialog(); fb.ShowDialog(); } } } |