1、创建Form窗体应用程序
打开VS,创建新项目-语言选择C#-Window窗体应用(.NET Framework)
如果找不到,检查一下有没有安装.NET 桌面开发模块,如果没有,需要下载,记得勾选相关开发工具
接上一步,选择windows窗体应用程序之后,点击下一步,修改相关路径和名字之后,点击创建。就可以得到一个如下的form设计界面。
2、使用到的程序包和控件
2.1、程序包
使用到Nuget程序包中的System.Drawing.Common,下载方式如下:
2.2、控件
通过视图-工具箱,把工具箱调出来,在工具箱选择需要的控件。
1)、此功能会运用openFileDialog、saveFileDialog、Button、Picturebox、label控件
2)、在Form设计界面布置界面,加入图片文件的Picturebox1控件,处理图片后显示图片的Picturebox2控件。
3)、一个openFileDialog控件,两个saveFileDialog控件
4)、接下来布置按钮,Button1:“打开”,Button2:“保存”,Button3:“添加暗角”,Button4:“降低亮度”,Button5:“去色”,Button6:“浮雕”,Button7:“马赛克”,Button8:“扩散”,Button9:“清除图片”,Button10:“保存2”,Button11:“油画效果”,Button12:“柔化”,Button13:“截屏”,Button14:“旋转90”。
5)、label控件。改名为:运行时间
提示:右键控件属性,即可改名
界面如下:
3、代码实现
3.1、Button1打开按钮
//打开按钮private void button1_Click(object sender, EventArgs e){if (openFileDialog1.ShowDialog() == DialogResult.OK){//获取打开文件的路径string path = openFileDialog1.FileName;bitmap = (Bitmap) Bitmap.FromFile(path);pictureBox1.Image = bitmap.Clone() as Image;}}
3.2、Button2保存按钮
//保存按钮private void button2_Click(object sender, EventArgs e){bool isSvae = true;if(saveFileDialog1.ShowDialog() == DialogResult.OK){string fileName = saveFileDialog1.FileName.ToString();if(fileName != "" && fileName != null){//获取文件格式string fileExtName = fileName.Substring(fileName.LastIndexOf('.') + 1).ToString();System.Drawing.Imaging.ImageFormat imgformat = null;if(fileExtName != ""){//确定文件格式switch (fileExtName){case "jpg":imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;break;case "bmp":imgformat = System.Drawing.Imaging.ImageFormat.Bmp;break;case "gif":imgformat = System.Drawing.Imaging.ImageFormat.Gif;break;default:MessageBox.Show("只能存储为:jpg,bmp, gif 格式");isSvae = false;break;}}//默认保存为jpg格式if(imgformat == null){imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;}if(isSvae){try{pictureBox2.Image.Save(fileName, imgformat);MessageBox.Show("图片保存成功!");}catch{MessageBox.Show("保存失败! 你还没有截取过图片或者图片已经清空!");}}}}}
3.3、Button3添加暗角按钮
//添加暗角按钮private void button3_Click(object sender, EventArgs e){if (bitmap != null){newbitmap = bitmap.Clone() as Bitmap;sw.Reset();sw.Restart();int width = newbitmap.Width;int height = newbitmap.Height;float cx = width / 2;float cy = height / 2;float maxDist = cx * cx + cy * cy;float currDist = 0, factor;Color pixel;for (int i = 0; i < width; i++){for (int j = 0; j < height; j++){currDist = ((float)i - cx) * ((float)i - cx) + ((float)j - cy) * ((float)j - cy);factor = currDist / maxDist;pixel = newbitmap.GetPixel(i, j);int red = (int)(pixel.R * (1 - factor));int green = (int)(pixel.G * (1 - factor));int blue = (int)(pixel.B * (1 - factor));newbitmap.SetPixel(i, j, Color.FromArgb(red, green, blue));}}sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();//显示处理好的图片pictureBox2.Image = newbitmap.Clone() as Image;}}
3.4、Button4降低亮度按钮
//降低亮度按钮
private void button4_Click(object sender, EventArgs e)
{if(bitmap != null){newbitmap = bitmap.Clone() as Bitmap;sw.Reset();sw.Restart();Color pixel;int red, green, blue;for (int x = 0; x < newbitmap.Width; x++){for (int y = 0; y < newbitmap.Height; y++){pixel = newbitmap.GetPixel(x, y);red = (int)(pixel.R * 0.6);green = (int)(pixel.G * 0.6);blue = (int)(pixel.B * 0.6);newbitmap.SetPixel(x, y, Color.FromArgb(red, green, blue));}}sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}
}
3.5、Button5去色按钮
//去色按钮private void button5_Click(object sender, EventArgs e){if(bitmap != null){newbitmap = bitmap.Clone() as Bitmap;sw.Reset();sw.Restart();Color pixel;int gray;for (int x = 0; x < newbitmap.Width; x++){for(int y = 0; y< newbitmap.Height; y++){pixel = newbitmap.GetPixel(x, y);gray = (int)(0.3 * pixel.R + 0.59 * pixel.G + 0.11 * pixel.B);newbitmap.SetPixel(x, y, Color.FromArgb(gray, gray, gray));}}sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}}
3.6、Button6 浮雕按钮
//浮雕按钮private void button6_Click(object sender, EventArgs e){if (bitmap != null){newbitmap = bitmap.Clone() as Bitmap;sw.Reset();sw.Restart();Color pixel;int red, green, blue;for (int x = 0; x < newbitmap.Width; x++){for (int y = 0;y < newbitmap.Height; y++){pixel = newbitmap.GetPixel(x, y);red = (int)(255 - pixel.R);green = (int)(255 - pixel.G);blue = (int)(255 - pixel.B);newbitmap.SetPixel(x, y, Color.FromArgb(red, green, blue));}}sw.Stop ();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}}
3.7、Button7马赛克按钮
//马赛克按钮private void button7_Click(object sender, EventArgs e){if (bitmap != null){newbitmap = bitmap.Clone() as Bitmap;sw.Reset();sw.Restart();int RIDIO = 50; // 马赛克的尺度,默认为周围的两个像素for (int h = 0; h < newbitmap.Height; h += RIDIO){for( int w = 0; w < newbitmap.Width; w+= RIDIO){int avgRed = 0, avgGreed = 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;avgGreed += pixel.G;avgBlue += pixel.B;count++;}}//取平均值avgRed = avgRed / count;avgBlue = avgBlue / count;avgGreed = avgGreed / 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, avgGreed, avgBlue);newbitmap.SetPixel(x, y, newColor);}} }}sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}}
3.8、Button8扩散按钮
//扩散按钮private void button8_Click(object sender, EventArgs e){if (bitmap != null){newbitmap = bitmap.Clone() as Bitmap;sw.Reset();sw.Restart();Color pixel;int red, green, blue;for (int x = 0; x < newbitmap.Width; x++){for (int y = 0; y < newbitmap.Height; y++){Random ran = new Random();int RankKey = ran.Next(-5, 5);if (x + RankKey >= newbitmap.Width || y + RankKey >= newbitmap.Height || x + RankKey < 0 || y + RankKey < 0){continue;}pixel = newbitmap.GetPixel(x + RankKey, y + RankKey);red = (int)(pixel.R);green = (int)(pixel.G);blue = (int)(pixel.B);newbitmap.SetPixel(x, y, Color.FromArgb(red, green, blue));}}sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}}
3.9、Button9清除图片按钮
//清除图片按钮private void button9_Click(object sender, EventArgs e){pictureBox1.Image = null;pictureBox2.Image = null;}
3.10、保存2按钮
//保存2按钮
private void button10_Click(object sender, EventArgs e)
{if(saveFileDialog1.ShowDialog() == DialogResult.OK){string path = saveFileDialog1.FileName;ImageFormat imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;pictureBox2.Image.Save(path, imgformat);}
}
3.11、Button11油画按钮
//油画按钮private void button11_Click(object sender, EventArgs e){if (bitmap != null){newbitmap = bitmap.Clone() as Bitmap;//取得图片尺寸int width = newbitmap.Width;int height = newbitmap.Height;sw.Reset();sw.Restart();//产生随机数序列Random rnd = new Random();//取不同的值决定油画效果的不同程度int iModel = 2;int i = width - iModel;while (i > 1){int j = height - iModel;while (j > 1){int iPos = rnd.Next(100000) % iModel;//将该点的RGB值设置成附近iModel点之内的任一点Color color = newbitmap.GetPixel(i + iPos, j + iPos);newbitmap.SetPixel(i, j, color);j = j - 1;}i = i - 1;}sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}}
3.12、Button12柔化按钮
//柔化按钮private void button12_Click(object sender, EventArgs e){if (bitmap != null){newbitmap = bitmap.Clone() as Bitmap;//取得图片尺寸int width = newbitmap.Width;int height = newbitmap.Height;sw.Reset();sw.Restart();Color piexl;//高斯模板int[] Gauss = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };for(int x = 1; x < width - 1; x++){for(int y = 1; y < height - 1; y++){int r = 0, g = 0, b = 0;int index = 0;for(int col = -1; col <= 1; col++){for (int row = -1; row <= 1; row++){piexl = newbitmap.GetPixel(x + row, y + col);r += piexl.R * Gauss[index];g += piexl.G * Gauss[index];b += piexl.B * Gauss[index];index++;}}r /= 16;g /= 16;b /= 16;//处理颜色溢出r = r > 255 ? 255 : r;r = r < 0 ? 0 : r;g = g > 255 ? 255 : g;g = g < 0 ? 0 : g;b = b > 255 ? 255 : b;b = b < 0 ? 0 : b;bitmap.SetPixel(x - 1, y -1, Color.FromArgb(r, g, b));}}sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}}
3.13、Button13截屏按钮
struct POINT
{public Int32 x;public Int32 y;
}struct CURSORINFO
{public Int32 cbSize;public Int32 flags;public IntPtr hCursor;public POINT ptScreenPos;
}// 按指定尺寸对图像pic进行非拉伸缩放public static Bitmap shrinkTo(Image pic, Size S, Boolean cutting){//创建图像Bitmap tmp = new Bitmap(S.Width, S.Height);//按指定大小创建图像//绘制Graphics g = Graphics.FromImage(tmp);//从位图创建Graphics对象g.Clear(Color.FromArgb(0, 0, 0)); //清空Boolean mode = (float)pic.Width / S.Width > (float)pic.Height / S.Height; //zoom缩放if (cutting) mode = !mode; //裁剪缩放//计算zoom绘制区域if (mode){S.Height = (int)((float)pic.Height * S.Width / pic.Width);}else{S.Width = (int)((float)pic.Width * S.Height / pic.Height);}Point p = new Point((tmp.Width - S.Width) / 2, (tmp.Height - S.Height) / 2);g.DrawImage(pic, new Rectangle(p, S));return tmp;//返回构建的新图像}//根据文件拓展名,获取对应的存储类型public static ImageFormat getFormat(string filePath){ImageFormat format = ImageFormat.MemoryBmp;String Ext = System.IO.Path.GetExtension(filePath).ToLower();if (Ext.Equals(".png")) format = ImageFormat.Png;else if (Ext.Equals(".jpg") || Ext.Equals(".jpeg")) format = ImageFormat.Jpeg;else if (Ext.Equals(".bmp")) format = ImageFormat.Bmp;else if (Ext.Equals(".gif")) format = ImageFormat.Gif;else if (Ext.Equals(".ico")) format = ImageFormat.Icon;else if (Ext.Equals(".emf")) format = ImageFormat.Emf;else if (Ext.Equals(".exif")) format = ImageFormat.Exif;else if (Ext.Equals(".tiff")) format = ImageFormat.Tiff;else if (Ext.Equals(".wmf")) format = ImageFormat.Wmf;else if (Ext.Equals(".memorybmp")) format = ImageFormat.MemoryBmp;return format;}//保存图像pic到文件fileName中,指定图像保存格式public static void SaveToFile(Image pic, string fileName, bool replace, ImageFormat format){//若图像已存在,则删除if (System.IO.File.Exists(fileName) && replace){System.IO.File.Delete(fileName);}//若不存在则创建if (!System.IO.File.Exists(fileName)){if (format == null){format = getFormat(fileName); //根据拓展名获取图像的对应存储类型}if (format == ImageFormat.MemoryBmp){pic.Save(fileName);MessageBox.Show("图片保存成功!");}else{pic.Save(fileName, format);//按给定格式保存图像MessageBox.Show("图片保存成功!");}}}// 缩放icon为指定的尺寸,并保存到路径PathNamepublic static void saveImage(Image image, Size size, String PathName){Image tmp = shrinkTo(image, size, false);SaveToFile(tmp, PathName, true, null);}// 截取屏幕指定区域为Image,保存到路径savePath下,haveCursor是否包含鼠标private static Image getScreen(int x = 0, int y = 0, int width = -1, int height = -1, String savePath = "", bool haveCursor = true){if (width == -1){width = SystemInformation.VirtualScreen.Width + 1280;//虚拟屏幕界限}if (height == -1){height = SystemInformation.VirtualScreen.Height + 800;}Bitmap tmp = new Bitmap(width, height); //按指定大小创建位图Graphics g = Graphics.FromImage(tmp); //从位图创建Graphics对象g.CopyFromScreen(x, y, 0, 0, new Size(width, height)); //绘制// 绘制鼠标if (haveCursor){try{CURSORINFO pci;pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO));GetCursorInfo(out pci);System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor);cur.Draw(g, new Rectangle(pci.ptScreenPos.x, pci.ptScreenPos.y, cur.Size.Width, cur.Size.Height));}catch (Exception ex){// 若获取鼠标异常则不显示}}if (!savePath.Equals("")){saveImage(tmp, tmp.Size, savePath); // 保存到指定的路径下}return tmp; //返回构建的新图像}private static void GetCursorInfo(out CURSORINFO pci){throw new NotImplementedException();}//截屏按钮private void button13_Click(object sender, EventArgs e){screen = getScreen(); // 截取屏幕 pictureBox1.Image = screen;}
//旋转90度按钮
private void button14_Click(object sender, EventArgs e){//获取需要旋转的图片// Bitmap bmp = (Bitmap)screen;Bitmap bmp = (Bitmap)pictureBox1.Image;sw.Reset();sw.Restart();//向右90度旋转bmp.RotateFlip(RotateFlipType.Rotate270FlipXY);sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = bmp.Clone() as Image;}
补充:旋转使用了Image类的RotateFlip方法
4、运行结果
打开一张图片显示到pictureBox1中,然后点击相应的按钮,便会在pictureBox2中显示处理后的图片。
5、完整代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace WindowsFormsApp1
{public partial class Form1 : Form{//创建一个新的位图private Bitmap bitmap, newbitmap;private Stopwatch sw;//计算运行时间//截屏的图片Image screen;public Form1(){InitializeComponent();sw = new Stopwatch();}struct POINT{public Int32 x;public Int32 y;}struct CURSORINFO{public Int32 cbSize;public Int32 flags;public IntPtr hCursor;public POINT ptScreenPos;}private void label1_Click(object sender, EventArgs e){}//保存按钮private void button2_Click(object sender, EventArgs e){bool isSvae = true;if(saveFileDialog1.ShowDialog() == DialogResult.OK){string fileName = saveFileDialog1.FileName.ToString();if(fileName != "" && fileName != null){string fileExtName = fileName.Substring(fileName.LastIndexOf('.') + 1).ToString();System.Drawing.Imaging.ImageFormat imgformat = null;if(fileExtName != ""){switch (fileExtName){case "jpg":imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;break;case "bmp":imgformat = System.Drawing.Imaging.ImageFormat.Bmp;break;case "gif":imgformat = System.Drawing.Imaging.ImageFormat.Gif;break;default:MessageBox.Show("只能存储为:jpg,bmp, gif 格式");isSvae = false;break;}}//默认保存为jpg格式if(imgformat == null){imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;}if(isSvae){try{pictureBox2.Image.Save(fileName, imgformat);MessageBox.Show("图片保存成功!");}catch{MessageBox.Show("保存失败! 你还没有截取过图片或者图片已经清空!");}}}}}//打开按钮private void button1_Click(object sender, EventArgs e){if (openFileDialog1.ShowDialog() == DialogResult.OK){string path = openFileDialog1.FileName;bitmap = (Bitmap) Bitmap.FromFile(path);pictureBox1.Image = bitmap.Clone() as Image;}}//添加暗角按钮private void button3_Click(object sender, EventArgs e){if (bitmap != null){newbitmap = bitmap.Clone() as Bitmap;sw.Reset();sw.Restart();int width = newbitmap.Width;int height = newbitmap.Height;float cx = width / 2;float cy = height / 2;float maxDist = cx * cx + cy * cy;float currDist = 0, factor;Color pixel;for (int i = 0; i < width; i++){for (int j = 0; j < height; j++){currDist = ((float)i - cx) * ((float)i - cx) + ((float)j - cy) * ((float)j - cy);factor = currDist / maxDist;pixel = newbitmap.GetPixel(i, j);int red = (int)(pixel.R * (1 - factor));int green = (int)(pixel.G * (1 - factor));int blue = (int)(pixel.B * (1 - factor));newbitmap.SetPixel(i, j, Color.FromArgb(red, green, blue));}}sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}}//降低亮度按钮private void button4_Click(object sender, EventArgs e){if(bitmap != null){newbitmap = bitmap.Clone() as Bitmap;sw.Reset();sw.Restart();Color pixel;int red, green, blue;for (int x = 0; x < newbitmap.Width; x++){for (int y = 0; y < newbitmap.Height; y++){pixel = newbitmap.GetPixel(x, y);red = (int)(pixel.R * 0.6);green = (int)(pixel.G * 0.6);blue = (int)(pixel.B * 0.6);newbitmap.SetPixel(x, y, Color.FromArgb(red, green, blue));}}sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}}//马赛克按钮private void button7_Click(object sender, EventArgs e){if (bitmap != null){newbitmap = bitmap.Clone() as Bitmap;sw.Reset();sw.Restart();int RIDIO = 50; // 马赛克的尺度,默认为周围的两个像素for (int h = 0; h < newbitmap.Height; h += RIDIO){for( int w = 0; w < newbitmap.Width; w+= RIDIO){int avgRed = 0, avgGreed = 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;avgGreed += pixel.G;avgBlue += pixel.B;count++;}}//取平均值avgRed = avgRed / count;avgBlue = avgBlue / count;avgGreed = avgGreed / 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, avgGreed, avgBlue);newbitmap.SetPixel(x, y, newColor);}} }}sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}}//去色按钮private void button5_Click(object sender, EventArgs e){if(bitmap != null){newbitmap = bitmap.Clone() as Bitmap;sw.Reset();sw.Restart();Color pixel;int gray;for (int x = 0; x < newbitmap.Width; x++){for(int y = 0; y< newbitmap.Height; y++){pixel = newbitmap.GetPixel(x, y);gray = (int)(0.3 * pixel.R + 0.59 * pixel.G + 0.11 * pixel.B);newbitmap.SetPixel(x, y, Color.FromArgb(gray, gray, gray));}}sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}}//浮雕按钮private void button6_Click(object sender, EventArgs e){if (bitmap != null){newbitmap = bitmap.Clone() as Bitmap;sw.Reset();sw.Restart();Color pixel;int red, green, blue;for (int x = 0; x < newbitmap.Width; x++){for (int y = 0;y < newbitmap.Height; y++){pixel = newbitmap.GetPixel(x, y);red = (int)(255 - pixel.R);green = (int)(255 - pixel.G);blue = (int)(255 - pixel.B);newbitmap.SetPixel(x, y, Color.FromArgb(red, green, blue));}}sw.Stop ();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}}//扩散按钮private void button8_Click(object sender, EventArgs e){if (bitmap != null){newbitmap = bitmap.Clone() as Bitmap;sw.Reset();sw.Restart();Color pixel;int red, green, blue;for (int x = 0; x < newbitmap.Width; x++){for (int y = 0; y < newbitmap.Height; y++){Random ran = new Random();int RankKey = ran.Next(-5, 5);if (x + RankKey >= newbitmap.Width || y + RankKey >= newbitmap.Height || x + RankKey < 0 || y + RankKey < 0){continue;}pixel = newbitmap.GetPixel(x + RankKey, y + RankKey);red = (int)(pixel.R);green = (int)(pixel.G);blue = (int)(pixel.B);newbitmap.SetPixel(x, y, Color.FromArgb(red, green, blue));}}sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}}//清除图片按钮private void button9_Click(object sender, EventArgs e){pictureBox1.Image = null;pictureBox2.Image = null;}//保存2按钮private void button10_Click(object sender, EventArgs e){if(saveFileDialog1.ShowDialog() == DialogResult.OK){string path = saveFileDialog1.FileName;ImageFormat imgformat = System.Drawing.Imaging.ImageFormat.Jpeg;pictureBox2.Image.Save(path, imgformat);}}//油画按钮private void button11_Click(object sender, EventArgs e){if (bitmap != null){newbitmap = bitmap.Clone() as Bitmap;//取得图片尺寸int width = newbitmap.Width;int height = newbitmap.Height;sw.Reset();sw.Restart();//产生随机数序列Random rnd = new Random();//取不同的值决定油画效果的不同程度int iModel = 2;int i = width - iModel;while (i > 1){int j = height - iModel;while (j > 1){int iPos = rnd.Next(100000) % iModel;//将该点的RGB值设置成附近iModel点之内的任一点Color color = newbitmap.GetPixel(i + iPos, j + iPos);newbitmap.SetPixel(i, j, color);j = j - 1;}i = i - 1;}sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}}//柔化按钮private void button12_Click(object sender, EventArgs e){if (bitmap != null){newbitmap = bitmap.Clone() as Bitmap;//取得图片尺寸int width = newbitmap.Width;int height = newbitmap.Height;sw.Reset();sw.Restart();Color piexl;//高斯模板int[] Gauss = { 1, 2, 1, 2, 4, 2, 1, 2, 1 };for(int x = 1; x < width - 1; x++){for(int y = 1; y < height - 1; y++){int r = 0, g = 0, b = 0;int index = 0;for(int col = -1; col <= 1; col++){for (int row = -1; row <= 1; row++){piexl = newbitmap.GetPixel(x + row, y + col);r += piexl.R * Gauss[index];g += piexl.G * Gauss[index];b += piexl.B * Gauss[index];index++;}}r /= 16;g /= 16;b /= 16;//处理颜色溢出r = r > 255 ? 255 : r;r = r < 0 ? 0 : r;g = g > 255 ? 255 : g;g = g < 0 ? 0 : g;b = b > 255 ? 255 : b;b = b < 0 ? 0 : b;bitmap.SetPixel(x - 1, y -1, Color.FromArgb(r, g, b));}}sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = newbitmap.Clone() as Image;}}/// 按指定尺寸对图像pic进行非拉伸缩放public static Bitmap shrinkTo(Image pic, Size S, Boolean cutting){//创建图像Bitmap tmp = new Bitmap(S.Width, S.Height);//按指定大小创建图像//绘制Graphics g = Graphics.FromImage(tmp);//从位图创建Graphics对象g.Clear(Color.FromArgb(0, 0, 0)); //清空Boolean mode = (float)pic.Width / S.Width > (float)pic.Height / S.Height; //zoom缩放if (cutting) mode = !mode; //裁剪缩放//计算zoom绘制区域if (mode){S.Height = (int)((float)pic.Height * S.Width / pic.Width);}else{S.Width = (int)((float)pic.Width * S.Height / pic.Height);}Point p = new Point((tmp.Width - S.Width) / 2, (tmp.Height - S.Height) / 2);g.DrawImage(pic, new Rectangle(p, S));return tmp;//返回构建的新图像}//根据文件拓展名,获取对应的存储类型public static ImageFormat getFormat(string filePath){ImageFormat format = ImageFormat.MemoryBmp;String Ext = System.IO.Path.GetExtension(filePath).ToLower();if (Ext.Equals(".png")) format = ImageFormat.Png;else if (Ext.Equals(".jpg") || Ext.Equals(".jpeg")) format = ImageFormat.Jpeg;else if (Ext.Equals(".bmp")) format = ImageFormat.Bmp;else if (Ext.Equals(".gif")) format = ImageFormat.Gif;else if (Ext.Equals(".ico")) format = ImageFormat.Icon;else if (Ext.Equals(".emf")) format = ImageFormat.Emf;else if (Ext.Equals(".exif")) format = ImageFormat.Exif;else if (Ext.Equals(".tiff")) format = ImageFormat.Tiff;else if (Ext.Equals(".wmf")) format = ImageFormat.Wmf;else if (Ext.Equals(".memorybmp")) format = ImageFormat.MemoryBmp;return format;}//保存图像pic到文件fileName中,指定图像保存格式public static void SaveToFile(Image pic, string fileName, bool replace, ImageFormat format){//若图像已存在,则删除if (System.IO.File.Exists(fileName) && replace){System.IO.File.Delete(fileName);}//若不存在则创建if (!System.IO.File.Exists(fileName)){if (format == null){format = getFormat(fileName); //根据拓展名获取图像的对应存储类型}if (format == ImageFormat.MemoryBmp){pic.Save(fileName);MessageBox.Show("图片保存成功!");}else{pic.Save(fileName, format);//按给定格式保存图像MessageBox.Show("图片保存成功!");}}}/// 缩放icon为指定的尺寸,并保存到路径PathNamepublic static void saveImage(Image image, Size size, String PathName){Image tmp = shrinkTo(image, size, false);SaveToFile(tmp, PathName, true, null);}// 截取屏幕指定区域为Image,保存到路径savePath下,haveCursor是否包含鼠标private static Image getScreen(int x = 0, int y = 0, int width = -1, int height = -1, String savePath = "", bool haveCursor = true){if (width == -1){width = SystemInformation.VirtualScreen.Width + 1280;//虚拟屏幕界限}if (height == -1){height = SystemInformation.VirtualScreen.Height + 800;}Bitmap tmp = new Bitmap(width, height); //按指定大小创建位图Graphics g = Graphics.FromImage(tmp); //从位图创建Graphics对象g.CopyFromScreen(x, y, 0, 0, new Size(width, height)); //绘制// 绘制鼠标if (haveCursor){try{CURSORINFO pci;pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO));GetCursorInfo(out pci);System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor);cur.Draw(g, new Rectangle(pci.ptScreenPos.x, pci.ptScreenPos.y, cur.Size.Width, cur.Size.Height));}catch (Exception ex){// 若获取鼠标异常则不显示}}if (!savePath.Equals("")){saveImage(tmp, tmp.Size, savePath); // 保存到指定的路径下}return tmp; //返回构建的新图像}private static void GetCursorInfo(out CURSORINFO pci){throw new NotImplementedException();}//旋转90度按钮private void button14_Click(object sender, EventArgs e){//获取需要旋转的图片// Bitmap bmp = (Bitmap)screen;Bitmap bmp = (Bitmap)pictureBox1.Image;sw.Reset();sw.Restart();//向右90度旋转bmp.RotateFlip(RotateFlipType.Rotate270FlipXY);sw.Stop();label1.Text = "运行时间: " + sw.ElapsedMilliseconds.ToString();pictureBox2.Image = bmp.Clone() as Image;}//截屏按钮private void button13_Click(object sender, EventArgs e){screen = getScreen(); // 截取屏幕 pictureBox1.Image = screen;}}
}