c#图像操作

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

c#图像操作
c#图像操作
1====================
private void button2_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{//以任意⾓度旋转显⽰图像
if(this.StrFileName.Trim()=="")
return;
System.Drawing.Bitmap MyBitmap=new Bitmap(this.StrFileName);
Graphics g = e.Graphics;
TextureBrush MyBrush = new TextureBrush(MyBitmap);
switch(this.iFlag)
{
case 1:
MyBrush.RotateTransform(0.0f); //正常显⽰图像
break;
case 2:
MyBrush.RotateTransform(18.0f); //顺时针旋转18度显⽰图像
break;
case 3:
MyBrush.RotateTransform(26.0f); //顺时针旋转26度显⽰图像
break;
}
/*
switch(this.iFlag)
{
case 1:
g.RotateTransform(0.0f); //正常显⽰图像
break;
case 2:
g.RotateTransform(18.0f); //顺时针旋转18度显⽰图像
break;
case 3:
g.RotateTransform(26.0f); //顺时针旋转26度显⽰图像
break;
}
*/
g.FillRectangle(MyBrush,0,0,this.ClientRectangle.Width,this.ClientRectangle.Height);
}
2==============================
private void button2_Click(object sender, System.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(100);
}
}
3========================
private void button2_Click(object sender, System.EventArgs e)
{//以浮雕⽅式显⽰图像
int Height=this.pictureBox1.Image.Height;
int Width=this.pictureBox1.Image.Width;
Bitmap bitmap=new Bitmap(Width,Height);
Bitmap MyBitmap=(Bitmap)this.pictureBox1.Image;
Color pixel1,pixel2;
for(int x=0;x<Width-1;x++)
{
for(int y=0;y<Height-1;y++)
{
int r=0,g=0,b=0;
pixel1=MyBitmap.GetPixel(x,y);
pixel2=MyBitmap.GetPixel(x+1,y+1);
r=Math.Abs(pixel1.R-pixel2.R+128);
g=Math.Abs(pixel1.G-pixel2.G+128);
b=Math.Abs(pixel1.B-pixel2.B+128);
if(r>255)
r=255;
if(r<0)
r=0;
if(g>255)
g=255;
if(g<0)
g=0;
if(b>255)
b=255;
if(b<0)
b=0;
bitmap.SetPixel(x,y,Color.FromArgb(r,g,b));
}
}
this.pictureBox1.Image=bitmap;
}
3=============================
private void button3_Click(object sender, System.EventArgs e)
{//复制图像
if(this.pictureBox1.Image.Size.Height<1)
{
MessageBox.Show("请装⼊图像后再执⾏本操作!","信息提⽰",MessageBoxButtons.OK,rmation); return;
}
Clipboard.SetDataObject(this.pictureBox1.Image.Clone());
MessageBox.Show("复制图像操作成功!","信息提⽰",MessageBoxButtons.OK,rmation);
}
private void button4_Click(object sender, System.EventArgs e)
{//粘贴图像
try
{
IDataObject MyData = Clipboard.GetDataObject ( ) ;
//判断剪切板中数据是不是位图
if ( MyData.GetDataPresent ( DataFormats.Bitmap ) )
{
//获得位图对象
Bitmap MyBitmap = ( Bitmap ) MyData.GetData ( DataFormats.Bitmap ) ;
//显⽰图⽚
this.pictureBox1.Image=MyBitmap;
}
else
{//如果剪贴板上没有图像⽂件,则发出提醒
MessageBox.Show("没有可显⽰的图像","信息提⽰",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
}
catch(Exception Err)
{//读取剪贴板出错处理
MessageBox.Show("错误信息是: "+Err.Message,"信息提⽰",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
4==============================
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{//绘制渐变⾊背景
Graphics g = e.Graphics;
System.Drawing.Drawing2D.LinearGradientBrush MyBrush =new System.Drawing.Drawing2D.LinearGradientBrush( this.ClientRectangle,Color.White, Color.Blue,
System.Drawing.Drawing2D.LinearGradientMode.Vertical);
g.FillRectangle(MyBrush, this.ClientRectangle);
}
private void Form1_Resize(object sender, System.EventArgs e)
{//缩放窗⼝时更新背景
this.Refresh();
}
5========================
using System.Drawing.Drawing2D;
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{//绘制路径渐变图形
Graphics gp=e.Graphics;
//绘制第⼀个图形
//设置第⼀个图形的点坐标数组
Point []myPtsA={new Point(10,10),new Point(90,10),new Point(170,10),new Point(170,50),
new Point(170,90),new Point(90,90), new Point(10,90),new Point(10,50)};
//设置第⼀个图形的颜⾊数组
Color []myColsA = { Color.White, Color.Black,Color.White, Color.Black,
Color.White,Color.Black,Color.White,Color.Black};
PathGradientBrush myPGBrushA=new PathGradientBrush(myPtsA);
myPGBrushA.SurroundColors=myColsA;
gp.FillRectangle(myPGBrushA,10,10,160,80);
//绘制第⼆个图形
//设置第⼆个图形的点坐标数组
Point []myPtsB={new Point(180,10),new Point(260,10),new Point(340,10),new Point(340,50),
new Point(340,90),new Point(260,90),new Point(180,90),new Point(180,50)};
//设置第⼆个图形的颜⾊数组
Color []myColsB = {Color.Green,Color.Blue, Color.Green, Color.Blue,
Color.Green, Color.Blue, Color.Green,Color.Blue,};
PathGradientBrush myPGBrushB=new PathGradientBrush(myPtsB);
myPGBrushB.SurroundColors=myColsB;
myPGBrushB.CenterColor=Color.Yellow;
gp.FillRectangle(myPGBrushB,180,10,340,80);
//绘制第三个图形
//设置第三个图形的点坐标数组
Point []myPtsC ={new Point(90, 100),new Point(115,165),new Point(170,170),
new Point(125,210),new Point(140,270),new Point(90,235),new Point(40,270),
new Point(55,210), new Point(10,170), new Point(65,165)};
GraphicsPath myPathC=new GraphicsPath();
myPathC.AddLines(myPtsC);
PathGradientBrush myPGBrushC=new PathGradientBrush(myPathC);
myPGBrushC.CenterColor=Color.Yellow;
//设置第三个图形的颜⾊数组
Color []myColorsC = {Color.Red,Color.Yellow,Color.Red,Color.Yellow,
Color.Red, Color.Yellow,Color.Red, Color.Yellow,Color.Red,Color.Yellow};
myPGBrushC.SurroundColors=myColorsC;
gp.FillPath(myPGBrushC, myPathC);
//绘制第四个图形
//设置第四个图形的点坐标数组
Point []myPtsD ={new Point(180, 170),new Point(240,100),new Point(340,100),
new Point(340,200),new Point(280,270),new Point(180,270), };
GraphicsPath myPathD=new GraphicsPath();
myPathD.AddLines(myPtsD);
PathGradientBrush myPGBrushD=new PathGradientBrush(myPathD);
myPGBrushD.CenterPoint=new Point(280,170);
myPGBrushD.CenterColor=Color.White;
//设置第四个图形的颜⾊数组
Color []myColorsD = {Color.Cyan,Color.Blue,Color.Cyan,
Color.Green,Color.Cyan,Color.LightGreen };
myPGBrushD.SurroundColors=myColorsD;
gp.FillPath(myPGBrushD, myPathD);
}
6================
private void button1_Click(object sender, System.EventArgs e)
{//浏览图像⽂件
this.openFileDialog1.ShowDialog();
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(Exception Err)
{
MessageBox.Show(this,"打开图像⽂件错误!","信息提⽰",MessageBoxButtons.OK,rmation); }
}
private void button2_Click(object sender, System.EventArgs e)
{//从左到右拉伸显⽰
int iWidth=this.pictureBox1.Width; //图像宽度
int iHeight=this.pictureBox1.Height; //图像⾼度
//取得Graphics对象
Graphics g=this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray); //初始为全灰⾊
for(int x=0;x<=iWidth;x++)
{
g.DrawImage(MyBitmap,0,0,x,iHeight);
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
MyBitmap=(Bitmap)this.pictureBox1.Image;
}
private void button3_Click(object sender, System.EventArgs e)
{//从上到下拉伸显⽰
int iWidth=this.pictureBox1.Width; //图像宽度
int iHeight=this.pictureBox1.Height; //图像⾼度
//取得Graphics对象
Graphics g=this.pictureBox1.CreateGraphics();
g.Clear(Color.Gray); //初始为全灰⾊
for(int y=0;y<=iHeight;y++)
{
g.DrawImage(MyBitmap,0,0,iWidth,y);
}
}
private void button4_Click(object sender, System.EventArgs e)
{//四周扩散显⽰
int iWidth=this.pictureBox1.Width; //图像宽度
int iHeight=this.pictureBox1.Height; //图像⾼度
//取得Graphics对象
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);
}
}
private void button5_Click(object sender, System.EventArgs e)
{//反转图像
int iWidth=this.pictureBox1.Width; //图像宽度
int iHeight=this.pictureBox1.Height; //图像⾼度
//取得Graphics对象
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-x,
iWidth,2*x);
Rectangle SrcRect=new Rectangle(0,0,MyBitmap.Width,MyBitmap.Height);
g.DrawImage(MyBitmap,DestRect,SrcRect,GraphicsUnit.Pixel);
}
}
private void button6_Click(object sender, System.EventArgs e)
{//两边拉伸显⽰
int iWidth=this.pictureBox1.Width; //图像宽度
int iHeight=this.pictureBox1.Height; //图像⾼度
//取得Graphics对象
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);
}
}
private void button7_Click(object sender, System.EventArgs e)
{//上下对接显⽰
int iWidth=this.pictureBox1.Width; //图像宽度
int iHeight=this.pictureBox1.Height; //图像⾼度
//取得Graphics对象
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));
}
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;
}
}
private void button8_Click(object sender, System.EventArgs e)
{//左右对接显⽰
int iWidth=this.pictureBox1.Width; //图像宽度
int iHeight=this.pictureBox1.Height; //图像⾼度
//取得Graphics对象
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++)
{
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;
}
}
7=======================
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {//旋转显⽰⽂字
Graphics g=e.Graphics;
g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
for(int i=0;i<=360;i+=10)
{
//平移Graphics对象到窗体中⼼
g.TranslateTransform(this.Width/2,this.Height/2);
//设置Graphics对象的输出⾓度
g.RotateTransform(i);
//设置⽂字填充颜⾊
Brush MyBrush=Brushes.Red;
//旋转显⽰⽂字
g.DrawString("......China is a great country",this.Font,MyBrush,0,0);
//恢复全局变换矩阵
g.ResetTransform();
}
}。

相关文档
最新文档