C\#---Winform 컨트롤 PictureBox 상세 설명

원본 링크:http://www.cnblogs.com/fengfuwanliu/p/11551043.html
   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; } /// /// /// /// /// 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) { } } /// /// /// /// /// 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; } /// /// /// /// /// 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) { } } /// /// /// /// /// 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

좋은 웹페이지 즐겨찾기