当前位置: 代码迷 >> C# >> C# winform 实现图片特效遇到难题,
  详细解决方案

C# winform 实现图片特效遇到难题,

热度:54   发布时间:2016-05-05 02:37:57.0
C# winform 实现图片特效遇到难题,求助!

public class ImageClass  
 {   
       /// <summary>    
       /// 将图片转换成黑白色效果     
   /// </summary>    
      /// <param name="bmp">Bitmap 对象</param>     
      /// <param name="picBox">PictureBox 对象</param>    
      public static void HeiBaiSeImage(Bitmap bmp, PictureBox picBox)    
      {      
             //以黑白效果显示图像       
       Bitmap oldBitmap;      
              Bitmap newBitmap=null;      
               try   
                {        
                          int Height = bmp.Height;      
                          int Width = bmp.Width;       
                          newBitmap = new Bitmap(Width, Height);         
                          oldBitmap = bmp;       
                          Color pixel;      
                          for (int x = 0; x < Width; x++)        
                          for (int y = 0; y < Height; y++)         
                          {            
                                 pixel = oldBitmap.GetPixel(x, y);         
                                 int r, g, b, Result = 0;        
                                 r = pixel.R;        
                                 g = pixel.G;            
                                 b = pixel.B;           
                                //实例程序以加权平均值法产生黑白图像            
                int iType = 2;          
                                switch (iType)          
                                {              
                                       case 0://平均值法              
                    Result = ((r + g + b) / 3);           
                                        break;           
                                       case 1://最大值法            
                    Result = r > g ? r : g;             
                                        Result = Result > b ? Result : b;                
                                        break;           
                                        case 2://加权平均值法            
                    Result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b));            
                                        break;  
                                    }         
                                  newBitmap.SetPixel(x, y, Color.FromArgb(Result, Result, Result));          
                          }      
                  }     
                  catch (Exception ex)  
                  {        
                          MessageBox.Show(ex.Message, "信息提示");     
                  }      
                   picBox.Image = newBitmap; 
           }
}
           我的问题是,如何在按钮的单击事件中执行上述代码,以达到让图片控件中的彩色图像变成黑白色的效果?
------解决思路----------------------
var img=Image.Fromxxx();
ImageClass.HeiBaiImage(img,this.picturebox1)
调用静态方法都不会了?
------解决思路----------------------
方法:/// <summary> 
        /// 图像灰度化 
        /// </summary> 
        /// <param name="bmp"></param> 
        /// <returns></returns> 
        public static Bitmap ToGray2(Bitmap bmp)
        {
            for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {
                    //获取该点的像素的RGB的颜色 
                    Color color = bmp.GetPixel(i, j);
                    //利用公式计算灰度值(加权平均法)
                    int gray = (int)(color.R * 0.299 + color.G * 0.587 + color.B * 0.114);
                    Color newColor = Color.FromArgb(gray, gray, gray);
                    bmp.SetPixel(i, j, newColor);
                }
            }
            return bmp;
        }

调用:this.pictureBox1.Image = ToGray2(new System.Drawing.Bitmap(this.pictureBox1.Image));
------解决思路----------------------
引用:
方法:/// <summary> 
        /// 图像灰度化 
        /// </summary> 
        /// <param name="bmp"></param> 
        /// <returns></returns> 
        public static Bitmap ToGray2(Bitmap bmp)
        {
            for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {
                    //获取该点的像素的RGB的颜色 
                    Color color = bmp.GetPixel(i, j);
                    //利用公式计算灰度值(加权平均法)
                    int gray = (int)(color.R * 0.299 + color.G * 0.587 + color.B * 0.114);
                    Color newColor = Color.FromArgb(gray, gray, gray);
                    bmp.SetPixel(i, j, newColor);
                }
            }
            return bmp;
        }

调用:this.pictureBox1.Image = ToGray2(new System.Drawing.Bitmap(this.pictureBox1.Image));



用C#来做这些运算本来就不适合,效率太低了, 你如果非要这么干 , 为何不用并行计算?? 现在的CPU都是多核的.
------解决思路----------------------
引用:
Quote: 引用:

方法:/// <summary> 
        /// 图像灰度化 
        /// </summary> 
        /// <param name="bmp"></param> 
        /// <returns></returns> 
        public static Bitmap ToGray2(Bitmap bmp)
        {
            for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {
                    //获取该点的像素的RGB的颜色 
                    Color color = bmp.GetPixel(i, j);
                    //利用公式计算灰度值(加权平均法)
                    int gray = (int)(color.R * 0.299 + color.G * 0.587 + color.B * 0.114);
                    Color newColor = Color.FromArgb(gray, gray, gray);
                    bmp.SetPixel(i, j, newColor);
                }
            }
            return bmp;
        }

调用:this.pictureBox1.Image = ToGray2(new System.Drawing.Bitmap(this.pictureBox1.Image));



用C#来做这些运算本来就不适合,效率太低了, 你如果非要这么干 , 为何不用并行计算?? 现在的CPU都是多核的.
矩阵才是王道,一个一个像素的弄就算多线程也是废柴
  相关解决方案