C#图像处理一、各种旋转、改变大小注意:先要添加画图相关的using引用。//向右旋转图像90°代码如下:privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){Graphicsg=e.Graphics;Bitmapbmp=newBitmap(rama.jpg);//加载图像g.FillRectangle(Brushes.White,this.ClientRectangle);//填充窗体背景为白色Point[]destinationPoints={newPoint(100,0),//destinationforupper-leftpointoforiginalnewPoint(100,100),//destinationforupper-rightpointoforiginalnewPoint(0,0)};//destinationforlower-leftpointoforiginalg.DrawImage(bmp,destinationPoints);}//旋转图像180°代码如下:privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){Graphicsg=e.Graphics;Bitmapbmp=newBitmap(rama.jpg);g.FillRectangle(Brushes.White,this.ClientRectangle);Point[]destinationPoints={newPoint(0,100),//destinationforupper-leftpointoforiginalnewPoint(100,100),//destinationforupper-rightpointoforiginalnewPoint(0,0)};//destinationforlower-leftpointoforiginalg.DrawImage(bmp,destinationPoints);}//图像切变代码:privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){Graphicsg=e.Graphics;Bitmapbmp=newBitmap(rama.jpg);g.FillRectangle(Brushes.White,this.ClientRectangle);Point[]destinationPoints={newPoint(0,0),//destinationforupper-leftpointoforiginalnewPoint(100,0),//destinationforupper-rightpointoforiginalnewPoint(50,100)};//destinationforlower-leftpointoforiginalg.DrawImage(bmp,destinationPoints);}//图像截取:privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){Graphicsg=e.Graphics;Bitmapbmp=newBitmap(rama.jpg);g.FillRectangle(Brushes.White,this.ClientRectangle);Rectanglesr=newRectangle(80,60,400,400);//要截取的矩形区域Rectangledr=newRectangle(0,0,200,200);//要显示到Form的矩形区域g.DrawImage(bmp,dr,sr,GraphicsUnit.Pixel);}//改变图像大小:privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){Graphicsg=e.Graphics;Bitmapbmp=newBitmap(rama.jpg);g.FillRectangle(Brushes.White,this.ClientRectangle);intwidth=bmp.Width;intheight=bmp.Height;//改变图像大小使用低质量的模式g.InterpolationMode=InterpolationMode.NearestNeighbor;g.DrawImage(bmp,newRectangle(10,10,120,120),//sourcerectanglenewRectangle(0,0,width,height),//destinationrectangleGraphicsUnit.Pixel);//使用高质量模式//g.CompositingQuality=CompositingQuality.HighSpeed;g.InterpolationMode=InterpolationMode.HighQualityBicubic;g.DrawImage(bmp,newRectangle(130,10,120,120),newRectangle(0,0,width,height),GraphicsUnit.Pixel);}//设置图像的分辩率:privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){Graphicsg=e.Graphics;Bitmapbmp=newBitmap(rama.jpg);g.FillRectangle(Brushes.White,this.ClientRectangle);bmp.SetResolution(300f,300f);g.DrawImage(bmp,0,0);bmp.SetResolution(1200f,1200f);g.DrawImage(bmp,180,0);}//用GDI+画图privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){GraphicsgForm=e.Graphics;gForm.FillRectangle(Brushes.White,this.ClientRectangle);for(inti=1;i=7;++i){//在窗体上面画出橙色的矩形Rectangler=newRectangle(i*40-15,0,15,this.ClientRectangle.Height);gForm.FillRectangle(Brushes.Orange,r);}//在内存中创建一个Bitmap并设置CompositingModeBitmapbmp=newBitmap(260,260,System.Drawing.Imaging.PixelFormat.Format32bppArgb);GraphicsgBmp=Graphics.FromImage(bmp);gBmp.CompositingMode=System.Drawing.Drawing2D.CompositingMode.SourceCopy;//创建一个带有Alpha的红色区域//并将其画在内存的位图里面Colorred=Color.FromArgb(0x60,0xff,0,0);BrushredBrush=newSolidBrush(red);gBmp.FillEllipse(redBrush,70,70,160,160);//创建一个带有Alpha的绿色区域Colorgreen=Color.FromArgb(0x40,0,0xff,0);BrushgreenBrush=newSolidBrush(green);gBmp.FillRectangle(greenBrush,10,10,140,140);//在窗体上面画出位图nowdrawthebitmaponourwindowgForm.DrawImage(bmp,20,20,bmp.Width,bmp.Height);//清理资源bmp.Dispose();gBmp.Dispose();redBrush.Dispose();greenBrush.Dispose();}//在窗体上面绘图并显示图像privatevoidForm1_Paint(objectsender,System.Windows.Forms.PaintEventArgse){Graphicsg=e.Graphics;PenblackPen=newPen(Color.Black,1);if(ClientRectangle.Height/100){for(inty=0;yClientRectangle.Height;y+=ClientRectangle.Height/10){g.DrawLine(blackPen,newPoint(0,0),newPoint(ClientRectangle.Width,y));}}blackPen.Dispose();}C#使用Bitmap类进行图片裁剪在Mapwin(手机游戏地图编辑器)生成的地图txt文件中添加自己需要处理的数据后转换成可在手机(Ophone)开发环境中使用的字节流地图文件的小工具,其中就涉及到图片的裁剪和生成了。有以下几种方式。方法一:拷贝像素。当然这种方法是最笨的,效率也就低了些。在Bitmap类中我们可以看到这样两个方法:GetPixel(intx,inty)和SetPixel(intx,inty,Colorcolor)方法。从字面的含以上就知道前者是获取图像某点像素值,是用Color对象返回的;后者是将已知像素描画到制定的位置。下面就来做个实例检验下:1.首先创建一个WindowsForm窗体程序,往该窗体上拖放7个PictureBox控件,第一个用于放置并显示原始的大图片,其后6个用于放置并显示裁剪后新生成的6个小图;2.放置原始大图的PictureBox控件name属性命名为pictureBoxBmpRes,其后pictureBox1到pictureBox6依次命名,并放置在合适的位置;3.双击Form窗体,然后在Form1_Load事件中加入下面的代码即可。//导入图像资源BitmapbmpRes=null;StringstrPath=Application.ExecutablePath;try{intnEndIndex=strPath.LastIndexOf('//');strPath=strPath.Substring(0,nEndIndex)+//Bmp//BmpResMM.bmp;bmpRes=newBitmap(strPath);//窗体上显示加载图片pictureBoxBmpRes.Width=bmpRes.Width;pictureBoxBmpRes.Height=bmpRes.Height;pictureBoxBmpRes.Image=bmpRes;}catch(Exceptionex){System.Windows.Forms.MessageBox.Show(图片资源加载失败!/r/n+ex.ToString());}//裁剪图片(裁成2行3列的6张图片)intnYClipNum=2,nXClipNum=3;Bitmap[]bmpaClipBmpArr=newBitmap[nYClipNum*nXClipNum];for(intnYClipNumIndex=0;nYClipNumIndexnYClipNum;nYClipNumIndex++){for(intnXClipNumIndex=0;nXClipNumInd