wpf之BitmapImage

 

WPF中的BitmapImage类是UI控件类,是.net对象,使用完毕不需要Dispose或close来对其释放资源;需要在UI线程上对其进行操作,不能再其他线程上对其进行操作,否则会报错。

该类继承于DispatcherObject,其继承链如下所示:

Object->DispatcherObject->DependencyObject->Freezable->Animatable->ImageSource->BitmapSource->BitmapImage

 

可以将其用于Image控件的Source属性,通常该Source属性可以简单的用路径来设置,比如<Image Source="c://123.jpg"  />,但是,当对123.jpg频繁进行修改时,会存在123.jpg文件被进程占用的异常。这时可以采用BitmapImage对象来绑定。比如:

<Image Source="{Binding BitmapImageSource}"/>

在VM中创建BitmapImageSource属性,比如:

private BitmapImage _bitmapImageSouce;
public BitmapImage BitmapImageSource
{
  get
  {
      return _bitmapImageSource;
  }
  set
  {
      if(_bitmapImageSource!=value)
       {
          _bitmapImageSource=value;
         OnPropertyChanged("BitmapImageSouce");
      }
  }
}

 

进而,可以在代码中对BitmapImageSource对象进行赋值。为了避免赋值后的文件被进程占用,可以先将文件读入MemoryStream对象,然后关键文件,再利用MemoryStream对象来构件BitmapImage对象,比如:

//把方法运行效果很好,达到了可以频繁显示经更改数据(但照片路径不变)后的照片的目的       
 private BitmapImage GetBitmapImage(string imagePath)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(imagePath, FileMode.Open);
                byte[] MyData = new byte[fs.Length];
                fs.Read(MyData, 0, (int)fs.Length);

                MemoryStream ms = new MemoryStream(MyData);
                BitmapImage bitmap = new BitmapImage();  //WPF
                bitmap.BeginInit();
                bitmap.CacheOption = BitmapCacheOption.OnLoad;
                bitmap.StreamSource = ms;
                bitmap.EndInit();
                return bitmap;
            }
            catch (Exception ex)
            {
                // Exception treatment code here

                return null;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
        }

有些网上介绍的方法,没有使用MemoryStream来创建BitmapImage,而是直接给BitmapImage赋UriSource值,并克隆初始创建的

BitmapImage对象,经实际使用,若多次修改照片数据但照片路径保持不变的情况下,存在不能更新显示的Image,且偶尔也会发生文件被占用的情况。其代码如下:

    //注意,以下代码达到不想要的效果,若照片数据需要修改,则不会及时反映到显示的照片中,这种情况下是不能使用本方法的
 try
            {

                BitmapImage bitmap = new BitmapImage();  //WPF
                bitmap.BeginInit();
                bitmap.CacheOption = BitmapCacheOption.OnLoad;
                bitmap.UriSource =new Uri(myImagePath);
                bitmap.EndInit();
                return bitmap.Clone(); //返回克隆对象
            }
            catch (Exception ex)
            {
                // Exception treatment code here

                return null;
            }

 

网格搜索是一种常用的模型超参数调整方法,特别是在机器学习中,它用于寻找最适合数据集的决策树算法(如随机森林、梯度提升机等)的最优参数组合。对于决策树而言,可能的参数包括但不限于: 1. **max_depth**(最大深度):限制树的最大层数,防止过拟合。 2. **min_samples_split**(最小样本分裂):划分节点所需的最小样本数。 3. **min_samples_leaf**(最小叶子节点样本):每个叶节点最少包含的样本数。 4. **criterion**(划分标准):如Gini impurity或信息增益。 5. **max_features**(最大特征数):每次选择多少特征进行划分。 网格搜索会在预设的参数范围内创建一个“网格”,然后对每组参数组合训练一个决策树模型,并通过交叉验证评估其性能(如准确率、AUC等)。最终,选取性能最好的一组参数作为最优解。 以下是使用scikit-learn库进行网格搜索的基本步骤: ```python from sklearn.model_selection import GridSearchCV from sklearn.tree import DecisionTreeClassifier # 假设我们有一个决策树分类器 dtree = DecisionTreeClassifier() # 定义参数网格 param_grid = { 'max_depth': [3, None], 'min_samples_split': [2, 5, 10], 'min_samples_leaf': [1, 2, 4], 'criterion': ['gini', 'entropy'], } # 创建GridSearchCV实例 grid_search = GridSearchCV(dtree, param_grid, cv=5) # 使用交叉验证,cv=5表示5折 # 训练模型 grid_search.fit(X_train, y_train) # 获取最佳参数 best_params = grid_search.best_params_ best_score = grid_search.best_score_ print(f"Best parameters found: {best_params}\nBest score: {best_score}") ``` 执行完毕后,你可以用这些最佳参数去构建和优化你的决策树模型。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值