Fire my passion

Anything with my most passion……
使用GDI+制作反色图片

近来研究了一下GDI+绘图的基本操作,也学到了不少东西

绘制灰色图片:

        System.Drawing.Bitmap bm = new System.Drawing.Bitmap

(this.pictureBox1.Image.Width,

this.pictureBox1.Image.Height,

System.Drawing.Imaging.PixelFormat.Format24bppRgb);

        System.Drawing.Bitmap bmOriginal = (Bitmap)this.pictureBox1.Image;

        for(int y=0;y<bmOriginal.Height;y++)

        {

                for(int x=0;x<bmOriginal.Width;x++)

                {

                        Color c=bmOriginal.GetPixel(x,y);

                        int red,green,blue;

                        red = (int)(c.R*0.3 + c.G*0.59+ c.B*0.11);

                        green = (int)(c.R*0.3 + c.G*0.59+ c.B*0.11);

                        blue = (int)(c.R*0.3 + c.G*0.59+ c.B*0.11);

                        bm.SetPixel(x,y,Color.FromArgb(red,green,blue));

                }

        }

this.pictureBox1.Image = bm;



绘制反色图片:


       
System.Drawing.Bitmap bm = new System.Drawing.Bitmap

(this.pictureBox1.Image.Width,

this.pictureBox1.Image.Height,

System.Drawing.Imaging.PixelFormat.Format24bppRgb);

        System.Drawing.Bitmap bmOriginal = (Bitmap)this.pictureBox1.Image;

        for(int y=0;y<bmOriginal.Height;y++)

        {

                for(int x=0;x<bmOriginal.Width;x++)

                {

                    Color c=bmOriginal.GetPixel(x,y);

                    int red,green,blue;    

      red = (int)(255-c.R);

                    green = (int)(255-c.G);

      blue = (int)(255-c.B);        
   }            
                                                

   bm.SetPixel(x,y,Color.FromArgb(red,green,blue));

               

        }


        this
.pictureBox1.Image = bm;


在使用
GraphicsDrawImage方法时,graphics使用Graphics.FromImage(Image)获得,既可绘制Image.

 

还有许多技巧,具体参考:http://www.bobpowell.net/faqmain.htmcodeproject上的一些文章和代码

 

posted on 2006-11-30 17:38  everx  阅读(1497)  评论(2编辑  收藏  举报