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);
)
{
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 = , 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, , (int)fs.Length);
fs.Close();
return buffer;
}
}
catch (Exception ex)
{
}
return buffer;
}
}
}
C# winform控件之PictureBox详解的更多相关文章
- 《手把手教你》系列技巧篇(三十八)-java+ selenium自动化测试-日历时间控件-下篇(详解教程)
1.简介 理想很丰满现实很骨感,在应用selenium实现web自动化时,经常会遇到处理日期控件点击问题,手工很简单,可以一个个点击日期控件选择需要的日期,但自动化执行过程中,完全复制手工这样的操作就 ...
- 《手把手教你》系列技巧篇(三十七)-java+ selenium自动化测试-日历时间控件-上篇(详解教程)
1.简介 我们在实际工作中,有可能遇到有些web产品,网页上有一些时间选择,然后支持按照不同时间段范围去筛选数据.网页上日历控件一般,是一个文本输入框,鼠标点击,就会弹出日历界面,可以选择具体日期.这 ...
- iOS:选择器控件UIPickerView的详解和演示
选择器控件UIPickerView: 功能:它能够创建一个类似于密码锁式的单列或多列的选择菜单,用户可以通过它设置的代理来选择需要菜单中的任意的数据.例如创建日历.字体表(类型.大小.颜色).图库等. ...
- delphi控件属性大全-详解-简介
http://blog.csdn.net/u011096030/article/details/18716713 button 组件: CAPTION 属性 :用于在按钮上显示文本内容 Cancel ...
- 【VB技巧】VB ListView 控件功能使用详解
来源:http://lcx.cc/?i=494 ListView控件 在工具箱上击鼠标右键,选择快捷菜单的Components(部件)项,在控件列表中选择Microsoft Windows Commo ...
- AnyCAD三维控件场景节点详解
SceneNode是AnyCAD三维图形平台的AnyViz显示引擎的核心对象之一,只有放在场景管理器(SceneManager)里的节点才能被显示引擎所显示. 1. 节点分类 SceneNode是 ...
- javascript遍历控件(实例详解)
js遍历页面控件, 代码如下 复制代码 var inputArr = document.forms[0]; for( var i = 0; i < inputArr.length; i++ ...
- Webbrowser控件execcommand参数详解
2D-Position 允许通过拖曳移动绝对定位的对象.AbsolutePosition 设定元素的 position 属性为“absolute”(绝对).BackColor 设置或获取当前选中区的背 ...
- 转 VB ListView控件各种操作详解
Private Sub Form_Load() ListView1.ListItems.Clear '清空列表 ListView1.ColumnHeaders.Clear '清空列表头 ListVie ...
随机推荐
- Hadoop2.2.0-64位编译
本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 实验环境:Ubunt ...
- iphone手机分辨率--持久维护
6.5英寸 —— 1242 x 2688 px —— Xs Max 6.1英寸 —— 828 x 1792 px —— XR 5.8英寸 —— 1125 x 2436 px —— X/Xs 5.5英寸 ...
- EAI G4-lidar ROS配置
(1)使用命令创建 ydlidar_ws 工作空间,并将 G4 资料包内的 ROS 驱动包 ydlidar 下载到ydlidar_ws/src 目录下,切换到 ydlidar_ws 工作空间下并重新进 ...
- 求解复数组 中模较大的N个数
//求解复数组 中模较大的N个数 void fianN_Complex(Complex outVec[], int& len, std::vector<int>& inde ...
- cmake学习之-project
一.系统版本 cmake version: 3.5.2 系统版本: Ubuntun 16.04 cmake docment: 3.14.4 最后更新: 2019-05-31 二.指令说明 projec ...
- Android NDK开发常见错误
错误一: make: *** No rule to make target `/cygdrive/d/1-workspace/showmap-android-opengles/jni/showmap_ ...
- html5 cocos2d js Access-Control-Allow-Origin
1.No 'Access-Control-Allow-Origin' header is present on the requested 近期在接html5的渠道,遇到了跨域的问题,使用 js 的 ...
- andorid中发送短信页面以及邮件发送
跳转到发送短信页面 Uri smsToUri = Uri.parse("smsto://10086"); Intent mIntent = new Intent( android. ...
- C# C/S程序使用HTML文件作为打印模板
C# C/S程序使用HTML文件作为打印模板 在网上找了一堆的资料,整理到郁闷呀,慢慢试慢慢改.哎,最终成功了,哈,菜鸟伤不起呀 public partial class Print : Form ...
- 【BZOJ2406】矩阵 二分+有上下界的可行流
[BZOJ2406]矩阵 Description Input 第一行两个数n.m,表示矩阵的大小. 接下来n行,每行m列,描述矩阵A. 最后一行两个数L,R. Output 第一行,输出最小的答案: ...