日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

解决 C# GetPixel 和 SetPixel 效率问题(转)

發布時間:2025/5/22 C# 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 解决 C# GetPixel 和 SetPixel 效率问题(转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在對Bitmap圖片操作的時候,有時需要用到獲取或設置像素顏色方法:GetPixel 和 SetPixel,

如果直接對這兩個方法進行操作的話速度很慢,這里我們可以通過把數據提取出來操作,然后操作完在復制回去可以加快訪問速度

其實對Bitmap的訪問還有兩種方式,一種是內存法,一種是指針法

1、內存法

  這里定義一個類LockBitmap,通過把Bitmap數據拷貝出來,在內存上直接操作,操作完成后在拷貝到Bitmap中

public class LockBitmap{Bitmap source = null;IntPtr Iptr = IntPtr.Zero;BitmapData bitmapData = null;public byte[] Pixels { get; set; }public int Depth { get; private set; }public int Width { get; private set; }public int Height { get; private set; }public LockBitmap(Bitmap source){this.source = source;}/// <summary>/// Lock bitmap data/// </summary>public void LockBits(){try{// Get width and height of bitmapWidth = source.Width;Height = source.Height;// get total locked pixels countint PixelCount = Width * Height;// Create rectangle to lockRectangle rect = new Rectangle(0, 0, Width, Height);// get source bitmap pixel format sizeDepth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);// Check if bpp (Bits Per Pixel) is 8, 24, or 32if (Depth != 8 && Depth != 24 && Depth != 32){throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");}// Lock bitmap and return bitmap databitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,source.PixelFormat);// create byte array to copy pixel valuesint step = Depth / 8;Pixels = new byte[PixelCount * step];Iptr = bitmapData.Scan0;// Copy data from pointer to arrayMarshal.Copy(Iptr, Pixels, 0, Pixels.Length);}catch (Exception ex){throw ex;}}/// <summary>/// Unlock bitmap data/// </summary>public void UnlockBits(){try{// Copy data from byte array to pointerMarshal.Copy(Pixels, 0, Iptr, Pixels.Length);// Unlock bitmap data source.UnlockBits(bitmapData);}catch (Exception ex){throw ex;}}/// <summary>/// Get the color of the specified pixel/// </summary>/// <param name="x"></param>/// <param name="y"></param>/// <returns></returns>public Color GetPixel(int x, int y){Color clr = Color.Empty;// Get color components countint cCount = Depth / 8;// Get start index of the specified pixelint i = ((y * Width) + x) * cCount;if (i > Pixels.Length - cCount)throw new IndexOutOfRangeException();if (Depth == 32) // For 32 bpp get Red, Green, Blue and Alpha {byte b = Pixels[i];byte g = Pixels[i + 1];byte r = Pixels[i + 2];byte a = Pixels[i + 3]; // aclr = Color.FromArgb(a, r, g, b);}if (Depth == 24) // For 24 bpp get Red, Green and Blue {byte b = Pixels[i];byte g = Pixels[i + 1];byte r = Pixels[i + 2];clr = Color.FromArgb(r, g, b);}if (Depth == 8)// For 8 bpp get color value (Red, Green and Blue values are the same) {byte c = Pixels[i];clr = Color.FromArgb(c, c, c);}return clr;}/// <summary>/// Set the color of the specified pixel/// </summary>/// <param name="x"></param>/// <param name="y"></param>/// <param name="color"></param>public void SetPixel(int x, int y, Color color){// Get color components countint cCount = Depth / 8;// Get start index of the specified pixelint i = ((y * Width) + x) * cCount;if (Depth == 32) // For 32 bpp set Red, Green, Blue and Alpha {Pixels[i] = color.B;Pixels[i + 1] = color.G;Pixels[i + 2] = color.R;Pixels[i + 3] = color.A;}if (Depth == 24) // For 24 bpp set Red, Green and Blue {Pixels[i] = color.B;Pixels[i + 1] = color.G;Pixels[i + 2] = color.R;}if (Depth == 8)// For 8 bpp set color value (Red, Green and Blue values are the same) {Pixels[i] = color.B;}}}
  使用:先鎖定Bitmap,然后通過Pixels操作顏色對象,最后釋放鎖,把數據更新到Bitmap中 string file = @"C:\test.jpg";Bitmap bmp = new Bitmap(Image.FromFile(file));LockBitmap lockbmp = new LockBitmap(bmp);//鎖定Bitmap,通過Pixel訪問顏色 lockbmp.LockBits();//獲取顏色Color color = lockbmp.GetPixel(10, 10);//從內存解鎖Bitmaplockbmp.UnlockBits();

?

2、指針法這種方法訪問速度比內存法更快,直接通過指針對內存進行操作,不需要進行拷貝,但是在C#中直接通過指針操作內存是不安全的,所以需要在代碼中加入unsafe關鍵字,在生成選項中把允許不安全代碼勾上,才能編譯通過這里定義成PointerBitmap類 public class PointBitmap{Bitmap source = null;IntPtr Iptr = IntPtr.Zero;BitmapData bitmapData = null;public int Depth { get; private set; }public int Width { get; private set; }public int Height { get; private set; }public PointBitmap(Bitmap source){this.source = source;}public void LockBits(){try{// Get width and height of bitmapWidth = source.Width;Height = source.Height;// get total locked pixels countint PixelCount = Width * Height;// Create rectangle to lockRectangle rect = new Rectangle(0, 0, Width, Height);// get source bitmap pixel format sizeDepth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);// Check if bpp (Bits Per Pixel) is 8, 24, or 32if (Depth != 8 && Depth != 24 && Depth != 32){throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");}// Lock bitmap and return bitmap databitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,source.PixelFormat);//得到首地址unsafe{Iptr = bitmapData.Scan0;//二維圖像循環 }}catch (Exception ex){throw ex;}}public void UnlockBits(){try{source.UnlockBits(bitmapData);}catch (Exception ex){throw ex;}}public Color GetPixel(int x, int y){unsafe{byte* ptr = (byte*)Iptr;ptr = ptr + bitmapData.Stride * y;ptr += Depth * x / 8;Color c = Color.Empty;if (Depth == 32){int a = ptr[3];int r = ptr[2];int g = ptr[1];int b = ptr[0];c = Color.FromArgb(a, r, g, b);}else if (Depth == 24){int r = ptr[2];int g = ptr[1];int b = ptr[0];c = Color.FromArgb(r, g, b);}else if (Depth == 8){int r = ptr[0];c = Color.FromArgb(r, r, r);}return c;}}public void SetPixel(int x, int y, Color c){unsafe{byte* ptr = (byte*)Iptr;ptr = ptr + bitmapData.Stride * y;ptr += Depth * x / 8;if (Depth == 32){ptr[3] = c.A;ptr[2] = c.R;ptr[1] = c.G;ptr[0] = c.B;}else if (Depth == 24){ptr[2] = c.R;ptr[1] = c.G;ptr[0] = c.B;}else if (Depth == 8){ptr[2] = c.R;ptr[1] = c.G;ptr[0] = c.B;}}}}

?

?

?

?

?使用方法這里就不列出來了,跟上面的LockBitmap類似

?

https://blog.csdn.net/yangyikun0428/article/details/53771596

轉載于:https://www.cnblogs.com/dearzhoubi/p/8710365.html

總結

以上是生活随笔為你收集整理的解决 C# GetPixel 和 SetPixel 效率问题(转)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。