using ImageMagick; using IOTContainer.Common; using IOTContainer.ViewModel; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; 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.Windows.Threading; namespace IOTContainer.View { /// /// WeatherControl.xaml 的交互逻辑 /// public partial class WeatherControl : UserControl { DispatcherTimer WeatherTimer = new DispatcherTimer();//停止播放视频的定时器 string city = string.Empty; [DllImport("gdi32")] static extern int DeleteObject(IntPtr o); public static BitmapSource loadBitmap(System.Drawing.Bitmap source) { IntPtr ip = source.GetHbitmap(); BitmapSource bs = null; try { bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); } finally { DeleteObject(ip); } return bs; } private string fontColor = "#FFFFF"; public WeatherControl() { InitializeComponent(); WeatherTimer.Interval = TimeSpan.FromMinutes(180);//每秒刷新一次 WeatherTimer.Tick += new EventHandler(WeatherTimer_Tick); } public void LoadWeather(string city, int type, double width, double height, string backgroundColor, string fontColor) { this.Width = width; this.Height = height; this.city = city; this.fontColor = fontColor; BrushConverter converter = new BrushConverter(); if (!string.IsNullOrEmpty(backgroundColor)) { Brush newFill = (Brush)converter.ConvertFromString(backgroundColor); gridMain.Background = newFill; } if (!string.IsNullOrEmpty(fontColor)) { Brush newFill = (Brush)converter.ConvertFromString(fontColor); txtFL1.Foreground = newFill; txtAQI1.Foreground = newFill; txtAQI2.Foreground = newFill; txtFL2.Foreground = newFill; txtPM1.Foreground = newFill; txtPM2.Foreground = newFill; txtsd1.Foreground = newFill; txtsd2.Foreground = newFill; txtTemp1.Foreground = newFill; txtTemp2.Foreground = newFill; txtTempZ1.Foreground = newFill; txtsdp1.Foreground = newFill; txtAQIA1.Foreground = newFill; txtPMpm1.Foreground = newFill; txtPMV1.Foreground = newFill; txtTempZ2.Foreground = newFill; txtsdp2.Foreground = newFill; txtAQIA2.Foreground = newFill; txtPMpm2.Foreground = newFill; txtPMV2.Foreground = newFill; } gridMain.Width = width; gridMain.Height = height; style1.Width = width; style1.Height = height; style2.Width = width; style2.Height = height; BindWeather(); Color color = (Color)ColorConverter.ConvertFromString(fontColor); if (type == 0) { try { if (fontColor != "#FFFFFF") { sd.Source = loadBitmap(ReplaceColor(AppDomain.CurrentDomain.BaseDirectory + @"\weather\sd.png", 255, 255, 255, color.R, color.G, color.B)); aqi.Source = loadBitmap(ReplaceColor(AppDomain.CurrentDomain.BaseDirectory + @"\weather\aqi.png", 255, 255, 255, color.R, color.G, color.B)); fl.Source = loadBitmap(ReplaceColor(AppDomain.CurrentDomain.BaseDirectory + @"\weather\fl.png", 255, 255, 255, color.R, color.G, color.B)); pm.Source = loadBitmap(ReplaceColor(AppDomain.CurrentDomain.BaseDirectory + @"\weather\pm.png", 255, 255, 255, color.R, color.G, color.B)); } } catch (Exception ex) { Log.MyLog.WriteLogFile("LoadWeather:" + ex.ToString(), "WeatherControl"); } style1.Visibility = Visibility.Visible; style2.Visibility = Visibility.Hidden; } else { try { if (fontColor != "#FFFFFF") { sd2.Source = loadBitmap(ReplaceColor(AppDomain.CurrentDomain.BaseDirectory + @"\weather\sd.png", 255, 255, 255, color.R, color.G, color.B)); aqi2.Source = loadBitmap(ReplaceColor(AppDomain.CurrentDomain.BaseDirectory + @"\weather\aqi.png", 255, 255, 255, color.R, color.G, color.B)); fl2.Source = loadBitmap(ReplaceColor(AppDomain.CurrentDomain.BaseDirectory + @"\weather\fl.png", 255, 255, 255, color.R, color.G, color.B)); pm2.Source = loadBitmap(ReplaceColor(AppDomain.CurrentDomain.BaseDirectory + @"\weather\pm.png", 255, 255, 255, color.R, color.G, color.B)); } } catch (Exception ex) { Log.MyLog.WriteLogFile("LoadWeather:" + ex.ToString(), "WeatherControl"); } style2.Visibility = Visibility.Visible; style1.Visibility = Visibility.Hidden; } WeatherTimer.Start(); } private void WeatherTimer_Tick(object sender, EventArgs e) { try { BindWeather(); } catch (Exception ex) { Log.MyLog.WriteLogFile("WeatherTimer_Tick:" + ex.ToString(), "WeatherControl"); } } private void BindWeather() { try { string imgUrl = ""; WeatherViewModel weather = HttpComm.Http.GetEnvironmental(city); if (weather == null || string.IsNullOrEmpty(weather.weather)) { this.Width = 0; this.Height = 0; return; } if (weather.weather.Contains("晴")) { imgUrl = "weather_sum.png"; } else if (weather.weather.Contains("云")) { imgUrl = "weather_cloud.png"; } else if (weather.weather.Contains("雨")) { imgUrl = "weather_rian.png"; } else if (weather.weather.Contains("雪")) { imgUrl = "weather_snow.png"; } else if (weather.weather.Contains("阴") || weather.weather.Contains("雾")) { imgUrl = "weather_cloud.png"; } else { imgUrl = "weather_sum.png"; } if (!string.IsNullOrEmpty(weather.temperatureNow)) { txtTemp1.Text = Math.Floor(Convert.ToDouble(weather.temperatureNow)).ToString(); txtTemp2.Text = Math.Floor(Convert.ToDouble(weather.temperatureNow)).ToString(); } if (!string.IsNullOrEmpty(weather.pm)) { txtPM1.Text = Math.Floor(Convert.ToDouble(weather.pm)).ToString(); txtPM2.Text = Math.Floor(Convert.ToDouble(weather.pm)).ToString(); } if (!string.IsNullOrEmpty(weather.humidity)) { txtsd1.Text = weather.humidity.Replace("%", ""); txtsd2.Text = weather.humidity.Replace("%", ""); } if (!string.IsNullOrEmpty(weather.windPower)) { txtFL1.Text = weather.windPower.Substring(0, weather.windPower.IndexOf("级")); txtFL2.Text = weather.windPower.Substring(0, weather.windPower.IndexOf("级")); } if (!string.IsNullOrEmpty(weather.aqi)) { txtAQI1.Text = weather.aqi; txtAQI2.Text = weather.aqi; } if (fontColor != "#FFFFFF") { try { Color color = (Color)ColorConverter.ConvertFromString(fontColor); this.imgWeather1.Source = loadBitmap(ReplaceColor(AppDomain.CurrentDomain.BaseDirectory + @"\weather\" + imgUrl, 255, 255, 255, color.R, color.G, color.B)); this.imgWeather2.Source = loadBitmap(ReplaceColor(AppDomain.CurrentDomain.BaseDirectory + @"\weather\" + imgUrl, 255, 255, 255, color.R, color.G, color.B)); } catch (Exception ex) { Log.MyLog.WriteLogFile("BindWeather:" + ex.ToString(), "WeatherControl"); } } } catch (Exception e) { this.Width = 0; this.Height = 0; } } public void Dispose() { if (WeatherTimer != null) WeatherTimer.Stop(); } #region 图片指定颜色替换成另一种颜色 /// /// 指定颜色替换成另一种颜色 /// /// 原图 /// 图宽 /// 图高 /// 要被替换颜色的RGB的R /// 要被替换颜色的RGB的G /// 要被替换颜色的RGB的B /// 替换色的RGB的R /// 替换色的RGB的G /// 替换色的RGB的B /// 处理后的结果图像 public System.Drawing.Bitmap ReplaceColor(string filePath,int R, int G, int B, int r, int g, int b) { System.Drawing.Bitmap img = new System.Drawing.Bitmap(filePath); System.Drawing.Bitmap bt = new System.Drawing.Bitmap(ConvertTo32bpp(img)); System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, img.Width, img.Height); System.Drawing.Imaging.BitmapData bmpdata = bt.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bt.PixelFormat); IntPtr ptr = bmpdata.Scan0; int bytes = Math.Abs(bmpdata.Stride) * bt.Height; byte[] rgbValues = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); int len = rgbValues.Length; byte a = (byte)int.Parse(100.ToString()); byte R1 = (byte)R; byte G1 = (byte)G; byte B1 = (byte)B; byte r1 = (byte)r; byte g1 = (byte)g; byte b1 = (byte)b; for (int i = 0; i < len; i += 4) { //Format32bppRgb是用4个字节表示一个像素,第一个字节表示RGB的B值,第一个表示为G值,第三个表示为R值,第四个表示为Alpha值 if (Math.Abs(rgbValues[i] - B1) < a && Math.Abs(rgbValues[i + 1] - G1) < a && Math.Abs(rgbValues[i + 2] - R1) < a) { rgbValues[i] = b1; rgbValues[i + 1] = g1; rgbValues[i + 2] = r1; } } System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); bt.UnlockBits(bmpdata); return Conver_1(bt,bt.Width,bt.Height); } #endregion /// /// Image图像转化为32位位图 /// /// /// public System.Drawing.Bitmap ConvertTo32bpp(System.Drawing.Image img) { var bmp = new System.Drawing.Bitmap(img.Width, img.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); using (var gr = System.Drawing.Graphics.FromImage(bmp)) gr.DrawImage(img, new System.Drawing.Rectangle(0, 0, img.Width, img.Height)); return bmp; } #region 图片背景透明化 Conver_1(Bitmap img, int w, int h) public System.Drawing.Bitmap Conver_1(System.Drawing.Bitmap img, int w, int h) { System.Drawing.Bitmap bt = new System.Drawing.Bitmap(ConvertTo32bpp(img)); System.Drawing.Rectangle rect = new System.Drawing.Rectangle(0, 0, bt.Width, bt.Height); System.Drawing.Imaging.BitmapData bmpdata = bt.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bt.PixelFormat); IntPtr ptr = bmpdata.Scan0; int bytes = Math.Abs(bmpdata.Stride) * bt.Height; byte[] rgbValues = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); int len = rgbValues.Length; byte a = (byte)40;//颜色的误差范围 for (int i = 0; i < len; i += 4) { //Format32bppRgb是用4个字节表示一个像素,第一个字节表示RGB的B值,第一个表示为G值,第三个表示为R值,第四个表示为Alpha值 if (Math.Abs(rgbValues[i] - rgbValues[0]) < a && Math.Abs(rgbValues[i + 1] - rgbValues[1]) < a && Math.Abs(rgbValues[i + 2] - rgbValues[2]) < a) { rgbValues[i + 3] = (byte)0; } } System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); bt.UnlockBits(bmpdata); return bt; } #endregion } }