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;
        }
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM