.NET ez 2012-06-20
利用 GetPixel 及 SetPixel,將圖片轉換為灰階。
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string Photo_route = Path.Combine(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Tulips.jpg");
Bitmap b = (Bitmap)Bitmap.FromFile(Photo_route);
pictureBox2.Image = (Bitmap)Bitmap.FromFile(Photo_route);
Stopwatch sw = new Stopwatch();
sw.Start();
//對每個pixel取出處理
for (int y = 0; y < b.Height; y++)
{
for (int x = 0; x < b.Width; x++)
{
Color color = b.GetPixel(x, y);
int avg = (color.R + color.G + color.B) / 3; //RGB同除3就會變成灰階
b.SetPixel(x, y, Color.FromArgb(avg, avg, avg));
}
}
sw.Stop();
pictureBox1.Image = b;
MessageBox.Show(sw.ElapsedMilliseconds.ToString() + " /ms");
}
}
}※因為將每個 pixel 取出後再寫入,所以效能會較差!
標籤: .NET
本文章網址:
https://www.ez2o.com/Blog/Post/csharp-Image-Gray-GetPixel-SetPixel
https://www.ez2o.com/Blog/Post/78
https://www.ez2o.com/Blog/Post/csharp-Image-Gray-GetPixel-SetPixel
https://www.ez2o.com/Blog/Post/78
