C# winform控件之PictureBox详解

Posted xianya

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
        }
    }
}

 

以上是关于C# winform控件之PictureBox详解的主要内容,如果未能解决你的问题,请参考以下文章

c# winform 循环控件循环赋值问题?

C# WinForm 透明控件 PictureBox透明

C# WinForm应用程序,PictureBox控件透明效果的相关问题。

C# winform picturebox控件显示图片问题 怎么使picturebox在登录学号10001时显示本地文件夹里10001.jpg

winform C# 将线定义成控件放在pictruebox上

Winform 设置panel容器的背景为pictureBox