標籤:winform style http java color os
把彩色圖片轉換為灰色的圖片,直接用.net介面遍曆每個像素點轉換的效率非常低,800K的圖片65萬像素我的電腦要用5分鐘,而用了unsafe,速度提高了幾千倍,同樣的圖片只用了0.幾秒
附一個常用的遍曆像素點轉換的代碼
建構函式
C#代碼
- public Tphc()
- {
- InitializeComponent();
- this.pictureBox1.ImageLocation = "F:\\黑色頭髮.jpg";
- }
按鈕單擊事件
C#代碼
- private void button3_Click(object sender, 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 pixel;
- for (int x = 0; x < Width; x++)
- for (int y = 0; y < Height; y++)
- {
- pixel = MyBitmap.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;
- }
- bitmap.SetPixel(x, y, Color.FromArgb(Result, Result, Result));
- }
- this.pictureBox1.Image = bitmap;
- }