zoukankan      html  css  js  c++  java
  • C#---- Winform控件PictureBox详解

       PictureBox表示用于显示图像的 Windows 图片框控件https://msdn.microsoft.com/zh-cn/library/system.windows.forms.picturebox.aspx

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace TestPictureBox
    {
        public partial class frmTestPictureBox : Form
        {
            public frmTestPictureBox()
            {
                InitializeComponent();
                this.tbxFilePath.Enabled = false;
                this.btnPreview.Enabled = false;
            }
    
            /// <summary>
            /// 选择文件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnSelectFile_Click(object sender, EventArgs e)
            {
                try
                {
                    OpenFileDialog openFileDialog = new OpenFileDialog();
                    //设置打开对话框的初始目录,默认目录为exe运行文件所在的路径
                    openFileDialog.InitialDirectory = Application.StartupPath;
                    //设置打开对话框的标题
                    openFileDialog.Title = "请选择图片";
                    //设置对话框是否记忆之前打开的目录
                    openFileDialog.RestoreDirectory = true;
                    if (openFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        //获取用户选择的文件完整路径
                        string filePath = openFileDialog.FileName;
                        //获取对话框中所选文件的文件名和扩展名,文件名不包括路径
                        string fileName = openFileDialog.SafeFileName;
                        if (isPicture(fileName))
                        {
                            //获取用户选择的文件,并判断文件大小不能超过2M,fileInfo.Length是以字节为单位的 
                            FileInfo fileInfo = new FileInfo(filePath);
                            if (fileInfo.Length > 2097152)
                            {
                                MessageBox.Show("图片不能大于2M!");
                            }
                            else
                            {
                                this.tbxFilePath.Text = filePath;
                                this.btnPreview.Enabled = true;
                            }
                        }
                        else
                        {
                            MessageBox.Show("图片不能为空!");
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }
    
            /// <summary>
            /// 判断是否是图片
            /// </summary>
            /// <param name="fileName"></param>
            /// <returns></returns>
            public bool isPicture(string fileName)
            {
                bool isFlag = true;
                try
                {
                    if (fileName.EndsWith(".gif") || fileName.EndsWith(".jpge") || fileName.EndsWith(".jpg") || fileName.EndsWith(".png"))
                    {
                        return isFlag;
                    }
                    else
                    {
                        isFlag = false;
                        return isFlag;
                    }
                }
                catch (Exception ex)
                {
                }
                return isFlag;
            }
    
            /// <summary>
            /// 预览
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnPreview_Click(object sender, EventArgs e)
            {
                try
                {
                    string filePath = this.tbxFilePath.Text;
                    //根据路径转换为Byte[]数组
                    byte[] imgBytes = GetImageByPath(filePath);
                    MemoryStream ms = new MemoryStream(imgBytes, 0, imgBytes.Length);
                    //设置图片
                    Image returnImage = Image.FromStream(ms);
                    //PictureBox 中的图像被拉伸或收缩,以适合PictureBox的大小
                    this.pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
                    this.pictureBox.Image = returnImage;
                }
                catch (Exception ex)
                {
                }
            }
    
            /// <summary>
            /// 根据图片路径获取字节
            /// </summary>
            /// <param name="filePath"></param>
            /// <returns></returns>
            public byte[] GetImageByPath(string filePath)
            {
                byte[] buffer = null;
                try
                {
                    if (!string.IsNullOrEmpty(filePath))
                    {
                        FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                        buffer = new byte[fs.Length];
                        fs.Read(buffer, 0, (int)fs.Length);
                        fs.Close();
                        return buffer;
                    }
                }
                catch (Exception ex)
                {
                }
                return buffer;
            }
        }
    }
  • 相关阅读:
    CentOS中文件夹基本操作命令
    Apache和Nginx下禁止访问特定的目录或文件
    自适应网页设计(Responsive Web Design)
    使用Google Https搜索
    AMD 3600+ X2 CPU配合昂达A69T主板超频教程
    dedecms上传图片相对路径改成绝对路径方法
    安装ecshop默认安装后的错误解决方案
    动态加载JS脚本的4种方法
    Java虚拟机(二)对象的创建与OOP-Klass模型
    Android系统启动流程(四)Launcher启动过程与系统启动流程
  • 原文地址:https://www.cnblogs.com/fengfuwanliu/p/11551043.html
Copyright © 2011-2022 走看看