在c#中可以使用PictureBox控件来呈现图像,本文主要介绍了C# PictureBox实现图片交换,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
在 Windows 窗体应用程序中显示图片时要使用图片控件 ( 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图片控件实现图片交换 的文章就介绍到这了,更多相关C# PictureBox 图片交换 内容请搜索得得之家以前的文章希望大家以后多多支持得得之家!
织梦狗教程
本文标题为:C# PictureBox图片控件实现图片交换


基础教程推荐
猜你喜欢
- C#中 Json 序列化去掉null值的方法 2022-11-18
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- 实例详解C#实现http不同方法的请求 2022-12-26
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- C# 解析XML和反序列化的示例 2023-04-14
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C#中的Linq to JSON操作详解 2023-06-08
- Unity shader实现高斯模糊效果 2023-01-16
- Unity 如何获取鼠标停留位置下的物体 2023-04-10