Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12509174
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: 嵌入式

2012-09-14 17:39:28

1、建议一个windows窗体控件库工程


image_thumb8

image_thumb10


2、在从工具箱里面拖动1个PictureBox、1个Button、6个Lable控件到用户界面上

布局如下:

并设置相关控件的属性。


image

 

3、对点击“打开”按钮添加处理事件


在“打开”按钮的Click事件写入代码,打开一个打开文件对话框,选择一个图形文件,打开并将它显示在picBox上。

事件代码如下:


点击(此处)折叠或打开

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.     OpenFileDialog OFDialog = new OpenFileDialog();
  4.     OFDialog.Filter = "JPG(*.JPG;*.JPEG);gif文件(*.GIF)|*.jpg;*.jpeg;*.gif";
  5.     OFDialog.FilterIndex = 1;
  6.     OFDialog.RestoreDirectory = true;
  7.     OFDialog.FileName = "";

  8.     if (OFDialog.ShowDialog() == DialogResult.OK)
  9.     {
  10.         string picAbsolutePath = OFDialog.FileName.ToString();
  11.         FileInfo FInfoDemo = new FileInfo(picAbsolutePath);

  12.         long lPicLong = FInfoDemo.Length / 1024;
  13.         string sPicName = FInfoDemo.Name;
  14.         string sPicDirectory = FInfoDemo.Directory.ToString();
  15.         string sPicDirectoryPath = FInfoDemo.DirectoryName;
  16.         Bitmap bmPic = new Bitmap(picAbsolutePath);

  17.         if (lPicLong > 400)
  18.         {
  19.             MessageBox.Show("此文件大小為" + lPicLong + "K;已超過最大限制的K范圍!");
  20.         }
  21.         else
  22.         {
  23.             Point ptLoction = new Point(bmPic.Size);
  24.             if (ptLoction.X > pictureBox1.Size.Width || ptLoction.Y > pictureBox1.Size.Height)
  25.             {
  26.                 pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
  27.             }
  28.             else
  29.             {
  30.                 pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
  31.             }
  32.         }

  33.         pictureBox1.LoadAsync(picAbsolutePath);
  34.         label2.Text = sPicName;
  35.         label3.Text = lPicLong.ToString() + " KB";
  36.         label5.Text = bmPic.Size.Width.ToString() + "×" + bmPic.Size.Height.ToString();

  37.     }
  38. }

 

读取效果

image


3、生成自定义控制动态连接库


image



4、在其他窗体应用程序中调用示例


image

image

image

image

image

image

 

 

阅读(2864) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~