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;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace 图片浏览器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap MyBitmap;
private void button1_Click(object sender, EventArgs e)
{
//string myname;
openFileDialog1.Filter = "(*.jpg)|*.jpg|(*.bmp)|*.bmp|(*.gif)|*.gif|(*.ico)|*.ico|(*.png)|*.png|(*.*)|*.*";
//openFileDialog1.ShowDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//在控件上显示图片方法1
//myname = openFileDialog1.FileName;
//pictureBox1.Image = Image.FromFile(myname);
//在控件上显示图片方法2
if (this.openFileDialog1.FileName.Trim() == "")
return;
try
{
Bitmap SrcBitmap = new Bitmap(this.openFileDialog1.FileName);
MyBitmap = new Bitmap(SrcBitmap, this.pictureBox1.Width, this.pictureBox1.Height);
this.pictureBox1.Image = MyBitmap; //在控件上显示图片
}
catch
{
MessageBox.Show("打开文件错误");
}
}
}
private void button3_Click(object sender, EventArgs e)
{
//缩小图像
if (pictureBox1.Width >= 50)
{
pictureBox1.Width = Convert.ToInt32(pictureBox1.Width * 0.8);
pictureBox1.Height = Convert.ToInt32(pictureBox1.Height * 0.8);
}
else
{
MessageBox.Show(this, "图像已是最小,不能再小了", "提示对话框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button2_Click(object sender, EventArgs e)
{
//放大图像
if (pictureBox1.Width < this.Width)
{
pictureBox1.Width = Convert.ToInt32(pictureBox1.Width * 1.2);
pictureBox1.Height = Convert.ToInt32(pictureBox1.Height * 1.2);
}
else
{
MessageBox.Show(this, "已经是最大不能再大了", "提示对话框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button6_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button4_Click(object sender, EventArgs e)
{
//图片上增加文字,单色
if (textBox1.Text != "")
{
Graphics g = pictureBox1.CreateGraphics();
SolidBrush brush = new SolidBrush(Color.Red); //定义画笔类型及其颜色
Font myfont = new Font("黑体", 20);
//调用DrawString方法在图像上写文字
g.DrawString(textBox1.Text, myfont, brush, new Rectangle(120, 5, 280, 120));
}
else
{
MessageBox.Show("请输入要加载的文字", "提示圣对话框", MessageBoxButtons.OK,MessageBoxIcon.Warning );
}
}
private void button5_Click(object sender, EventArgs e)
{
//图片上增加文字,渐变
if (textBox1.Text != "")
{
Graphics g = pictureBox1.CreateGraphics();
//定义线形画笔
LinearGradientBrush mybrush = new LinearGradientBrush(ClientRectangle, Color.Yellow, Color.Red,
LinearGradientMode.BackwardDiagonal);
Font myfont = new Font("隶书", 25);
g.DrawString(textBox1.Text, myfont, mybrush, new Rectangle(120, 50, 280, 100));
}
else
{
MessageBox.Show(this,"请输入要加载的文字", "提示对话框", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void button7_Click(object sender, EventArgs e)
{
//水平遮罩
int iWidth = this.pictureBox1.Width;
int iHeight = this.pictureBox1.Height;
Graphics g = this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
Bitmap bitmap = new Bitmap(iWidth, iHeight);
int x = 0;
while (x <= iWidth / 2)
{
for (int i = 0; i <= iHeight - 1; i++)
{
//调用SetPixel方法.MyBitmap.GetPixel(x, i)=>获取picturebox中图片的颜色
//bitmap.SetPixel(x, i, MyBitmap.GetPixel(x, i))在新的bitmap中图片的颜色
//左边遮罩
bitmap.SetPixel(x, i, MyBitmap.GetPixel(x, i));
}
for (int i = 0; i <= iHeight - 1; i++)
{
//右边遮罩
bitmap.SetPixel(iWidth - x - 1, i, MyBitmap.GetPixel(iWidth - x - 1, i));
}
x++;
this.pictureBox1.Refresh();
this.pictureBox1.Image = bitmap;
System.Threading.Thread.Sleep(3);
}
}
private void button8_Click(object sender, EventArgs e)
{
//垂直遮罩
int iWidth = this.pictureBox1.Width;
int iHeight = this.pictureBox1.Height;
Graphics g = this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
Bitmap bitmap = new Bitmap(iWidth, iHeight);
int x = 0;
while (x <= iHeight / 2)
{
for (int i = 0; i <= iWidth - 1; i++)
{
bitmap.SetPixel(i,x, MyBitmap.GetPixel(i,x)); //调用SetPixel方法
}
for (int i = 0; i <= iWidth - 1; i++)
{
bitmap.SetPixel(i,iHeight - x - 1, MyBitmap.GetPixel(i,iHeight - x - 1));
}
x++;
this.pictureBox1.Refresh();
this.pictureBox1.Image = bitmap;
System.Threading.Thread.Sleep(3);
}
}
private void button9_Click(object sender, EventArgs e)
{
//上下拉抻
int iWidth = this.pictureBox1.Width;
int iHeight = this.pictureBox1.Height;
Graphics g = this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
for (int y = 0; y < iHeight; y++)
{
g.DrawImage(MyBitmap, 0, 0, iWidth, y);
System.Threading.Thread.Sleep(3);
}
}
private void button10_Click(object sender, EventArgs e)
{
//左右拉伸
int iWidth = this.pictureBox1.Width;
int iHeight = this.pictureBox1.Height;
Graphics g = this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
for (int x = 0; x < iWidth ; x++)
{
g.DrawImage(MyBitmap, 0, 0, x,iHeight);
System.Threading.Thread.Sleep(3);
}
}
private void button11_Click(object sender, EventArgs e)
{
//两边拉伸
int iWidth = this.pictureBox1.Width;
int iHeight = this.pictureBox1.Height;
Graphics g = this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
for (int y = 0; y <= iWidth / 2; y++)
{
Rectangle DestRect = new Rectangle(iWidth / 2 - y, 0, 2 * y, iHeight);
Rectangle SrcRect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
g.DrawImage(MyBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
System.Threading.Thread.Sleep(3);
}
}
private void button12_Click(object sender, EventArgs e)
{
//水平百味窗
MyBitmap = (Bitmap)this.pictureBox1.Image.Clone();
int dh = MyBitmap.Height / 20;
int dw = MyBitmap.Width;
Graphics g = this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
Point[] MyPoint = new Point[20];
for (int y = 0; y < 20; y++)
{
MyPoint[y].X = 0;
MyPoint[y].Y = y * dh;
}
Bitmap bitmap = new Bitmap(MyBitmap.Width, MyBitmap.Height);
for (int i = 0; i < dh; i++)
{
for (int j = 0; j < 20; j++)
{
for (int k = 0; k < dw; k++)
{
bitmap.SetPixel(MyPoint[j].X + k, MyPoint[j].Y + i,
MyBitmap.GetPixel(MyPoint[j].X + k, MyPoint[j].Y + i));
}
}
this.pictureBox1.Refresh();
this.pictureBox1.Image = bitmap;
System.Threading.Thread.Sleep(3);
}
}
private void button13_Click(object sender, EventArgs e)
{
//垂直百味窗
MyBitmap=(Bitmap)this.pictureBox1.Image.Clone();
int dw=MyBitmap.Width/30;
int dh=MyBitmap.Height;
Graphics g=this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
Point []MyPoint=new Point[30];
for(int x=0;x<30;x++)
{
MyPoint[x].Y=0;
MyPoint[x].X=x*dw;
}
Bitmap bitmap=new Bitmap(MyBitmap.Width,MyBitmap.Height);
for (int i = 0; i < dw; i++)
{
for (int j = 0; j < 30; j++)
{
for (int k = 0; k < dh; k++)
{
bitmap.SetPixel(MyPoint[j].X + i, MyPoint[j].Y + k, MyBitmap.GetPixel(MyPoint[j].X + i, MyPoint[j].Y + k));
}
}
this.pictureBox1.Refresh(); //刷新图像
this.pictureBox1.Image = bitmap;
System.Threading.Thread.Sleep(3);
}
}
private void button14_Click(object sender, EventArgs e)
{
int iWidth = this.pictureBox1.Width;
int iHeight = this.pictureBox1.Height;
Graphics g = this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
for (int x = -iWidth / 2; x <= iWidth / 2; x++)
{
Rectangle DestRect = new Rectangle(0, iHeight / 2, iWidth, 2 * x);
Rectangle SrcRect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
g.DrawImage(MyBitmap, DestRect, SrcRect, GraphicsUnit.Pixel);
System.Threading.Thread.Sleep(10);
}
}
private void button15_Click(object sender, EventArgs e)
{
int iWidth = this.pictureBox1.Width;
int iHeight = this.pictureBox1.Height;
Graphics g = this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray);
for (int x = 0; x <= iWidth / 2; x++)
{
Rectangle DestRect = new Rectangle(iWidth / 2 - x, iHeight / 2 - x, 2 * x, 2 * x);
Rectangle SrcRect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
g.DrawImage(MyBitmap, DestRect, SrcRect, GraphicsUnit.Pixel );
System.Threading.Thread.Sleep(10);
}
}
//纹理图像
private void button17_Click(object sender, EventArgs e)
{
Image myImage=Image .FromFile (openFileDialog1 .FileName );
MyBitmap = new Bitmap(myImage);
Rectangle rect = new Rectangle(0, 0, MyBitmap.Width, MyBitmap.Height);
//指定图像锁定到内存中
BitmapData bmpDate = MyBitmap.LockBits(rect, ImageLockMode.ReadWrite, MyBitmap.PixelFormat);
//获取像素图像中第一个像素图像的地址
IntPtr ptr = bmpDate.Scan0;
int bytes = MyBitmap.Width * MyBitmap.Height * 3;
byte[] rgbValues = new byte[bytes];
//使用RGB值为声明的rgbValues数组赋值
System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
for (int counter = 0; counter < rgbValues.Length; counter += 3)
{
rgbValues[counter] = 125;
}
//使用RGB值为图像的像素点着色
System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);
//从内存中解锁图像
MyBitmap.UnlockBits(bmpDate);
this.pictureBox1.Image = myImage;
}
//浮雕图像
private void button16_Click(object sender, EventArgs e)
{
Image myImage = Image.FromFile(openFileDialog1.FileName);
Bitmap myBitmap = new Bitmap(myImage);
for (int i = 0; i < myBitmap.Width - 1; i++)
{
for (int j = 0; j < myBitmap.Height - 1; j++)
{
//调用GetPixel方法获取像素点
Color color1 = myBitmap.GetPixel(i, j);
Color color2 = myBitmap.GetPixel(i + 1, j + 1);
int red = Math.Abs(color1.R - color2.R + 128);
int green = Math.Abs(color1.G - color2.G + 128);
int blue = Math.Abs(color1.B - color2.B + 128);
//颜色处理
if (red > 255) red = 255;
if (red < 0) red = 0;
if (green > 255) green = 255;
if (green < 0) green = 0;
if (blue > 255) blue = 255;
if (blue < 0) blue = 0;
//使用SetPixel方法设置像素点的颜色
myBitmap.SetPixel(i, j, Color.FromArgb(red, green, blue));
}
}
this.pictureBox1.Image = myBitmap;
}
private void button18_Click(object sender, EventArgs e)
{
Image myImage = Image.FromFile(openFileDialog1.FileName);
MyBitmap = new Bitmap(myImage);
int intWidth = MyBitmap.Width / 50;
int intHeight = MyBitmap.Height / 50;
Graphics myGraphics = this.CreateGraphics();
myGraphics.Clear(Color.WhiteSmoke);
Point[] myPoint = new Point[2500];
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
myPoint[i * 50 + j].X = i * intWidth;
myPoint[i * 50 + j].Y = j * intHeight;
}
}
Bitmap bitmap = new Bitmap(MyBitmap.Width, MyBitmap.Height);
for (int i = 0; i < 10000; i++)
{
Random rand = new Random();
int intPos = rand.Next(2500);//随机数不超过2500;
for (int m = 0; m < intWidth; m++)
{
for (int n = 0; n < intHeight; n++)
{
bitmap.SetPixel(myPoint[intPos].X + m, myPoint[intPos].Y + n,
MyBitmap.GetPixel(myPoint[intPos].X + m, myPoint[intPos].Y + n));
}
}
this.Refresh(); //刷新程序
this.pictureBox1.Image = bitmap;
for (int k = 0; k < 2500; k++)
{
for (int m = 0; m < intWidth; m++)
{
for (int n = 0; n < intHeight; n++)
{
bitmap .SetPixel (myPoint [k].X +m,myPoint [k].Y +n,
MyBitmap.GetPixel(myPoint[k].X + m, myPoint[k].Y + n));
}
}
this.Refresh();
this.pictureBox1.Image = bitmap;
}
}
}
//将图片另存为
private void button19_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
SaveFileDialog _SaveFileDialog = new SaveFileDialog();
_SaveFileDialog.Filter = "*.jpg|*.jpg";
if (_SaveFileDialog.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image.Save(_SaveFileDialog.FileName, ImageFormat.Jpeg);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}