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;
        }
    }
}
原文地址:https://www.cnblogs.com/fengfuwanliu/p/11551043.html