• c# winform 把彩色图片转换为灰色的图片,变灰,灰度图片,速度很快,safe,unsafe


    把彩色图片转换为灰色的图片,直接用.net接口遍历每个像素点转换的效率非常低,800K的图片65万像素我的电脑要用5分钟,而用了unsafe,速度提高了几千倍,同样的图片只用了0.几秒 

    附一个常用的遍历像素点转换的代码 

    构造函数 

    C#代码  收藏代码
    1. public Tphc()  
    2. {  
    3.     InitializeComponent();  
    4.     this.pictureBox1.ImageLocation = "F:\黑色头发.jpg";  
    5. }  



    按钮单击事件 

    C#代码  收藏代码
    1. private void button3_Click(object sender, EventArgs e)  
    2. {  
    3.     int Height = this.pictureBox1.Image.Height;  
    4.     int Width = this.pictureBox1.Image.Width;  
    5.     Bitmap bitmap = new Bitmap(Width, Height);  
    6.     Bitmap MyBitmap = (Bitmap)this.pictureBox1.Image;  
    7.     Color pixel;  
    8.     for (int x = 0; x < Width; x++)  
    9.         for (int y = 0; y < Height; y++)  
    10.         {  
    11.             pixel = MyBitmap.GetPixel(x, y);  
    12.             int r, g, b, Result = 0;  
    13.             r = pixel.R;  
    14.             g = pixel.G;  
    15.             b = pixel.B;  
    16.             //实例程序以加权平均值法产生黑白图像  
    17.             int iType = 2;  
    18.             switch (iType)  
    19.             {  
    20.                 case 0://平均值法  
    21.                     Result = ((r + g + b) / 3);  
    22.                     break;  
    23.                 case 1://最大值法  
    24.                     Result = r > g ? r : g;  
    25.                     Result = Result > b ? Result : b;  
    26.                     break;  
    27.                 case 2://加权平均值法  
    28.                     Result = ((int)(0.7 * r) + (int)(0.2 * g) + (int)(0.1 * b));  
    29.                     break;  
    30.             }  
    31.             bitmap.SetPixel(x, y, Color.FromArgb(Result, Result, Result));  
    32.         }  
    33.     this.pictureBox1.Image = bitmap;  
    34. }  


  • 相关阅读:
    iPhone页面的常用调试方法
    前端代码相关规范
    使用BEM命名规范来组织CSS代码
    安卓微信页面的调试
    前端调试的那些手段
    Webpack打包构建太慢了?试试几个方法
    [前端] 记录工作中遇到的各种问题(Bug,总结,记录)
    jqPlot图表插件学习之饼状图和环状图
    jqPlot图表插件学习之阴阳烛图
    jqPlot图表插件学习之数据节点高亮和光标提示
  • 原文地址:https://www.cnblogs.com/gc2013/p/3836146.html
Copyright © 2020-2023  润新知