1.读取并显示图像

1.新建窗体应用程序; 2.添加并设置控件属性: pictureBox1控件; openFileDialog1控件。 Button控件 3.双击Button控件编写代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;
namespace FormReadImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//定义一组图片变量
byte[] pictureBytes;
private void button1_Click(object sender, EventArgs e)
{
//定义打开图片的类型,图片的格式;
openFileDialog1.Filter = "*jpg|*.JPG|.*GIF|.*gif|.*BMP|.*bmp|.*png|.PNG";
//获取打开文件的返回值
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//文件的路径
string fullPath = openFileDialog1.FileName;

FileStream fs = new FileStream(fullPath, FileMode.Open);
pictureBytes = new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
pictureBytes = br.ReadBytes(Convert.ToInt32(fs.Length));
MemoryStream ms = new MemoryStream(pictureBytes);
Bitmap bmpt = new Bitmap(ms);
pictureBox1.Image = bmpt;
}
else
{
MessageBox.Show("打开图像失败");
}

}
}
}
4.运行结果

2.添加新的Button,实现保存图片:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;
namespace FormReadImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private string pathname = string.Empty;
private void button1_Click(object sender, EventArgs e)
{

OpenFileDialog file = new OpenFileDialog();
file.InitialDirectory = ".";
file.Filter = "所有文件(*.*)|*.*";
file.ShowDialog();
if (file.FileName != string.Empty)
{
try
{
pathname = file.FileName;   //获得文件的绝对路径
this.pictureBox1.Load(pathname);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

}

private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.ShowDialog();
if (save.FileName != string.Empty)
{
pictureBox1.Image.Save(save.FileName);
}
}
}
}

3.放大缩小图像

1.放置panel,和pictureBox两个控件(sizemode=zoom),将PictureXBox放置在panel上; 2.注册鼠标滚轮滑动事件,并实现pictureBox1的放大和缩小
        void pictureBox1_MouseWheel(object sender, MouseEventArgs e)

{
this.pictureBox1.Width += e.Delta;
this.pictureBox1.Height += e.Delta;
}

private void Form1_Load(object sender, EventArgs e)
{
this.StartPosition = FormStartPosition.Manual;
pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
}

4.马赛克图片

1.功能:读入图片,将图片做马赛克,并显示运行事件; 2.控件:3个Label,两个PictureBox,两个BUtton 3.原理:马赛克的基本思想就是把一个像素点周围的点的像素取个平均,然后把这些像素点的颜色设为这个平均值。 周围的像素点取的越多,马克赛的效果也就越明显
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Diagnostics;


namespace ZoomImage
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//定义全局变量
Bitmap bitmap;
Bitmap newbitmap;
Stopwatch sw = new Stopwatch();
//主窗口事件
private void Form1_Load(object sender, EventArgs e)
{

}
//点击按钮1,实现读取图片并显示在pictureBox1上;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;//获取图片的路径
bitmap = (Bitmap)Image.FromFile(path);//加载图片
this.pictureBox1.Image = bitmap.Clone() as Image;//图片在pictureBox1中显示
}

}
//马赛克的基本思想就是把一个像素点周围的点的像素取个平均,然后把这些像素点的颜色设为这个平均值。
// 周围的像素点取的越多,马克赛的效果也就越明显
private void button3_Click(object sender, EventArgs e)
{
if (bitmap != null)
{
newbitmap = bitmap.Clone() as Bitmap;
sw.Reset();
sw.Restart();

int RIDIO = 10;//马赛克的尺度,默认为周围两个像素
for (int h = 0; h < newbitmap.Height; h += RIDIO)
{
for (int w = 0; w < newbitmap.Width; w += RIDIO)
{
int avgRed = 0, avgGreen = 0, avgBlue = 0;
int count = 0;
//取周围的像素
for (int x = w; (x < w + RIDIO && x < newbitmap.Width); x++)
{
for (int y = h; (y < h + RIDIO && y < newbitmap.Height); y++)
{
Color pixel = newbitmap.GetPixel(x, y);
avgRed += pixel.R;
avgGreen += pixel.G;
avgBlue += pixel.B;
count++;
}
}

//取平均值
avgRed = avgRed / count;
avgBlue = avgBlue / count;
avgGreen = avgGreen / count;

//设置颜色
for (int x = w; (x < w + RIDIO && x < newbitmap.Width); x++)
{
for (int y = h; (y < h + RIDIO && y < newbitmap.Height); y++)
{
Color newColor = Color.FromArgb(avgRed, avgGreen, avgBlue);
newbitmap.SetPixel(x, y, newColor);
}
}
}
}
sw.Stop();
this.LabelTimer.Text = sw.ElapsedMilliseconds.ToString();
pictureBox2.Image = newbitmap.Clone() as Image;
}

}

private void label1_Click(object sender, EventArgs e)
{

}
}
}