zoukankan      html  css  js  c++  java
  • C# OpenFileDialog和PictrueBox

    
    string resultFile = "";
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.InitialDirectory = "D:\Patch";
    openFileDialog1.Filter = "All files (*.*)|*.*|txt files (*.txt)|*.txt";
    openFileDialog1.FilterIndex = 2;
    openFileDialog1.RestoreDirectory = true;
      if (openFileDialog1.ShowDialog() == DialogResult.OK)
         resultFile = openFileDialog1.FileName;

    resultFile 就能得到你选中文件的路径

    OpenFileDialog控件有下面基本属性

     InitialDirectory 对话框的初始文件夹
    Filter 要在对话框中显示的文件筛选器,比如,"文本文件(*.txt)|*.txt|全部文件(*.*)||*.*"
    FilterIndex 在对话框中选择的文件筛选器的索引,假设选第一项就设为1
    RestoreDirectory 控制对话框在关闭之前是否恢复当前文件夹
    FileName 第一个在对话框中显示的文件或最后一个选取的文件
    Title 将显示在对话框标题栏中的字符
    AddExtension 是否自己主动加入默认扩展名
    CheckPathExists
    在对话框返回之前。检查指定路径是否存在
    DefaultExt 默认扩展名
    DereferenceLinks 在从对话框返回前是否取消引用快捷方式
    ShowHelp
    启用"帮助"button
    ValiDateNames 控制对话框检查文件名称中是否不含有无效的字符或序列


     如何设置OpenFileDialog组件的Filter。使实现一次过滤出多种扩展名的文件??

    dlg.Filter   =   "Image   Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All   files   (*.*)|*.* "


    第一个參数是picturebox的宽度,第二个是picturebox的高度。第三个是你的图片。

    这种方法能够把图片调整到合适的大小。你就不要设置SizeMode的属性了,通过这种方法得到合适的图片后,设置picturebox的image属性等于这个图片。不要设置背景图。我没有測试。

    你自己去測试下吧,假设还是有问题,那就是图片太小了。你要又一次做张图

     public Image GetNewImage(int newImgWidth, int newImgHeight, Image srcImage)
            {
                Image newImg = srcImage.GetThumbnailImage(newImgWidth, newImgHeight, null, new IntPtr());
                Graphics gr = Graphics.FromImage(newImg);
                gr.DrawImage(newImg, 0, 0, newImg.Width, newImg.Height);
                gr.Dispose();
                return newImg;
           }


    PictrueBox的SizeMode属性:

           // 摘要:
            //     图像被置于 System.Windows.Forms.PictureBox 的左上角。假设图像比包括它的   System.Windows.Forms.PictureBox
            //     大。则该图像将被剪裁掉。


            Normal = 0,
            //
            // 摘要:
            //     System.Windows.Forms.PictureBox 中的图像被拉伸或收缩。以适合 System.Windows.Forms.PictureBox
            //     的大小。
            StretchImage = 1,
            //
            // 摘要:
            //     调整 System.Windows.Forms.PictureBox 大小。使其等于所包括的图像大小。
            AutoSize = 2,
            //
            // 摘要:
            //     假设 System.Windows.Forms.PictureBox 比图像大,则图像将居中显示。

    假设图像比 System.Windows.Forms.PictureBox
            //     大。则图片将居于 System.Windows.Forms.PictureBox 中心,而外边缘将被剪裁掉。


            CenterImage = 3,
            //
            // 摘要:
            //     图像大小按其原有的大小比例被添加或减小。
            Zoom = 4,

  • 相关阅读:
    redis的数据类型与应用场景(二)
    redis的安装与配置(一)
    Spring Security教程 ---- 验证码功能的实现
    Java类文件结构
    实体字符
    前端安全之XSS攻击
    $_SERVER[]数组解析
    php主要用于哪几方面
    集群与分布式概念
    python操作mongodb实例
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5369835.html
Copyright © 2011-2022 走看看