欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > C# >内容正文

C#

C# PictureBox 图片控件

发布时间:2023/12/20 C# 55 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C# PictureBox 图片控件 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

图片控件中常用的属性如下表所示:

       

         图片控件中图片的设置除了可以直接使用 ImageLocation 属性指定图片路径以外,

还可以通过 Image.FromFile 方法来设置。实现的代码如下:

图片控件的名称 .Image = Image. FromFile( 图像的路径 );

【实例】

               实现图片交换。

Form1.cs

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace PictureBoxForm {public partial class Form1 : Form{public Form1(){InitializeComponent();}//窗体加载事件,设置图片空间中显示的图片private void Form1_Load(object sender, EventArgs e){//指定图片路径:图片控件的名称 .Image = Image. FromFile( 图像的路径 );pictureBox1.Image = Image.FromFile(@"C:\Users\86186\Desktop\01.jpg");//图片在图片控件中被拉伸或收缩,适合图片的大小pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;pictureBox2.Image = Image.FromFile(@"C:\Users\86186\Desktop\02.jpg");pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; }//“交换”按钮的单击事件,用于交换图片private void button1_Click(object sender, EventArgs e){//定义中间变量存放图片地址,用于交换图片地址PictureBox pictureBox = new PictureBox();pictureBox.Image = pictureBox1.Image;pictureBox1.Image = pictureBox2.Image;pictureBox2.Image = pictureBox.Image;}} }

Program.cs

using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms;namespace PictureBoxForm {static class Program{/// <summary>/// 应用程序的主入口点。/// </summary>[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Form1());}} }

             

 

                  

 

 

 

 

 

 

总结

以上是生活随笔为你收集整理的C# PictureBox 图片控件的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。