Bitmap Pixel 高速化處理

http://zip.nvp.com.tw/forum.php?mod=viewthread&tid=1104&extra=page%3D29

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
namespace Bitmap_Pixel_CS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            this.pictureBox1.Image = Image.FromFile(@"c:windowsOPEN-chan_no33_1280.jpg");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Bitmap bmp = (Bitmap)Image.FromFile(@"c:windowsOPEN-chan_no33_1280.jpg");
            BitmapPlus bmpP = new BitmapPlus(bmp);
            bmpP.BeginAccess();
            for (int i = 0; i < bmp.Width; i++)
            {
                for (int j = 0; j < bmp.Height; j++)
                {
                    Color bmpCol = bmpP.GetPixel(i, j);
                    if (bmpCol.R == (byte)0)
                    {
                        bmpP.SetPixel(i, j, Color.FromArgb(255, 255, 0, 0));
                    }
                    else
                    {
                        bmpP.SetPixel(i, j, Color.FromArgb(255, 0, 0, 0));
                    }
                }
            }
            bmpP.EndAccess();
            pictureBox1.Image = bmp;
        }
        class BitmapPlus
        {
            private Bitmap _bmp = null;
            private BitmapData _img = null;
            private int _BytesPerPixel;
            public BitmapPlus(Bitmap original)
            {
                _bmp = original;
                _BytesPerPixel = Bitmap.GetPixelFormatSize(_bmp.PixelFormat) / 8;
            }
            public void BeginAccess()
            {
                _img = _bmp.LockBits(new Rectangle(0, 0, _bmp.Width, _bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, _bmp.PixelFormat);
            }
            public void EndAccess()
            {
                if (_img != null)
                {
                    _bmp.UnlockBits(_img);
                    _img = null;
                }
            }
            public Color GetPixel(int x, int y)
            {
                if (_img == null)
                {
                    return _bmp.GetPixel(x, y);
                }
                IntPtr adr = _img.Scan0;
                int pos = x * _BytesPerPixel + _img.Stride * y;
                byte b = System.Runtime.InteropServices.Marshal.ReadByte(adr, pos + 0);
                byte g = System.Runtime.InteropServices.Marshal.ReadByte(adr, pos + 1);
                byte r = System.Runtime.InteropServices.Marshal.ReadByte(adr, pos + 2);
                return Color.FromArgb(r, g, b);
            }
            public void SetPixel(int x, int y, Color col)
            {
                if (_img == null)
                {
                    _bmp.SetPixel(x, y, col);
                    return;
                }
                IntPtr adr = _img.Scan0;
                int pos = x * _BytesPerPixel + _img.Stride * y;
                System.Runtime.InteropServices.Marshal.WriteByte(adr, pos + 0, col.B);
                System.Runtime.InteropServices.Marshal.WriteByte(adr, pos + 1, col.G);
                System.Runtime.InteropServices.Marshal.WriteByte(adr, pos + 2, col.R);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值