在对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 bitmap
Width = source.Width;
Height = source.Height; // get total locked pixels count
int PixelCount = Width * Height; // Create rectangle to lock
Rectangle rect = new Rectangle(, , Width, Height); // get source bitmap pixel format size
Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat); // Check if bpp (Bits Per Pixel) is 8, 24, or 32
if (Depth != && Depth != && Depth != )
{
throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
} // Lock bitmap and return bitmap data
bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
source.PixelFormat); // create byte array to copy pixel values
int step = Depth / ;
Pixels = new byte[PixelCount * step];
Iptr = bitmapData.Scan0; // Copy data from pointer to array
Marshal.Copy(Iptr, Pixels, , Pixels.Length);
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// Unlock bitmap data
/// </summary>
public void UnlockBits()
{
try
{
// Copy data from byte array to pointer
Marshal.Copy(Pixels, , 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 count
int cCount = Depth / ; // Get start index of the specified pixel
int i = ((y * Width) + x) * cCount; if (i > Pixels.Length - cCount)
throw new IndexOutOfRangeException(); if (Depth == ) // For 32 bpp get Red, Green, Blue and Alpha
{
byte b = Pixels[i];
byte g = Pixels[i + ];
byte r = Pixels[i + ];
byte a = Pixels[i + ]; // a
clr = Color.FromArgb(a, r, g, b);
}
if (Depth == ) // For 24 bpp get Red, Green and Blue
{
byte b = Pixels[i];
byte g = Pixels[i + ];
byte r = Pixels[i + ];
clr = Color.FromArgb(r, g, b);
}
if (Depth == )
// 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 count
int cCount = Depth / ; // Get start index of the specified pixel
int i = ((y * Width) + x) * cCount; if (Depth == ) // For 32 bpp set Red, Green, Blue and Alpha
{
Pixels[i] = color.B;
Pixels[i + ] = color.G;
Pixels[i + ] = color.R;
Pixels[i + ] = color.A;
}
if (Depth == ) // For 24 bpp set Red, Green and Blue
{
Pixels[i] = color.B;
Pixels[i + ] = color.G;
Pixels[i + ] = color.R;
}
if (Depth == )
// 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(, ); //从内存解锁Bitmap
lockbmp.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 bitmap
Width = source.Width;
Height = source.Height; // get total locked pixels count
int PixelCount = Width * Height; // Create rectangle to lock
Rectangle rect = new Rectangle(, , Width, Height); // get source bitmap pixel format size
Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat); // Check if bpp (Bits Per Pixel) is 8, 24, or 32
if (Depth != && Depth != && Depth != )
{
throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
} // Lock bitmap and return bitmap data
bitmapData = 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 / ;
Color c = Color.Empty;
if (Depth == )
{
int a = ptr[];
int r = ptr[];
int g = ptr[];
int b = ptr[];
c = Color.FromArgb(a, r, g, b);
}
else if (Depth == )
{
int r = ptr[];
int g = ptr[];
int b = ptr[];
c = Color.FromArgb(r, g, b);
}
else if (Depth == )
{
int r = ptr[];
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 / ;
if (Depth == )
{
ptr[] = c.A;
ptr[] = c.R;
ptr[] = c.G;
ptr[] = c.B;
}
else if (Depth == )
{
ptr[] = c.R;
ptr[] = c.G;
ptr[] = c.B;
}
else if (Depth == )
{
ptr[] = c.R;
ptr[] = c.G;
ptr[] = c.B;
}
}
}
}
 

使用方法这里就不列出来了,跟上面的LockBitmap类似

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

解决 C# GetPixel 和 SetPixel 效率问题(转)的更多相关文章

  1. Bitmap之getPixel和setPixel函数

    package com.loaderman.customviewdemo; import android.app.Activity; import android.graphics.Bitmap; i ...

  2. 第一章-第七题( 有人认为,“中文编程”, 是解决中国程序员编程效率一个秘密武器,请问它是一个 “银弹” 么? )--By 侯伟婷

    首先,“银弹”在百度百科中的解释是银色的子弹,我们更熟知的“银弹”一词,应该是在<人月神话>中提到的.银弹原本应该是指某种策略.技术或者技巧可以极大地提高程序员的生产力[1].此题目中关于 ...

  3. Navicat使用常见的两个问题及解决方法,提高开发效率

    Navicat使用常见问题 在我们日常开发过程中,一般不会直接使用命令行来操作 MYSQL 数据库,而会选择一些图形化界面去帮助我们来进行此类操作,常用的有:SQLyog(Logo也是小海豚),Nav ...

  4. 使用Xcode Instruments Leak解决内存泄漏问题

    iOS 5.0之后apple引入了Xcode编译器特性ARC(Automatic Reference Counting,自动引用计数)来帮助开发者管理内存,但为了追求app的高性能与减少安装包大小,工 ...

  5. ButterKnife的使用以及不能自动生成代码问题的解决

    ButterKnife的使用以及不能自动生成代码问题的解决 转载请注明出处:http://www.cnblogs.com/zhengjunfei/p/5910497.html 最近换了个工作刚入职,又 ...

  6. 基于C#的机器学习--贝叶斯定理-执行数据分析解决肇事逃逸之谜

    贝叶斯定理-执行数据分析解决肇事逃逸之谜 ​ 在这一章中,我们将: 应用著名的贝叶斯定理来解决计算机科学中的一个非常著名的问题. 向您展示如何使用贝叶斯定理和朴素贝叶斯来绘制数据,从真值表中发现异常值 ...

  7. 浅谈自底向上的Shell脚本编程及效率优化

    作者:沐星晨 出处:http://blog.csdn.net/sosodream/article/details/6276758 浅谈自底向上的Shell脚本编程及效率优化 小论文,大家多批评指导:) ...

  8. 微服务效率工具 goctl 深度解析(上)

    前言 本文根据 安前松 的视频分享整理而来,视频回放地址如下: https://www.bilibili.com/video/BV1Hr4y1x7Ne goctl 的由来 1. goctl 的诞生 g ...

  9. DirectX中文手册

    目  录 第一章 DirectX基础(初级篇) 第一节  什么是DirectX 一.什么是DirectX ? 二.DirectX的组成部分 三.关于DirectDraw 四.为什么要使用DirectD ...

随机推荐

  1. mapstatetoprops更新state但props不更新渲染的问题

    通过react-redux和redux实现react组件之间的通信,reducer.action.store都编写正确,mapDispatchToProps也能正确传值.唯独mapStateToPro ...

  2. OpenWrt Kernel Module Creation Howto

    OpenWrt Kernel Module Creation Howto About OpenWrt Kernel Module Compilation You are planning to com ...

  3. C++Review6_优先队列priority_queue

    普通队列是一个先进先出的数据结构,元素在队尾添加,在队头删除. 优先队列的出队逻辑相比于普通队列发生了改变,具有最高优先级的元素先出队. 在C++中只要包含了#include<queue> ...

  4. MySQL Server逻辑架构

    1.MySQL基础 MySQL是一个开放源代码的关系数据库管理系统.原开发者为瑞典的MySQL AB公司,最早是在2001年MySQL3.23进入到管理员的视野并在之后获得广泛的应用. 2008年My ...

  5. HDU 2068 RPG错排 [错排公式]

    1.题意:1到N的序列的排列中,元素位置与元素值相对应的情况(值为i的元素在某个排列中正好排在第i个位置)大于等于序列规模一半的情况,有多少个? 2.输入输出:每组数据一个数,N,规定输入以0结尾: ...

  6. 微信小程序酒店日历超强功能

    首先利用date拿到年月日 月记得+1 ,因为是从0开始的 先遍历月份,跨年年+1 ,月归至1: 然后遍历天数, lastDat = new Date(val.year,val.month,0).ge ...

  7. Django 中配置MySQL数据库

    在Django的项目中会默认使用sqlite的数据库 配置MySQL需要在setting.py 里加入以下设置: 配置数据库 DATABASES = { 'default': { 'ENGINE': ...

  8. html页脚固定在底部的方法

    <style type="text/css"> html { height: 100%; } body { height: 100%; margin: 0; paddi ...

  9. C++简单项目--贪吃蛇

    在800*600的地图上,蛇的初始长度为3节,用数组记录每一节的位置(每个正方形左上角的坐标),每一节为长度为10的正方形,初始方向向右.随机生成30个障碍物的的位置,随机生成食物的位置.吃到食物之后 ...

  10. 「Luogu P3395」路障 解题报告

    点开有惊喜 其实是题面 这D1T1给的很有面子! 我居然做的来! 从左上角走到右上角 然后n<=1000 所以果断放弃DFS,选择BFS 思路还是一样的BFS 证明: 走到一个点的时间越早越好( ...