C#對映像像素處理的三種方式

來源:互聯網
上載者:User

在C#中,可以採用直接擷取像素法(GetPixel)、記憶體拷貝法和指標法(unsafe)來擷取映像像素並進行處理。

下面以映像的灰階化為例說明具體的處理方法和速度的比較(1G記憶體,P4處理器測試)。

1.GetPixel方法

GetPixel(i,j)和SetPixel(i, j,Color)可以直接得到映像的一個像素的Color結構,但是處理速度比較慢,處理一副180*180的映像大約需要100.48ms。

private void pixel_Click(object sender, EventArgs e){    if(curBitmap != null)    {        myTimer.ClearTimer();        myTimer.Start();        Color curColor;        int ret;        for (int i = 0; i < curBitmap.Width; i++)        {            for (int j = 0; j < curBitmap.Height ; j++)            {                curColor = curBitmap.GetPixel(i,j);                ret = (int)(curColor.R * 0.299 + curColor.G * 0.587 + curColor.B * 0.114);                curBitmap.SetPixel(i, j, Color.FromArgb(ret, ret, ret));            }        }        myTimer.Stop();        timeBox.Text = myTimer.Duration.ToString("####.##") + " 毫秒";         Invalidate();    }}
2.記憶體拷貝法
記憶體拷貝法就是採用System.Runtime.InteropServices.Marshal.Copy將映像資料拷貝到數組中,然後進行處理,這不需要直接對指標進行操作,不需採用unsafe,處理速度和指標處理相差不大,
處理一副180*180的映像大約需要1.32ms。
private void memory_Click(object sender, EventArgs e){    if (curBitmap != null)    {        myTimer.ClearTimer();        myTimer.Start();        Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);        System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);        IntPtr ptr = bmpData.Scan0;        int bytes = curBitmap.Width * curBitmap.Height * 3;        byte[] rgbValues = new byte[bytes];        System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);        double colorTemp = 0;        for (int i = 0; i < rgbValues.Length; i += 3)        {            colorTemp = rgbValues[i + 2] * 0.299 + rgbValues[i + 1] * 0.587 + rgbValues[i] * 0.114;            rgbValues[i] = rgbValues[i + 1] = rgbValues[i + 2] = (byte)colorTemp;        }        System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);        curBitmap.UnlockBits(bmpData);        myTimer.Stop();        timeBox.Text = myTimer.Duration.ToString("####.##") + " 毫秒";         Invalidate();    }}
3.指標法
指標在c#中屬於unsafe操作,需要用unsafe括起來進行處理,速度最快,處理一副180*180的映像大約需要1.16ms。
採用byte* ptr = (byte*)(bmpData.Scan0); 擷取映像資料根位置的指標,然後用bmpData.Scan0擷取映像的掃描寬度,就可以進行指標操作了。
 
private void pointer_Click(object sender, EventArgs e){    if (curBitmap != null)    {        myTimer.ClearTimer();        myTimer.Start();        Rectangle rect = new Rectangle(0, 0, curBitmap.Width, curBitmap.Height);        System.Drawing.Imaging.BitmapData bmpData = curBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, curBitmap.PixelFormat);        byte temp = 0;        unsafe        {            byte* ptr = (byte*)(bmpData.Scan0);            for (int i = 0; i < bmpData.Height; i++)            {                for (int j = 0; j < bmpData.Width; j++)                {                    temp = (byte)(0.299 * ptr[2] + 0.587 * ptr[1] + 0.114 * ptr[0]);                    ptr[0] = ptr[1] = ptr[2] = temp;                    ptr += 3;                }                ptr += bmpData.Stride - bmpData.Width * 3;            }        }        curBitmap.UnlockBits(bmpData);        myTimer.Stop();        timeBox.Text = myTimer.Duration.ToString("####.##") + " 毫秒";         Invalidate();    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.