C#中图片操作

来源:互联网 发布:中差评师拦截功能软件 编辑:程序博客网 时间:2024/04/29 08:27
   //选择图片并显示
        private void btnCaseAddPic_Click(object sender, System.EventArgs e)
        {
            openFileDialog1.Filter      = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
            openFileDialog1.FilterIndex = 0;
            openFileDialog1.Title       = "选择照片";
            affixInfo                   = new AffixInfo();
            if (openFileDialog1.ShowDialog() ==DialogResult.OK)
            {
                //取文件名
                affixInfo.FileName = openFileDialog1.FileName;
                while(affixInfo.FileName.IndexOf("//") != -1)
                {
                    affixInfo.FileName = affixInfo.FileName.Substring(affixInfo.FileName.IndexOf("//")+1);
                }
                //取文件扩展名
                affixInfo.FileExt = affixInfo.FileName.Substring(affixInfo.FileName.LastIndexOf(".")+1);
                if(affixInfo.FileName.Length != 0)
                {
                    //读取图片
                    FileStream fs          = (FileStream)openFileDialog1.OpenFile();
                    byte[] content         = new byte[fs.Length];
                    fs.Read(content, 0,content.Length);
                    affixInfo.FileImage = content;
                    affixInfo.FileSize  = Convert.ToInt32(fs.Length);
                    //显示图片
                    //Stream stream = new MemoryStream(content, true);
                    //stream.Write(content, 0, content.Length);
                    Image bmp     = new Bitmap(fs);
                    //stream.Close();
                    pbxCasePic.Image = bmp;
                    //设置查看图片按钮可用
                    this.btnCaseLookPic.Enabled = true;
                }//endif
            }
        }
        //启动系统默认应用程序查看图片
        private void btnCaseLookPic_Click(object sender, System.EventArgs e)
        {
            if (affixInfo.FileName != null)
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UseShellExecute = true;
                process.StartInfo.FileName = affixInfo.FileName;
                process.Start();
            }
            else
            {
                MessageBox.Show("请正确选择图片文件!","提示");
            }
        } 
 
原创粉丝点击