1、建议一个windows窗体控件库工程
2、在从工具箱里面拖动1个PictureBox、1个Button、6个Lable控件到用户界面上
布局如下:
并设置相关控件的属性。
3、对点击“打开”按钮添加处理事件
在“打开”按钮的Click事件写入代码,打开一个打开文件对话框,选择一个图形文件,打开并将它显示在picBox上。
事件代码如下:
- private void button1_Click(object sender, EventArgs e)
- {
- OpenFileDialog OFDialog = new OpenFileDialog();
- OFDialog.Filter = "JPG(*.JPG;*.JPEG);gif文件(*.GIF)|*.jpg;*.jpeg;*.gif";
- OFDialog.FilterIndex = 1;
- OFDialog.RestoreDirectory = true;
- OFDialog.FileName = "";
- if (OFDialog.ShowDialog() == DialogResult.OK)
- {
- string picAbsolutePath = OFDialog.FileName.ToString();
- FileInfo FInfoDemo = new FileInfo(picAbsolutePath);
- long lPicLong = FInfoDemo.Length / 1024;
- string sPicName = FInfoDemo.Name;
- string sPicDirectory = FInfoDemo.Directory.ToString();
- string sPicDirectoryPath = FInfoDemo.DirectoryName;
- Bitmap bmPic = new Bitmap(picAbsolutePath);
- if (lPicLong > 400)
- {
- MessageBox.Show("此文件大小為" + lPicLong + "K;已超過最大限制的K范圍!");
- }
- else
- {
- Point ptLoction = new Point(bmPic.Size);
- if (ptLoction.X > pictureBox1.Size.Width || ptLoction.Y > pictureBox1.Size.Height)
- {
- pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
- }
- else
- {
- pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage;
- }
- }
- pictureBox1.LoadAsync(picAbsolutePath);
- label2.Text = sPicName;
- label3.Text = lPicLong.ToString() + " KB";
- label5.Text = bmPic.Size.Width.ToString() + "×" + bmPic.Size.Height.ToString();
- }
- }
读取效果
3、生成自定义控制动态连接库
4、在其他窗体应用程序中调用示例
阅读(2919) | 评论(0) | 转发(0) |