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
        }
    }
  • 相关阅读:
    脚本(js)控制页面输入
    Javascript小技巧(6)
    解析 XML
    防止用户不输入正确用户名,密码登陆
    Javascript小技巧(5)
    自己想了一下为什么在ajax方法里找不到服务器控件
    Javascript小技巧(4)
    C#后台绑事件 和前台js方法
    Community Server专题九:MemberRole之Profile(转载)
    Community Server专题六:Delegates & Events(转载)
  • 原文地址:https://www.cnblogs.com/CodeBase/p/3764529.html
Copyright © 2011-2022 走看看