zoukankan      html  css  js  c++  java
  • Class.FastBitmap

    /*******************************************************************************
     * FastBitmap.cs
     * A fast implementation of System.Drawing.Bitmap class.
     * -----------------------------------------------------------------------------
     * Project: DIG+ Image Library (ABANDONED)
     * Author: Conmajia
     * Url: conmajia@gmail.com
     * Initiate: 9th July, 2012
     * Version: 1.0.0.0
     * History:
     *      9th July, 2012
     *      Initiated.
     * ****************************************************************************/
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Drawing.Imaging;
    using System.Drawing;
    
    namespace Conmajia.Drawing.Image
    {
        /// <summary>
        /// 增强位图类。
        /// </summary>
        /// <remarks>
        /// 高性能 Bitmap 类。
        /// 为DIG+(数字智能游戏)项目设计,目前已废弃。
        /// By Conmajia
        /// </remarks>
        public class FastBitmap : IDisposable, ICloneable
        {
            #region 字段
            internal Bitmap _bitmap;
            BitmapData _bitmapData;
            #endregion
    
            #region 构造与析构
            private FastBitmap() { }
    
            public FastBitmap(Bitmap bitmap)
            {
                _bitmap = bitmap;
            }
            
            public FastBitmap(int width, int height, PixelFormat format)
            {
                _bitmap = new Bitmap(width, height, format);
            }
    
            ~FastBitmap()
            {
                Dispose(false);
            }
    
            public void Dispose()
            {
                GC.SuppressFinalize(this);
                Dispose(true);
            }
    
            protected virtual void Dispose(bool disposing)
            {
                Unlock();
                if (disposing)
                {
                    _bitmap.Dispose();
                }
            }
            #endregion
    
            #region 方法
            public object Clone()
            {
                FastBitmap clone = new FastBitmap();
                clone._bitmap = (Bitmap)_bitmap.Clone();
                return clone;
            }
    
            public int Width
            {
                get { return _bitmap.Width; }
            }
    
            public int Height
            {
                get { return _bitmap.Height; }
            }
    
            public void Lock()
            {
                _bitmapData = _bitmap.LockBits(
                    new Rectangle(0, 0, _bitmap.Width, _bitmap.Height),
                    ImageLockMode.ReadWrite,
                    _bitmap.PixelFormat
                    );
            }
    
            unsafe public Color GetPixel(int x, int y)
            {
                if (_bitmapData.PixelFormat == PixelFormat.Format32bppArgb)
                {
                    byte* b = (byte*)_bitmapData.Scan0 + (y * _bitmapData.Stride) + (x * 4);
                    return Color.FromArgb(*(b + 3), *(b + 2), *(b + 1), *b);
                }
                else if (_bitmapData.PixelFormat == PixelFormat.Format24bppRgb)
                {
                    byte* b = (byte*)_bitmapData.Scan0 + (y * _bitmapData.Stride) + (x * 3);
                    return Color.FromArgb(*(b + 2), *(b + 1), *b);
                }
                return Color.Empty;
            }
    
            unsafe public void SetPixel(int x, int y, Color c)
            {
                if (_bitmapData.PixelFormat == PixelFormat.Format32bppArgb)
                {
                    byte* b = (byte*)_bitmapData.Scan0 + (y * _bitmapData.Stride) + (x * 4);
                    *b = c.B;
                    *(b + 1) = c.G;
                    *(b + 2) = c.R;
                    *(b + 3) = c.A;
                }
                else if (_bitmapData.PixelFormat == PixelFormat.Format24bppRgb)
                {
                    byte* b = (byte*)_bitmapData.Scan0 + (y * _bitmapData.Stride) + (x * 3);
                    *b = c.B;
                    *(b + 1) = c.G;
                    *(b + 2) = c.R;
                }
            }
    
            public Byte GetIntensity(int x, int y)
            {
                Color c = GetPixel(x, y);
                return (byte)((c.R * 0.30 + c.G * 0.59 + c.B * 0.11) + 0.5);
            }
    
            public void Unlock()
            {
                if (_bitmapData != null)
                {
                    _bitmap.UnlockBits(_bitmapData);
                    _bitmapData = null;
                }
            }
    
            public void Save(string filename, ImageFormat format)
            {
                _bitmap.Save(filename, format);
            }
    
            public void Save(string filename)
            {
                _bitmap.Save(filename);
            }
            #endregion
        }
    }
  • 相关阅读:
    XML学习教程
    JSON 的含义?
    局域网共享问题全方位解决
    VMware虚拟化培训手册
    信息化建设中的IT规划精要
    洛谷1265 公路修建
    洛谷1144 最短路计数
    NOI题库05 派
    NOI题库7624 山区建小学(162:Post Office / IOI2000 POST OFFICE [input] )
    vijosP1014 旅行商简化版
  • 原文地址:https://www.cnblogs.com/CodeBase/p/3764529.html
Copyright © 2011-2022 走看看